如何在自动伸缩组内按计划停止/启动EC2实例而不终止该实例? [英] How to stop/start an EC2 instance on a schedule within an autoscale group without terminating the instance?

查看:88
本文介绍了如何在自动伸缩组内按计划停止/启动EC2实例而不终止该实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您有一个自动扩展组,如何开始/停止实例(启动/终止)?默认行为仅适用于启动和终止实例,但是由于我们还因为停止实例而节省了成本,因此我们宁愿允许该实例继续存在。



的目的,我们有一个QA环境,我们想每天启动/停止几个自动伸缩组,以在开发团队晚上和周末离开办公室时省钱。



要做到这一点?



注意:



我将跟进

解决方案

首先,AWS自动伸缩组是基于启动的多个实例的容器组态。如果我们能够禁用触发向上/向下缩放的进程,那么我们回到仅保存实例的容器。



为了禁用这些进程,我们需要使用 suspend-processes 命令从AWS-CLI。在此示例中,我将使用 powershell ,但是在 bash 中写起来同样容易:

 #暂停HealthCheck和ReplaceUnhealthy进程,您可能会发现另一种组合更适合您。 
$ asGroup = nameOfYourAutoScalingGroup;
aws自动缩放暂停过程`
--auto-scaling-group-name $ asGroup`
--scaling-processes HealthCheck ReplaceUnhealthy;

#验证更改
awsp autoscaling describe-auto-scaling-groups`
--auto-scaling-group-name $ asGroup;为了我的目的,我希望实例在上午7点到晚上7点之间处于联机状态以降低成本。这些实例在我们的开发团队和质量检查团队之间使用,他们希望每天保持服务器状态。



注意:在EC2实例损坏或意外终止的情况下,我们的团队可以将其回滚到最新的AMI(它们确实只是希望日志继续存在,但是如果它们丢失了,这不是世界末日)就可以了。



接下来,我们将需要一个脚本来启动/停止服务器,这里有2个脚本,但是您可以轻松地将其优化为一个脚本并传递一个参数在:

 #在我们的例子中,我们想对所有自动伸缩组$ b $执行此操作b#您需要Powershell 3.0+才能使用ConvertFrom-Json 
$ asGroups = aws autoscaling describe-auto-scaling-groups --query'AutoScalingGroups [*]。{Name:AutoScalingGroupName,Instances:Instances [ *]。InstanceId}';
$ asGroups = {asGroups:$ asGroups} | ConvertFrom-Json;

#foreach自动伸缩组,遍历每个实例并启动
foreach($ asGroup in $($ asGroups.asGroups)){

echo AS:$ ($ asGroup.Name);
foreach($ asGroup.instances中的$ instance){
echo起始实例:$ instance;
aws ec2起始实例`
--instance-ids $ instance;
}
}



 #在我们的例子中,我们想对所有自动缩放组执行此操作
#您需要Powershell 3.0+才能使用ConvertFrom-Json
$ asGroups = awsp自动缩放describe-auto -scaling-groups --query'AutoScalingGroups [*]。{Name:AutoScalingGroupName,Instances:Instances [*]。InstanceId}';
$ asGroups = {asGroups:$ asGroups} | ConvertFrom-Json;

#foreach自动伸缩组,遍历每个实例并停止
foreach($ asGroup in $($ asGroups.asGroups)){

echo AS:$ ($ asGroup.Name);
foreach($ asGroup.instances中的$ instance){
echo停止实例:$ instance;
awsp ec2停止实例`
--instance-ids $ instance;
}
}

最后一步是将其添加到计划的控制服务器上的任务(我目前仅使用永不关闭的桌面)。随附的示例是导出的计划任务的示例,它每周一,周一,周二,周三,周四,周五上午7点运行。

 <?xml version = 1.0 encoding = UTF-16?> ; 
<任务版本= 1.2 xmlns = http://schemas.microsoft.com/windows/2004/02/mit/task>
< RegistrationInfo>
< Date> 2014-08-22T13:13:02.2103946< / Date>
< Author> localhost\Administrator< / Author>
< / RegistrationInfo>
< Triggers>
< CalendarTrigger>
< StartBoundary> 2014-08-22T07:00:00< / StartBoundary>
< Enabled> true< / Enabled>
< ScheduleByWeek>
< DaysOfWeek>
<星期一/>
<星期二/>
<星期三/>
<星期四/>
<星期五/>
< / DaysOfWeek>
< WeeksInterval> 1< / WeeksInterval>
< / ScheduleByWeek>
< / CalendarTrigger>
< / Triggers>
< Principals>
< Principal id = Author>
< UserId> localhost\Administrator< / UserId>
< LogonType> InteractiveToken< / LogonType>
< RunLevel>最低特权< / RunLevel>
< / Principal>
< / Principals>
<设置>
< MultipleInstancesPolicy> IgnoreNew< / MultipleInstancesPolicy>
< DisallowStartIfOnBatteries> true< / DisallowStartIfOnBatteries>
< StopIfGoingOnBatteries> true< / StopIfGoingOnBatteries>
< AllowHardTerminate> true< / AllowHardTerminate>
< StartWhenAvailable> false< / StartWhenAvailable>
< RunOnlyIfNetworkAvailable> false< / RunOnlyIfNetworkAvailable>
< IdleSettings>
< StopOnIdleEnd> true< / StopOnIdleEnd>
< RestartOnIdle> false< / RestartOnIdle>
< / IdleSettings>
< AllowStartOnDemand> true< / AllowStartOnDemand>
< Enabled> true< / Enabled>
< Hidden> false< / Hidden>
< RunOnlyIfIdle> false< / RunOnlyIfIdle>
< WakeToRun> false< / WakeToRun>
< ExecutionTimeLimit> P3D< / ExecutionTimeLimit>
< Priority> 7< / Priority>
< /设置>
< Actions Context = Author>
< Exec>
< Command> C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe< / Command>
< Arguments> -ExecutionPolicy Bypass c:\tasks\AWS-Autoscaling-EC2-Start-Morning.ps1< / Arguments>
< WorkingDirectory> c:\tasks< / WorkingDirectory>
< / Exec>
< / Actions>
< / Task>

您需要执行此任务的停止版本才能在晚上7点停止服务器。只需将您的开始边界更改为< StartBoundary> 2014-08-22T19:00:00< / StartBoundary> 并更新< Arguments> -ExecutionPolicy绕过c:\tasks\AWS-Autoscaling-EC2-Start-Morning.ps1< / Arguments> 到正确的 ps1


If you have have an autoscaling group, how do you go about start/stopping the instances (not launch/terminate) ? The default behaviour is only for launching and terminating instances, but since we also get cost savings for just stopping an instance, we would rather allow the instance to persist.

For our purposes, we have a QA environment and we want to start/stop several autoscaling groups daily to save money when the development team leaves the office for evenings and weekends.

So how to accomplish this?

NOTE:

I will be following up with my own detailed answer shortly.

解决方案

First and foremost, the AWS autoscaling group is a container for multiple instances that are based on a launch configuration. If we are able to disable the processes that trigger up/down scaling, we are back to the container that just holds instances.

In order to disable these processes, we need to use the suspend-processes command from AWS-CLI. For this example, I will use powershell, but its just as easy to write in bash:

# suspend HealthCheck and ReplaceUnhealthy processes, you may find another combination works better for you.
$asGroup = "nameOfYourAutoScalingGroup" ;
aws autoscaling suspend-processes `
    --auto-scaling-group-name $asGroup `
    --scaling-processes HealthCheck ReplaceUnhealthy ;

# verify the change
awsp autoscaling describe-auto-scaling-groups `
    --auto-scaling-group-name $asGroup ;

For my purposes, I wanted the instances to be online between 7am and 7pm to reduce costs. These instances are used between our development and QA teams, and they prefer to keep the state of the server from day to day.

NOTE: in the circumstance where the EC2 instance becomes corrupt or accidentally terminated, our team is fine with rollbacking back to the latest AMI (they really just want the logs to persist, but if they are lost, it isn't the end of the world)

Next we'll require a script to start/stop the servers, here I have it as 2 scripts, but you can easily optimize it into one script and pass an argument in:

# in our case, we want to perform this to all autoscaling groups
# you'll need Powershell 3.0+ in order to use ConvertFrom-Json
$asGroups = aws autoscaling describe-auto-scaling-groups --query 'AutoScalingGroups[*].{Name:AutoScalingGroupName,Instances:Instances[*].InstanceId}' ;
$asGroups = "{ asGroups: $asGroups }" | ConvertFrom-Json ;

# foreach autoscaling group, go through each instance and start
foreach ($asGroup in $($asGroups.asGroups)) {

    echo "AS: $($asGroup.Name)" ;
    foreach ($instance in $asGroup.instances) {
        echo "starting instance: $instance";
        aws ec2 start-instances `
            --instance-ids $instance ;
    }
}

# in our case, we want to perform this to all autoscaling groups
# you'll need Powershell 3.0+ in order to use ConvertFrom-Json
$asGroups = awsp autoscaling describe-auto-scaling-groups --query 'AutoScalingGroups[*].{Name:AutoScalingGroupName,Instances:Instances[*].InstanceId}' ;
$asGroups = "{ asGroups: $asGroups }" | ConvertFrom-Json ;

# foreach autoscaling group, go through each instance and stop
foreach ($asGroup in $($asGroups.asGroups)) {

    echo "AS: $($asGroup.Name)" ;
    foreach ($instance in $asGroup.instances) {
        echo "stopping instance: $instance";
        awsp ec2 stop-instances `
            --instance-ids $instance ;
    }
}

The last step would be to add it to the Scheduled Tasks on a control server (I'm currently just using my desktop which never turns off). Attached is an example of the exported Schedule Task, runs weekly Mon, Tue, Wed, Thurs, Friday at 7am.

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2014-08-22T13:13:02.2103946</Date>
    <Author>localhost\Administrator</Author>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <StartBoundary>2014-08-22T07:00:00</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByWeek>
        <DaysOfWeek>
          <Monday />
          <Tuesday />
          <Wednesday />
          <Thursday />
          <Friday />
        </DaysOfWeek>
        <WeeksInterval>1</WeeksInterval>
      </ScheduleByWeek>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>localhost\Administrator</UserId>
      <LogonType>InteractiveToken</LogonType>
      <RunLevel>LeastPrivilege</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>P3D</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</Command>
      <Arguments>-ExecutionPolicy ByPass c:\tasks\AWS-Autoscaling-EC2-Start-Morning.ps1</Arguments>
      <WorkingDirectory>c:\tasks</WorkingDirectory>
    </Exec>
  </Actions>
</Task>

You'll need to make the "Stop" version of this task to stop servers at 7pm. Just change your start boundary to <StartBoundary>2014-08-22T19:00:00</StartBoundary> and update <Arguments>-ExecutionPolicy ByPass c:\tasks\AWS-Autoscaling-EC2-Start-Morning.ps1</Arguments> to the correct ps1.

这篇关于如何在自动伸缩组内按计划停止/启动EC2实例而不终止该实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆