使用 Amazon Command Line Interface (Amazon CLI) 启用跳过匹配的实例刷新示例
预设情况下,Amazon EC2 Auto Scaling 可以在实例刷新期间替换 Auto Scaling 组中的任何实例。通过启用跳过匹配,可避免替换已包含所需配置的实例。
跳过匹配可以更高效地执行以下操作:
-
启动一个或多个测试实例后,从启动配置迁移到启动模板的默认或最新版本。
-
从不需要的实例类型迁移到更适合您的应用程序的实例类型。
-
作为失败或取消的实例刷新的一部分,替换一个或多个实例后的回滚更改。
跳过匹配功能不能用于更新使用启动配置的 Auto Scaling 组,除非为所需配置指定了启动模板。
以下 Amazon CLI 示例演示了使用跳过匹配的几种情景。如果在运行这些命令遇到任何错误,请确保您已在本地将 Amazon CLI 更新到最新版本。
迁移到启动模板的默认版本
以下示例显示了 start-instance-refresh 命令,该命令将 Auto Scaling 组更新为启动模板的默认版本。如果有任何实例已使用指定启动模板的默认版本,则不会替换它们。
aws autoscaling start-instance-refresh --cli-input-json file://config.json
config.json 的内容。
{ "AutoScalingGroupName": "my-asg", "DesiredConfiguration": { "LaunchTemplate": { "LaunchTemplateId": "lt-068f72b729example", "Version": "$Default" } }, "Preferences": { "SkipMatching": true } }
如果成功,此命令会返回包含实例刷新 ID 的 JSON 响应。
迁移到启动模板的最新版本
以下示例显示了 start-instance-refresh 命令,该命令将 Auto Scaling 组更新为启动模板的最新版本。如果有任何实例已使用指定启动模板的最新版本,则不会替换它们。
aws autoscaling start-instance-refresh --cli-input-json file://config.json
config.json 的内容。
{ "AutoScalingGroupName": "my-asg", "DesiredConfiguration": { "LaunchTemplate": { "LaunchTemplateId": "lt-068f72b729example", "Version": "$Latest" } }, "Preferences": { "SkipMatching": true } }
如果成功,此命令会返回包含实例刷新 ID 的 JSON 响应。
跳过匹配和混合实例组
要更新混合实例组,必须在所需配置中为混合实例策略指定设置。对于所需配置中未提供的任何混合实例策略参数,Amazon EC2 Auto Scaling 会将参数值重置为默认值。
迁移到启动模板的默认版本
以下示例显示了 start-instance-refresh 命令,该命令将混合实例组更新为启动模板的默认版本。如果有任何实例已使用指定启动模板的默认版本,则不会替换它们。
aws autoscaling start-instance-refresh --cli-input-json file://config.json
config.json 的内容。
{ "AutoScalingGroupName":"my-asg", "DesiredConfiguration":{ "MixedInstancesPolicy":{ "LaunchTemplate":{ "LaunchTemplateSpecification":{ "LaunchTemplateId":"lt-068f72b729example", "Version":"$Default" }, "Overrides":[ ... existing instance types ... ] }, "InstancesDistribution":{ "OnDemandPercentageAboveBaseCapacity":50, "SpotAllocationStrategy":"capacity-optimized" } } }, "Preferences":{ "SkipMatching":true } }
如果成功,此命令会返回包含实例刷新 ID 的 JSON 响应。
迁移到启动模板的最新版本
以下示例显示了 start-instance-refresh 命令,该命令将混合实例组更新为启动模板的最新版本。如果有任何实例已使用指定启动模板的最新版本,则不会替换它们。
aws autoscaling start-instance-refresh --cli-input-json file://config.json
config.json 的内容。
{ "AutoScalingGroupName":"my-asg", "DesiredConfiguration":{ "MixedInstancesPolicy":{ "LaunchTemplate":{ "LaunchTemplateSpecification":{ "LaunchTemplateId":"lt-068f72b729example", "Version":"$Latest" }, "Overrides":[ ... existing instance types ... ] }, "InstancesDistribution":{ "OnDemandPercentageAboveBaseCapacity":50, "SpotAllocationStrategy":"capacity-optimized" } } }, "Preferences":{ "SkipMatching":true } }
如果成功,此命令会返回包含实例刷新 ID 的 JSON 响应。
从不需要的实例类型迁移
当您拥有混合实例组时,通常具有一组用于预置实例的启动模板覆盖(实例类型)。实例类型包含在 Overrides 部分中。要告诉 Amazon EC2 Auto Scaling 您要替换使用特定实例类型的实例,您的所需配置必须指定不包含不需要实例类型的 Overrides 部分。当组中的实例类型与 Overrides 部分中的实例类型之一不匹配时,实例将被替换为实例刷新的一部分。请注意,实例刷新不会选择要从中预置新实例的实例池;而是分配策略会做到这一点。
以下示例显示了 start-instance-refresh 命令,该命令通过替换与所需配置中指定的实例类型不匹配的任何实例来更新混合实例组。
aws autoscaling start-instance-refresh --cli-input-json file://config.json
config.json 的内容。
{ "AutoScalingGroupName":"my-asg", "DesiredConfiguration":{ "MixedInstancesPolicy":{ "LaunchTemplate":{ "LaunchTemplateSpecification":{ ... existing launch template and version ... }, "Overrides":[ { "InstanceType":"c5.large" }, { "InstanceType":"c5a.large" }, { "InstanceType":"m5.large" }, { "InstanceType":"m5a.large" } ] }, "InstancesDistribution":{ "OnDemandPercentageAboveBaseCapacity":50, "SpotAllocationStrategy":"capacity-optimized" } } }, "Preferences":{ "SkipMatching":true } }
如果成功,此命令会返回包含实例刷新 ID 的 JSON 响应。