Spot 队列示例配置 - Amazon Elastic Compute Cloud
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

Spot 队列示例配置

以下各示例显示了可与 request-spot-fleet 命令结合使用以创建 Spot 队列请求的启动配置。有关更多信息,请参阅创建 Spot 队列请求

注意

对于 Spot 队列,您无法在启动规范中指定网络接口 ID。请确保在启动规范中省略 NetworkInterfaceID 参数。

示例 1:使用区域中价格最低的可用区或子网启动 Spot 实例

以下示例指定一个没有可用区或子网的启动规范。Spot 队列会在具有默认子网且价格最低的可用区中启动实例。您支付的价格不得超过按需价格。

{ "TargetCapacity": 20, "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role", "LaunchSpecifications": [ { "ImageId": "ami-1a2b3c4d", "KeyName": "my-key-pair", "SecurityGroups": [ { "GroupId": "sg-1a2b3c4d" } ], "InstanceType": "m3.medium", "IamInstanceProfile": { "Arn": "arn:aws:iam::123456789012:instance-profile/my-iam-role" } } ] }

示例 2:使用指定列表中价格最低的可用区或子网启动 Spot 实例

以下示例指定具有的可用区或子网不同但实例类型和 AMI 相同的两种启动规范。

可用区

Spot 队列会在价格最低的指定可用区的默认子网中启动实例。

{ "TargetCapacity": 20, "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role", "LaunchSpecifications": [ { "ImageId": "ami-1a2b3c4d", "KeyName": "my-key-pair", "SecurityGroups": [ { "GroupId": "sg-1a2b3c4d" } ], "InstanceType": "m3.medium", "Placement": { "AvailabilityZone": "us-west-2a, us-west-2b" }, "IamInstanceProfile": { "Arn": "arn:aws:iam::123456789012:instance-profile/my-iam-role" } } ] }

子网

您可以指定默认子网或非默认子网,并且非默认子网可来自默认 VPC 或非默认 VPC。Spot 服务会在位于价格最低的可用区的子网中启动实例。

您无法在 Spot 队列请求中指定来自相同可用区的不同子网。

{ "TargetCapacity": 20, "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role", "LaunchSpecifications": [ { "ImageId": "ami-1a2b3c4d", "KeyName": "my-key-pair", "SecurityGroups": [ { "GroupId": "sg-1a2b3c4d" } ], "InstanceType": "m3.medium", "SubnetId": "subnet-a61dafcf, subnet-65ea5f08", "IamInstanceProfile": { "Arn": "arn:aws:iam::123456789012:instance-profile/my-iam-role" } } ] }

如果在默认 VPC 中启动实例,则实例在默认情况下会收到一个公有 IPv4 地址。如果在非默认 VPC 中启动实例,则实例在默认情况下不会收到一个公有 IPv4 地址。在启动规范中使用网络接口来将一个公有 IPv4 地址分配给在非默认 VPC 中启动的实例。指定网络接口时,您必须包括使用网络接口的子网 ID 和安全组 ID。

... { "ImageId": "ami-1a2b3c4d", "KeyName": "my-key-pair", "InstanceType": "m3.medium", "NetworkInterfaces": [ { "DeviceIndex": 0, "SubnetId": "subnet-1a2b3c4d", "Groups": [ "sg-1a2b3c4d" ], "AssociatePublicIpAddress": true } ], "IamInstanceProfile": { "Arn": "arn:aws:iam::880185128111:instance-profile/my-iam-role" } } ...

示例 3:使用指定列表中价格最低的实例类型启动 Spot 实例

以下示例指定实例类型不同、但 AMI 和可用区或子网相同的两种启动配置。Spot 队列使用价格最低的指定实例类型启动实例。

可用区

{ "TargetCapacity": 20, "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role", "LaunchSpecifications": [ { "ImageId": "ami-1a2b3c4d", "SecurityGroups": [ { "GroupId": "sg-1a2b3c4d" } ], "InstanceType": "cc2.8xlarge", "Placement": { "AvailabilityZone": "us-west-2b" } }, { "ImageId": "ami-1a2b3c4d", "SecurityGroups": [ { "GroupId": "sg-1a2b3c4d" } ], "InstanceType": "r3.8xlarge", "Placement": { "AvailabilityZone": "us-west-2b" } } ] }

子网

{ "TargetCapacity": 20, "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role", "LaunchSpecifications": [ { "ImageId": "ami-1a2b3c4d", "SecurityGroups": [ { "GroupId": "sg-1a2b3c4d" } ], "InstanceType": "cc2.8xlarge", "SubnetId": "subnet-1a2b3c4d" }, { "ImageId": "ami-1a2b3c4d", "SecurityGroups": [ { "GroupId": "sg-1a2b3c4d" } ], "InstanceType": "r3.8xlarge", "SubnetId": "subnet-1a2b3c4d" } ] }

示例 4. 覆盖请求的价格

我们建议您使用默认最高价 (这是按需价格)。如果愿意,您可以为队列请求以及各个启动规范指定最高价。

以下示例为队列请求以及两个启动规范 (共三个) 指定最高价。队列请求的最高价用于未指定最高价的任何启动规范。Spot 队列使用价格最低的实例类型启动实例。

可用区

{ "SpotPrice": "1.00", "TargetCapacity": 30, "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role", "LaunchSpecifications": [ { "ImageId": "ami-1a2b3c4d", "InstanceType": "c3.2xlarge", "Placement": { "AvailabilityZone": "us-west-2b" }, "SpotPrice": "0.10" }, { "ImageId": "ami-1a2b3c4d", "InstanceType": "c3.4xlarge", "Placement": { "AvailabilityZone": "us-west-2b" }, "SpotPrice": "0.20" }, { "ImageId": "ami-1a2b3c4d", "InstanceType": "c3.8xlarge", "Placement": { "AvailabilityZone": "us-west-2b" } } ] }

子网

{ "SpotPrice": "1.00", "TargetCapacity": 30, "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role", "LaunchSpecifications": [ { "ImageId": "ami-1a2b3c4d", "InstanceType": "c3.2xlarge", "SubnetId": "subnet-1a2b3c4d", "SpotPrice": "0.10" }, { "ImageId": "ami-1a2b3c4d", "InstanceType": "c3.4xlarge", "SubnetId": "subnet-1a2b3c4d", "SpotPrice": "0.20" }, { "ImageId": "ami-1a2b3c4d", "InstanceType": "c3.8xlarge", "SubnetId": "subnet-1a2b3c4d" } ] }

示例 5:使用多样化分配策略启动 Spot 队列

以下示例使用 diversified 分配策略。启动规范具有不同的实例类型,但具有相同的 AMI 和可用区或子网。Spot 队列在 3 个启动规范间分配 30 个实例,以便每种类型有 10 个实例。有关更多信息,请参阅Spot 实例分配策略

可用区

{ "SpotPrice": "0.70", "TargetCapacity": 30, "AllocationStrategy": "diversified", "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role", "LaunchSpecifications": [ { "ImageId": "ami-1a2b3c4d", "InstanceType": "c4.2xlarge", "Placement": { "AvailabilityZone": "us-west-2b" } }, { "ImageId": "ami-1a2b3c4d", "InstanceType": "m3.2xlarge", "Placement": { "AvailabilityZone": "us-west-2b" } }, { "ImageId": "ami-1a2b3c4d", "InstanceType": "r3.2xlarge", "Placement": { "AvailabilityZone": "us-west-2b" } } ] }

子网

{ "SpotPrice": "0.70", "TargetCapacity": 30, "AllocationStrategy": "diversified", "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role", "LaunchSpecifications": [ { "ImageId": "ami-1a2b3c4d", "InstanceType": "c4.2xlarge", "SubnetId": "subnet-1a2b3c4d" }, { "ImageId": "ami-1a2b3c4d", "InstanceType": "m3.2xlarge", "SubnetId": "subnet-1a2b3c4d" }, { "ImageId": "ami-1a2b3c4d", "InstanceType": "r3.2xlarge", "SubnetId": "subnet-1a2b3c4d" } ] }

在其中一个可用区中断的情况下,增加 EC2 容量可以满足 Spot 请求的几率的最佳实践是跨区域实现多样化。对于这种情况,请在启动规范中包含每个对您可用的可用区。并且,不是每次使用同一个子网,而是使用三个唯一的子网(每个子网映射到不同的区域)。

可用区

{ "SpotPrice": "0.70", "TargetCapacity": 30, "AllocationStrategy": "diversified", "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role", "LaunchSpecifications": [ { "ImageId": "ami-1a2b3c4d", "InstanceType": "c4.2xlarge", "Placement": { "AvailabilityZone": "us-west-2a" } }, { "ImageId": "ami-1a2b3c4d", "InstanceType": "m3.2xlarge", "Placement": { "AvailabilityZone": "us-west-2b" } }, { "ImageId": "ami-1a2b3c4d", "InstanceType": "r3.2xlarge", "Placement": { "AvailabilityZone": "us-west-2c" } } ] }

子网

{ "SpotPrice": "0.70", "TargetCapacity": 30, "AllocationStrategy": "diversified", "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role", "LaunchSpecifications": [ { "ImageId": "ami-1a2b3c4d", "InstanceType": "c4.2xlarge", "SubnetId": "subnet-1a2b3c4d" }, { "ImageId": "ami-1a2b3c4d", "InstanceType": "m3.2xlarge", "SubnetId": "subnet-2a2b3c4d" }, { "ImageId": "ami-1a2b3c4d", "InstanceType": "r3.2xlarge", "SubnetId": "subnet-3a2b3c4d" } ] }

示例 6:使用实例权重启动 Spot 队列

以下示例使用实例权重,这意味着价格是每单位小时价格,而不是每实例小时价格。每个启动配置列出不同的实例类型和不同的权重。Spot 队列选择每单位小时价格最低的实例类型。Spot 队列通过将目标容量除以实例权重,计算出要启动的 Spot 实例数。如果结果不是整数,则 Spot 队列会将其向上舍入到下一个整数,以便队列的大小不低于其目标容量。

如果 r3.2xlarge 请求成功,Spot 将预置其中的 4 个实例。将 20 除以 6 可得到总共 3.33 个实例,然后向上舍入为 4 个实例。

如果 c3.xlarge 请求成功,Spot 将预置其中的 7 个实例。将 20 除以 3 可得到总共 6.66 个实例,然后向上舍入为 7 个实例。

有关更多信息,请参阅Spot 队列实例权重

可用区

{ "SpotPrice": "0.70", "TargetCapacity": 20, "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role", "LaunchSpecifications": [ { "ImageId": "ami-1a2b3c4d", "InstanceType": "r3.2xlarge", "Placement": { "AvailabilityZone": "us-west-2b" }, "WeightedCapacity": 6 }, { "ImageId": "ami-1a2b3c4d", "InstanceType": "c3.xlarge", "Placement": { "AvailabilityZone": "us-west-2b" }, "WeightedCapacity": 3 } ] }

子网

{ "SpotPrice": "0.70", "TargetCapacity": 20, "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role", "LaunchSpecifications": [ { "ImageId": "ami-1a2b3c4d", "InstanceType": "r3.2xlarge", "SubnetId": "subnet-1a2b3c4d", "WeightedCapacity": 6 }, { "ImageId": "ami-1a2b3c4d", "InstanceType": "c3.xlarge", "SubnetId": "subnet-1a2b3c4d", "WeightedCapacity": 3 } ] }

示例 7:启动具有按需容量的 Spot 队列

为确保始终拥有实例容量,您可以在 Spot 队列请求中包含按需容量请求。如果具有容量,则将始终执行按需请求。目标容量的余量将在具有容量且可用的情况下作为 Spot 容量执行。

以下示例将所需的目标容量指定为 10,其中 5 个必须为按需容量。未指定 Spot 容量;它由目标容量减去按需容量的余量隐含指定。Amazon EC2 启动 5 个容量单位作为按需容量,并在有可用的 Amazon EC2 容量并且可用时,启动 5 个容量单位 (10-5=5) 作为 Spot 容量。

有关更多信息,请参阅Spot 队列中的按需实例

{ "IamFleetRole": "arn:aws:iam::781603563322:role/aws-ec2-spot-fleet-tagging-role", "AllocationStrategy": "lowestPrice", "TargetCapacity": 10, "SpotPrice": null, "ValidFrom": "2018-04-04T15:58:13Z", "ValidUntil": "2019-04-04T15:58:13Z", "TerminateInstancesWithExpiration": true, "LaunchSpecifications": [], "Type": "maintain", "OnDemandTargetCapacity": 5, "LaunchTemplateConfigs": [ { "LaunchTemplateSpecification": { "LaunchTemplateId": "lt-0dbb04d4a6cca5ad1", "Version": "2" }, "Overrides": [ { "InstanceType": "t2.medium", "WeightedCapacity": 1, "SubnetId": "subnet-d0dc51fb" } ] } ] }

示例 8:配置容量再平衡以启动替换 Spot 实例

下面的示例将 Spot 队列配置为在 Amazon EC2 为队列中的 Spot 实例发出再平衡建议时启动替换 Spot 实例。要配置 Spot 实例 的自动替换,对于 ReplacementStrategy,请指定 launch

注意

启动替换实例时,标记为再平衡的实例不会自动终止。您可以终止它,也可以让它保持运行。在实例运行期间,您需要为它们付费。

容量再平衡策略的有效性取决于 Spot 队列请求中指定的 Spot 容量池的数量。我们建议您使用一组多样化的实例类型和可用区配置队列,对于 AllocationStrategy,请指定 capacityOptimized。有关为容量再平衡配置 Spot 队列时应考虑的内容的更多信息,请参阅 容量再平衡

{ "SpotFleetRequestConfig": { "AllocationStrategy": "capacityOptimized", "IamFleetRole": "arn:aws:iam::000000000000:role/aws-ec2-spot-fleet-tagging-role", "LaunchTemplateConfigs": [ { "LaunchTemplateSpecification": { "LaunchTemplateName": "LaunchTemplate", "Version": "1" }, "Overrides": [ { "InstanceType": "c3.large", "WeightedCapacity": 1, "Placement": { "AvailabilityZone": "us-east-1a" } }, { "InstanceType": "c4.large", "WeightedCapacity": 1, "Placement": { "AvailabilityZone": "us-east-1a" } }, { "InstanceType": "c5.large", "WeightedCapacity": 1, "Placement": { "AvailabilityZone": "us-east-1a" } } ] } ], "TargetCapacity": 5, "SpotMaintenanceStrategies": { "CapacityRebalance": { "ReplacementStrategy": "launch" } } } }

示例 9:在容量优化的队列中启动 Spot 实例

以下示例演示如何使用针对容量进行优化的 Spot 分配策略配置 Spot 队列。要优化容量,您必须将 AllocationStrategy 设置为 capacityOptimized

在以下示例中,三个启动规范指定了三个 Spot 容量池。目标容量为 50 个 Spot 实例。Spot 队列尝试在 Spot 容量池中启动 50 个 Spot 实例,并为启动的实例数量提供最佳容量。

{ "TargetCapacity": "50", "SpotFleetRequestConfig": { "AllocationStrategy": "capacityOptimized", }, "LaunchTemplateConfigs": [ { "LaunchTemplateSpecification": { "LaunchTemplateName": "my-launch-template", "Version": "1" }, "Overrides": [ { "InstanceType": "r4.2xlarge", "AvailabilityZone": "us-west-2a" }, { "InstanceType": "m4.2xlarge", "AvailabilityZone": us-west-2b }, { "InstanceType": "c5.2xlarge", "AvailabilityZone": us-west-2b } ] } ] }

示例 10:在具有优先级的容量优化队列中启动 Spot 实例

以下示例演示如何通过 Spot 分配策略配置 Spot 队列,该策略针对容量进行优化,同时尽最大努力使用优先级。

使用 capacityOptimizedPrioritized 分配策略时,您可以使用 Priority 参数指定 Spot 容量池的优先级,数字越小优先级越高。如果您平等地支持多个 Spot 容量池,您还可以为它们设置相同的优先级。如果您没有为某个池设置优先级,则该池将在优先级方面被视为最后一个。

要设置 Spot 容量池的优先级,您必须将 AllocationStrategy 设置为 capacityOptimizedPrioritized。Spot 队列首先会针对容量进行优化,但会尽最大努力遵循优先级。(例如,如果遵循优先级不会显著影响 Spot 队列预置最佳容量的能力)。对于必须最大限度地减少中断可能性,同时对某些实例类型的偏好也很重要的工作负载来说,这是一个不错的选择。

在以下示例中,三个启动规范指定了三个 Spot 容量池。对每个池进行优先级排序,数字越小优先级越高。目标容量为 50 个 Spot 实例。Spot 队列尝试以最高优先级在 Spot 容量池中启动 50 个 Spot 实例,但首先针对容量进行优化。

{ "TargetCapacity": "50", "SpotFleetRequestConfig": { "AllocationStrategy": "capacityOptimizedPrioritized" }, "LaunchTemplateConfigs": [ { "LaunchTemplateSpecification": { "LaunchTemplateName": "my-launch-template", "Version": "1" }, "Overrides": [ { "InstanceType": "r4.2xlarge", "Priority": 1 "AvailabilityZone": "us-west-2a" }, { "InstanceType": "m4.2xlarge", "Priority": 2 "AvailabilityZone": us-west-2b }, { "InstanceType": "c5.2xlarge", "Priority": 3 "AvailabilityZone": us-west-2b } ] } ] }