gpt4 book ai didi

amazon-web-services - 如何在模板中指定没有像 Web 界面之类的首选项的子网?

转载 作者:行者123 更新时间:2023-12-03 07:14:38 25 4
gpt4 key购买 nike

使用控制台创建竞价型实例时,并不强制选择子网。我可以...

Subnet: No preference (default subnet in any Availability Zone)

但是,在使用 cloudformation 模板创建现货实例时,我需要选择一个子网,但我不确定选择哪个子网可以获得最优惠的价格。这是否意味着使用 Web 界面比 cloudformation 模板(针对现货实例)更好?

最佳答案

需求及相应的分配策略

我将您的“最佳价格”要求解释为获取配置用于成本优化的 SpotFleet [1]。 AWS 在文档 [2] 中描述了如何创建成本优化的现货队列:

To optimize the costs for your use of Spot Instances, specify the lowestPrice allocation strategy so that Spot Fleet automatically deploys the cheapest combination of instance types and Availability Zones based on the current Spot price.

您已经在上面评论中提供的 CloudFormation 模板中为这种情况指定了正确的AllocationStrategy lowestPrice。 AWS 将这种分配策略描述为:

The Spot Instances come from the pool with the lowest price. This is the default strategy.

什么是现货实例池?

A Spot Instance pool is a set of unused EC2 instances with the same instance type (for example, m5.large), operating system, Availability Zone, and network platform [i.e. EC2-Classic or EC2-VPC, see [3]].

The Spot Fleet selects the Spot Instance pools that are used to fulfill the request, based on the launch specifications included in your Spot Fleet request, and the configuration of the Spot Fleet request [which is lowestPrice in our case]. [2]

这回答了您问题的一部分:

不,子网并不直接决定现货价格。价格是由可用区决定的,即相同实例类型、操作系统和网络平台的实例如果位于同一可用区,则始终具有相同的现货价格。由于子网无法跨越多个可用区,因此特定子网中实例的现货价格始终相同 - 即(可用区、实例类型、操作系统、网络平台)组合的价格。

自动子网和可用区选择

您在问题中声明必须选择一个子网。那不是真的。 LaunchSpecification [4] 的 NetworkInterfaces 属性是可选的。如果省略它,CloudFormation 的行为与 AWS 管理控制台完全相同。您可以在文档 [5] 的 Spot 队列示例 部分阅读相关内容:

Example 1: Launch Spot Instances Using the Lowest-Priced Availability Zone or Subnet in the Region

The following example specifies a single launch specification without an Availability Zone or subnet. The Spot Fleet launches the instances in the lowest-priced Availability Zone that has a default subnet. The price you pay does not exceed the On-Demand price.

{
"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"
}
}
]
}

如果您确实需要为您的实例分配 NetworkInterface(例如,因为您需要公共(public) IP 地址),[5] 中还列出了一个解决方案:

      {
"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"
}
}

据我所知,这正是您在 CloudFormation 模板中实现的内容。AWS 对于这种方法的说明:

The Spot service launches the instances in whichever subnet is in the lowest-priced Availability Zone.

You can't specify different subnets from the same Availability Zone in a Spot Fleet request.

这意味着,您应该为要分布实例的每个可用区域创建一个启动规范。每个启动规范应因 SubnetId 的不同而不同:

[...]
LaunchSpecifications: [
{
[...]
"NetworkInterfaces": [
{
"DeviceIndex": 0,
"SubnetId": "subnet-1a2b3c4d", // choose a subnet from AZ a
"Groups": [ "sg-1a2b3c4d" ],
"AssociatePublicIpAddress": true
}
],
[...]
},
{
[...]
"NetworkInterfaces": [
{
"DeviceIndex": 0,
"SubnetId": "subnet-xxxxxxxx", // choose a subnet from AZ b
"Groups": [ "sg-1a2b3c4d" ],
"AssociatePublicIpAddress": true
}
],
[...]
},
[... etc. for each AZ]
],
[...]

引用文献

[1] https://docs.aws.amazon.com/de_de/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-spotfleet.html
[2] https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-fleet.html#spot-fleet-allocation-strategy
[3] https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-classic-platform.html
[4] https://docs.aws.amazon.com/de_de/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-spotfleet-spotfleetrequestconfigdata-launchspecifications.html#cfn-ec2-spotfleet-spotfleetrequestconfigdata-launchspecifications-networkinterfaces
[5] https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-fleet-examples.html#fleet-config1

关于amazon-web-services - 如何在模板中指定没有像 Web 界面之类的首选项的子网?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56205671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com