AWS 是否可以定期自动终止并为 Auto Scaling 组重新创建新实例?
AWS Is it possible to automatically terminate and recreate new instances for an auto scaling group periodically?
我们有一个 AWS 扩展组,在负载均衡器后面有 10-20 台服务器。在 运行 几周后,一些服务器出现故障。我们不知道为什么服务器会变坏,我们需要一些时间才能到达可以调试此问题的阶段。
在此期间,有没有办法告诉 AWS 以受控方式(一个接一个)终止扩展组中的所有实例,直到所有实例每周左右被新实例替换?
您可以使用数据管道非常有效地实现这一目标。
这是 How do I stop and start Amazon EC2 Instances at scheduled intervals with AWS Data Pipeline?
的开发者指南
Auto Scaling 中没有告诉它自动终止和替换实例的功能。但是,您可以编写此类功能的脚本。
假设:
- 终止超过特定小时数的实例
- 一次做一个以避免影响可用容量
- 您想立即更换它们
合适的脚本将执行以下操作:
- 使用
describe-auto-scaling-instances
遍历给定 Auto-Scaling 组中的所有实例
- 如果实例属于所需的 Auto Scaling 组,通过
describe-instances
检索其启动时间
- 如果实例早于所需的小时数,使用
terminate-instance-in-auto-scaling-group
和 --no-should-decrement-desired-capacity
终止它,以便自动替换它
- 然后,等待几分钟让它被替换并继续循环
可以使用 AWS Command-Line Interface (CLI) 或 Python 等编程语言创建脚本。
或者,您可以将实例编程为在给定时间(例如72 小时)后通过简单地调用操作系统关闭实例来自毁。这将导致自动缩放终止实例并替换它。
有两种方法可以实现您正在寻找的目标,计划的 Auto Scaling 操作或将它们作为 ASG 中的实例之一。
Scheduled Scaling
Scaling based on a schedule allows you to scale your application in response to predictable load changes. For example, every week the traffic to your web application starts to increase on Wednesday, remains high on Thursday, and starts to decrease on Friday. You can plan your scaling activities based on the predictable traffic patterns of your web application.
https://docs.aws.amazon.com/autoscaling/latest/userguide/schedule_time.html
你很可能想要这个。
Auto Scaling enables you to 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 application traffic.
https://docs.aws.amazon.com/autoscaling/latest/userguide/as-enter-exit-standby.html
截至 2019 年 11 月 20 日,EC2 AutoScaling 支持最大实例生命周期:https://aws.amazon.com/about-aws/whats-new/2019/11/amazon-ec2-auto-scaling-supports-max-instance-lifetime/
发件人:
The maximum instance lifetime specifies the maximum amount of time (in
seconds) that an instance can be in service. The maximum duration
applies to all current and future instances in the group. As an
instance approaches its maximum duration, it is terminated and
replaced, and cannot be used again.
When configuring the maximum instance lifetime for your Auto Scaling
group, you must specify a value of at least 86,400 seconds (1 day). To
clear a previously set value, specify a new value of 0.
我们有一个 AWS 扩展组,在负载均衡器后面有 10-20 台服务器。在 运行 几周后,一些服务器出现故障。我们不知道为什么服务器会变坏,我们需要一些时间才能到达可以调试此问题的阶段。
在此期间,有没有办法告诉 AWS 以受控方式(一个接一个)终止扩展组中的所有实例,直到所有实例每周左右被新实例替换?
您可以使用数据管道非常有效地实现这一目标。
这是 How do I stop and start Amazon EC2 Instances at scheduled intervals with AWS Data Pipeline?
的开发者指南Auto Scaling 中没有告诉它自动终止和替换实例的功能。但是,您可以编写此类功能的脚本。
假设:
- 终止超过特定小时数的实例
- 一次做一个以避免影响可用容量
- 您想立即更换它们
合适的脚本将执行以下操作:
- 使用
describe-auto-scaling-instances
遍历给定 Auto-Scaling 组中的所有实例
- 如果实例属于所需的 Auto Scaling 组,通过
describe-instances
检索其启动时间
- 如果实例早于所需的小时数,使用
terminate-instance-in-auto-scaling-group
和--no-should-decrement-desired-capacity
终止它,以便自动替换它 - 然后,等待几分钟让它被替换并继续循环
可以使用 AWS Command-Line Interface (CLI) 或 Python 等编程语言创建脚本。
或者,您可以将实例编程为在给定时间(例如72 小时)后通过简单地调用操作系统关闭实例来自毁。这将导致自动缩放终止实例并替换它。
有两种方法可以实现您正在寻找的目标,计划的 Auto Scaling 操作或将它们作为 ASG 中的实例之一。
Scheduled Scaling
Scaling based on a schedule allows you to scale your application in response to predictable load changes. For example, every week the traffic to your web application starts to increase on Wednesday, remains high on Thursday, and starts to decrease on Friday. You can plan your scaling activities based on the predictable traffic patterns of your web application.
https://docs.aws.amazon.com/autoscaling/latest/userguide/schedule_time.html
你很可能想要这个。
Auto Scaling enables you to 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 application traffic.
https://docs.aws.amazon.com/autoscaling/latest/userguide/as-enter-exit-standby.html
截至 2019 年 11 月 20 日,EC2 AutoScaling 支持最大实例生命周期:https://aws.amazon.com/about-aws/whats-new/2019/11/amazon-ec2-auto-scaling-supports-max-instance-lifetime/
发件人:
The maximum instance lifetime specifies the maximum amount of time (in seconds) that an instance can be in service. The maximum duration applies to all current and future instances in the group. As an instance approaches its maximum duration, it is terminated and replaced, and cannot be used again.
When configuring the maximum instance lifetime for your Auto Scaling group, you must specify a value of at least 86,400 seconds (1 day). To clear a previously set value, specify a new value of 0.