使用 Amazon CLI 创建目标跟踪扩缩策略
您可以使用 Amazon CLI 为以下配置任务创建 Application Auto Scaling 的目标跟踪扩缩策略。
Tasks
注册可扩展目标
如果您尚未注册,请注册可扩展目标。使用 register-scalable-target 命令可将目标服务中的特定资源注册为可扩展目标。以下示例使用 Application Auto Scaling 注册 Spot 实例集请求。Application Auto Scaling 可以扩展 Spot 实例集中的实例数,最少为 2 个实例,最多为 10 个实例。
Linux、macOS 或 Unix
aws application-autoscaling register-scalable-target --service-namespace
ec2
\ --scalable-dimensionec2:spot-fleet-request:TargetCapacity
\ --resource-idspot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE
\ --min-capacity2
--max-capacity10
Windows
aws application-autoscaling register-scalable-target --service-namespace
ec2
--scalable-dimensionec2:spot-fleet-request:TargetCapacity
--resource-idspot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE
--min-capacity2
--max-capacity10
为简洁起见,本主题中的示例说明了用于 Amazon EC2 Spot 实例集的 CLI 命令。要指定不同的可扩展目标,请在 --service-namespace
中指定其命名空间,在 --scalable-dimension
中指定其可扩展维度,并在 --resource-id
中指定其资源 ID。有关更多信息,请参阅 register-scalable-target。
创建目标跟踪扩缩策略
示例:目标跟踪配置文件
以下是将 CPU 平均使用率保持在 40% 的示例目标跟踪配置。将此配置保存在名为 config.json
的文件中。
{ "TargetValue": 40.0, "PredefinedMetricSpecification": { "PredefinedMetricType": "EC2SpotFleetRequestAverageCPUUtilization" } }
有关更多信息,请参阅 Application Auto Scaling API 参考中的 PredefinedMetricSpecification。
或者,您可以通过创建自定义指标规范并为 CloudWatch 的每个参数添加值来使用自定义指标进行扩缩。以下是一个示例目标跟踪配置,它将指定指标的平均利用率保持在 40%。
{ "TargetValue":40.0, "CustomizedMetricSpecification":{ "MetricName":"MyUtilizationMetric", "Namespace":"MyNamespace", "Dimensions":[ { "Name":"MyOptionalMetricDimensionName", "Value":"MyOptionalMetricDimensionValue" } ], "Statistic":"Average", "Unit":"Percent" } }
有关更多信息,请参阅 Application Auto Scaling API 参考中的 CustomizedMetricSpecification。
示例:cpu40-target-tracking-scaling-policy
使用以下 put-scaling-policy 命令以及您创建的 config.json
文件,创建一个名为 cpu40-target-tracking-scaling-policy
的扩缩策略。
Linux、macOS 或 Unix
aws application-autoscaling put-scaling-policy --service-namespace
ec2
\ --scalable-dimensionec2:spot-fleet-request:TargetCapacity
\ --resource-idspot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE
\ --policy-namecpu40-target-tracking-scaling-policy
--policy-type TargetTrackingScaling \ --target-tracking-scaling-policy-configurationfile://config.json
Windows
aws application-autoscaling put-scaling-policy --service-namespace
ec2
--scalable-dimensionec2:spot-fleet-request:TargetCapacity
--resource-idspot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE
--policy-namecpu40-target-tracking-scaling-policy
--policy-type TargetTrackingScaling --target-tracking-scaling-policy-configurationfile://config.json
如果成功,此命令将返回包含代表您创建的两个 CloudWatch 警报的 ARN 和名称。
{
"PolicyARN": "arn:aws:autoscaling:region:account-id:scalingPolicy:policy-id:resource/ec2/spot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE:policyName/cpu40-target-tracking-scaling-policy",
"Alarms": [
{
"AlarmARN": "arn:aws:cloudwatch:region:account-id:alarm:TargetTracking-spot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE-AlarmHigh-d4f0770c-b46e-434a-a60f-3b36d653feca",
"AlarmName": "TargetTracking-spot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE-AlarmHigh-d4f0770c-b46e-434a-a60f-3b36d653feca"
},
{
"AlarmARN": "arn:aws:cloudwatch:region:account-id:alarm:TargetTracking-spot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE-AlarmLow-1b437334-d19b-4a63-a812-6c67aaf2910d",
"AlarmName": "TargetTracking-spot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE-AlarmLow-1b437334-d19b-4a63-a812-6c67aaf2910d"
}
]
}
描述目标跟踪扩缩策略
您可使用以下 describe-scaling-policies 命令描述指定服务命名空间的所有扩缩策略。
aws application-autoscaling describe-scaling-policies --service-namespace
ec2
您可使用 --query
参数筛选结果以仅显示目标跟踪扩展策略。有关 query
的语法的更多信息,请参阅 Amazon Command Line Interface 用户指南中的控制 Amazon CLI 的命令输出。
Linux、macOS 或 Unix
aws application-autoscaling describe-scaling-policies --service-namespace
ec2
\ --query 'ScalingPolicies[?PolicyType==`TargetTrackingScaling`]'
Windows
aws application-autoscaling describe-scaling-policies --service-namespace
ec2
--query "ScalingPolicies[?PolicyType==`TargetTrackingScaling`]"
下面是示例输出。
[
{
"PolicyARN": "PolicyARN",
"TargetTrackingScalingPolicyConfiguration": {
"PredefinedMetricSpecification": {
"PredefinedMetricType": "EC2SpotFleetRequestAverageCPUUtilization"
},
"TargetValue": 40.0
},
"PolicyName": "cpu40-target-tracking-scaling-policy",
"ScalableDimension": "ec2:spot-fleet-request:TargetCapacity",
"ServiceNamespace": "ec2",
"PolicyType": "TargetTrackingScaling",
"ResourceId": "spot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE",
"Alarms": [
{
"AlarmARN": "arn:aws:cloudwatch:region:account-id:alarm:TargetTracking-spot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE-AlarmHigh-d4f0770c-b46e-434a-a60f-3b36d653feca",
"AlarmName": "TargetTracking-spot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE-AlarmHigh-d4f0770c-b46e-434a-a60f-3b36d653feca"
},
{
"AlarmARN": "arn:aws:cloudwatch:region:account-id:alarm:TargetTracking-spot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE-AlarmLow-1b437334-d19b-4a63-a812-6c67aaf2910d",
"AlarmName": "TargetTracking-spot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE-AlarmLow-1b437334-d19b-4a63-a812-6c67aaf2910d"
}
],
"CreationTime": 1515021724.807
}
]
删除目标跟踪扩缩策略
在使用完目标跟踪扩缩策略后,您可以使用 delete-scaling-policy 命令删除该策略。
以下命令将删除指定 Spot 队组请求的目标跟踪扩展策略。它还将删除 Application Auto Scaling 代表您创建的 CloudWatch 警报。
Linux、macOS 或 Unix
aws application-autoscaling delete-scaling-policy --service-namespace
ec2
\ --scalable-dimensionec2:spot-fleet-request:TargetCapacity
\ --resource-idspot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE
\ --policy-namecpu40-target-tracking-scaling-policy
Windows
aws application-autoscaling delete-scaling-policy --service-namespace
ec2
--scalable-dimensionec2:spot-fleet-request:TargetCapacity
--resource-idspot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE
--policy-namecpu40-target-tracking-scaling-policy