ASG实例级别的操作

除了设置ASG整体控制EC2的扩容与缩容外,在实例层面也有一些单独的操作,比如:

  • 将某个实例从ASG中摘除(detach操作)
  • 将某个实例设置为Standby,不让它接收线上流量;再把它设置成InService
  • 在ASG缩容时,不允许缩掉某台实例(scale-in protection)

image-20221008223320964

我们将一一介绍这三种操作

Detach

可以将ASG中InService状态的某台机器从中分离出来,通常用于:

  • 将某台机器从一个ASG中分离出来,然后挂到另一个ASG上面
  • 将EC2挂到先某个ASG上进行测试,测试完成后再分离出来

在进行detach操作时,要注意:

  • 如果ASG此时的机器数量等于minimum capacity,则操作会失败
  • 如果detach后,当前数量小于desired capacity,ASG会重新拉起新的机器
  • 如果ASG挂在ELB后面,则detach时,会等待deregistration delay完成。

Detach实验

在ASG的Instance management页面,将某台EC2进行Detach:

image-20221009094517890

点击Detach instance

image-20221009094556007

发现报错,因为当前minimum capacity = 2, Detach这台机器后会使ASG机器数量小于这个值:

image-20221009094609217

在Detach时,有一个操作是先补一台EC2到ASG中,再进行Detach操作:

image-20221009094630740

重新进行Detach操作,此时新加一台机器和Detach原来的机器并行进行:

image-20221009094659452

Set to standby

Detach操作是将某台机器从ASG中移除;Set to standby是临时设置,机器还存在于ASG中,只是不接收线上流量,然后随时还能将它设置成InService状态。

You can put an instance that is in the InService state into the Standby state, update or troubleshoot the instance, and then return the instance to service. Instances that are on standby are still part of the Auto Scaling group, but they do not actively handle load balancer traffic.

This feature helps you stop and start the instances or reboot them without worrying about Amazon EC2 Auto Scaling terminating the instances as part of its health checks or during scale-in events.

For example, you can change the Amazon Machine Image (AMI) for an Auto Scaling group at any time by changing the launch template or launch configuration. Any subsequent instances that the Auto Scaling group launches use this AMI. However, the Auto Scaling group does not update the instances that are currently in service. You can terminate these instances and let Amazon EC2 Auto Scaling replace them, or use the instance refresh feature to terminate and replace the instances. Or, you can put the instances on standby, update the software, and then put the instances back in service.

Detaching instances from an Auto Scaling group is similar to putting instances on standby. Detaching instances might be useful if you want to manage the instances like standalone EC2 instances and possibly terminate them. For more information, see Detach EC2 instances from your Auto Scaling group .

When you put instances on standby, your Auto Scaling group can become unbalanced between Availability Zones. Amazon EC2 Auto Scaling compensates by rebalancing the Availability Zones unless you suspend the AZRebalance process. For more information, see Suspend and resume a process for an Auto Scaling group .

How the standby state works

The standby state works as follows to help you temporarily remove an instance from your Auto Scaling group:

  1. You put the instance into the standby state. The instance remains in this state until you exit the standby state.
  2. If there is a load balancer target group or Classic Load Balancer attached to your Auto Scaling group, the instance is deregistered from the load balancer. If connection draining is enabled for the load balancer, Elastic Load Balancing waits 300 seconds by default before completing the deregistration process, which helps in-flight requests to complete.
  3. By default, the value that you specified as your desired capacity is decremented when you put an instance on standby. This prevents the launch of an additional instance while you have this instance on standby. Alternatively, you can specify that your desired capacity is not decremented. If you specify this option, the Auto Scaling group launches an instance to replace the one on standby. The intention is to help you maintain capacity for your application while one or more instances are on standby.
  4. You can update or troubleshoot the instance.
  5. You return the instance to service by exiting the standby state.
  6. After you put an instance that was on standby back in service, the desired capacity is incremented. If you did not decrement the capacity when you put the instance on standby, the Auto Scaling group detects that you have more instances than you need. It applies the termination policy in effect to reduce the size of the group. For more information, see Control which Auto Scaling instances terminate during scale in .
  7. If there is a load balancer target group or Classic Load Balancer attached to your Auto Scaling group, the instance is registered with the load balancer.

                     Instances enter and exit the standby state.

Health status of an instance in a standby state

Amazon EC2 Auto Scaling does not perform health checks on instances that are in a standby state. While the instance is in a standby state, its health status reflects the status that it had before you put it on standby. Amazon EC2 Auto Scaling does not perform a health check on the instance until you put it back in service.

For example, if you put a healthy instance on standby and then terminate it, Amazon EC2 Auto Scaling continues to report the instance as healthy. If you attempt to put a terminated instance that was on standby back in service, Amazon EC2 Auto Scaling performs a health check on the instance, determines that it is terminating and unhealthy, and launches a replacement instance. For an introduction to health checks, see Health checks for Auto Scaling instances .

如果我们想查某台机器的问题,先把它从ASG中剥离,查完之后将其再恢复到ASG中。此时可以使用standby方式:

image-20191104105713896

剥离的同时,可以同时新建一台实例补上去:

image-20191104105821846

状态稳定后:

image-20191104110438737

可以将备用的机器重新恢复成可用:

image-20191104110453212

image-20191104110751444

实例保护

如果某台机器特别重要,不想让ASG将其terminate,可以设置实例保护:

image-20191104111209016

极端情况下,将所有机器都设置实例保护:

image-20191104111227523

此时更改所需要机器数量,不会影响到这两台被保护的机器:

image-20191104111254309

image-20191104111708499