本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用 Amazon CLI创建分步扩缩策略
您可以使用执行以下配置任务,为 Application Auto Scaling 创建步进扩展策略。 Amazon CLI
-
注册可扩展目标。
-
在可扩展目标上添加分步扩缩策略。
-
为策略创建 CloudWatch 警报。
为简洁起见,本主题中的示例说明了 Amazon ECS 服务的 CLI 命令。要指定不同的可扩展目标,请在 --service-namespace
中指定其命名空间,在 --scalable-dimension
中指定其可扩展维度,并在 --resource-id
中指定其资源 ID。有关每项服务的更多信息和示例,请参阅 Amazon 可以与 Application Auto Scaling 一起使用的服务 中的主题。
使用时 Amazon CLI,请记住您的命令在 Amazon Web Services 区域 配置文件中运行。如果您想要在不同的区域中运行命令,可以为配置文件更改默认区域,或者与命令一起使用 --region
参数。
注册可扩展目标
如果您尚未注册,请注册可扩展目标。使用register-scalable-target命令将目标服务中的特定资源注册为可扩展目标。以下示例使用 Application Auto Scaling 注册 Amazon ECS 服务。Application Auto Scaling 可扩展任务的数量,最少 2 个任务,最多 10 个任务。将每个用户输入占位符
替换为您自己的信息。
Linux、macOS 或 Unix
aws application-autoscaling register-scalable-target --service-namespace
ecs
\ --scalable-dimensionecs:service:DesiredCount
\ --resource-idservice/my-cluster/my-service
\ --min-capacity2
--max-capacity10
Windows
aws application-autoscaling register-scalable-target --service-namespace
ecs
--scalable-dimensionecs:service:DesiredCount
--resource-idservice/my-cluster/my-service
--min-capacity2
--max-capacity10
如果成功,该命令会返回可扩展目标的 ARN。
{
"ScalableTargetARN": "arn:aws:application-autoscaling:region
:account-id
:scalable-target/1234abcd56ab78cd901ef1234567890ab123"
}
创建分步扩缩策略
要为可扩展目标创建分步扩展策略,您可以使用以下示例来帮助您入门。
创建调用扩缩策略的警报
最后,使用以下 CloudWatch put-metric-alarm命令创建警报,以便与步进缩放策略一起使用。在本示例中,您将根据平均 CPU 利用率发出警报。如果警报在至少两个连续 60 秒的评估期间达到 70% 的阈值,则它将被配置为处于 ALARM 状态。要指定其他 CloudWatch 指标或使用您自己的自定义指标,请在中指定其名称,在中--metric-name
指定其命名空间--namespace
。
Linux、macOS 或 Unix
aws cloudwatch put-metric-alarm --alarm-name
Step-Scaling-AlarmHigh-ECS:service/my-cluster/my-service
\ --metric-nameCPUUtilization
--namespaceAWS/ECS
--statistic Average \ --period60
--evaluation-periods2
--threshold70
\ --comparison-operator GreaterThanOrEqualToThreshold \ --dimensions Name=ClusterName,Value=default
Name=ServiceName,Value=sample-app-service
\ --alarm-actionsPolicyARN
Windows
aws cloudwatch put-metric-alarm --alarm-name
Step-Scaling-AlarmHigh-ECS:service/my-cluster/my-service
--metric-nameCPUUtilization
--namespaceAWS/ECS
--statistic Average --period60
--evaluation-periods2
--threshold70
--comparison-operator GreaterThanOrEqualToThreshold --dimensions Name=ClusterName,Value=default
Name=ServiceName,Value=sample-app-service
--alarm-actionsPolicyARN
描述分步扩缩策略
您可使用以下 describe-scaling-policies 命令描述指定服务命名空间的所有扩展策略。
aws application-autoscaling describe-scaling-policies --service-namespace
ecs
您可以使用 --query
参数将结果筛选为仅步进扩展策略。有关 query
的语法的更多信息,请参阅 Amazon Command Line Interface 用户指南中的控制 Amazon CLI的命令输出。
Linux、macOS 或 Unix
aws application-autoscaling describe-scaling-policies --service-namespace
ecs
\ --query 'ScalingPolicies[?PolicyType==`StepScaling`]'
Windows
aws application-autoscaling describe-scaling-policies --service-namespace
ecs
--query "ScalingPolicies[?PolicyType==`StepScaling`]"
下面是示例输出。
[
{
"PolicyARN": "PolicyARN",
"StepScalingPolicyConfiguration": {
"MetricAggregationType": "Average",
"Cooldown": 60,
"StepAdjustments": [
{
"MetricIntervalLowerBound": 0.0,
"MetricIntervalUpperBound": 15.0,
"ScalingAdjustment": 1
},
{
"MetricIntervalLowerBound": 15.0,
"MetricIntervalUpperBound": 25.0,
"ScalingAdjustment": 2
},
{
"MetricIntervalLowerBound": 25.0,
"ScalingAdjustment": 3
}
],
"AdjustmentType": "ChangeInCapacity"
},
"PolicyType": "StepScaling",
"ResourceId": "service/my-cluster/my-service",
"ServiceNamespace": "ecs",
"Alarms": [
{
"AlarmName": "Step-Scaling-AlarmHigh-ECS:service/my-cluster/my-service",
"AlarmARN": "arn:aws:cloudwatch:region:012345678910:alarm:Step-Scaling-AlarmHigh-ECS:service/my-cluster/my-service"
}
],
"PolicyName": "my-step-scaling-policy",
"ScalableDimension": "ecs:service:DesiredCount",
"CreationTime": 1515024099.901
}
]
删除分步扩缩策略
当您不再需要某个步进扩展策略时,可将其删除。要同时删除扩展策略和 CloudWatch 警报,请完成以下任务。
删除您的扩展策略
使用以下 delete-scaling-policy 命令。
Linux、macOS 或 Unix
aws application-autoscaling delete-scaling-policy --service-namespace
ecs
\ --scalable-dimensionecs:service:DesiredCount
\ --resource-idservice/my-cluster/my-service
\ --policy-namemy-step-scaling-policy
Windows
aws application-autoscaling delete-scaling-policy --service-namespace
ecs
--scalable-dimensionecs:service:DesiredCount
--resource-idservice/my-cluster/my-service
--policy-namemy-step-scaling-policy
删除 CloudWatch 警报
使用 delete-alarms 命令。您可以一次删除一个或多个警报。例如,使用以下命令可删除 Step-Scaling-AlarmHigh-ECS:service/my-cluster/my-service
和 Step-Scaling-AlarmLow-ECS:service/my-cluster/my-service
警报:
aws cloudwatch delete-alarms --alarm-name
Step-Scaling-AlarmHigh-ECS:service/my-cluster/my-service
Step-Scaling-AlarmLow-ECS:service/my-cluster/my-service