为了解决这个问题,我们可以使用AWS EC2 Auto Scaling组合Spot Instances和On-Demand Instances,确保在Spot Instances被驱逐时可以启动相应数量的On-Demand Instances,以保持可用性。下面是一个示例代码:
Auto Scaling Group 配置示例:
"AutoScalingGroupName": "ASGSpotOnDemandExample",
"MinSize": 1,
"MaxSize": 10,
"DesiredCapacity": 1,
"AvailabilityZones": [
"us-west-2a",
"us-west-2b",
"us-west-2c"
],
"SpotPrice": "0.05",
"LaunchConfigurationName": "LCExample",
"MixedInstancesPolicy": {
"LaunchTemplate": {
"LaunchTemplateSpecification": {
"LaunchTemplateId": "lt-0123456789abcdef0",
"Version": "$Latest"
},
"Overrides": [
{
"InstanceType": "t2.micro",
"WeightedCapacity": "1",
"Priority": 1
},
{
"InstanceType": "c4.large",
"WeightedCapacity": "2",
"Priority": 2
}
]
},
"InstancesDistribution": {
"OnDemandPercentageAboveBaseCapacity": 0,
"SpotAllocationStrategy": "lowest-price",
"SpotInstancePools": 2,
"BaseCapacity": 0
}
}
在这种情况下,Auto Scaling Group将启动1个On-Demand Instance和2个Spot Instances,并将使用最新版本的LaunchTemplate。如果Spot Instances被驱逐,Auto Scaling将自动启动相应的On-Demand Instances以维持容量。
在Spot Instances的单个替代模式下,可以使用 EC2 Fleet 代替 Spot Instances 和 On-Demand Instances 组合。 EC2 Fleet 具有相同的目的,但更好地满足