使用 AWS CLI适用于 Amazon AppStream 2.0 的管理舰队扩展 - 亚马逊 AppStream 2.0

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 AWS CLI适用于 Amazon AppStream 2.0 的管理舰队扩展

您可以使用 AWS Command Line Interface (AWS CLI) 设置和管理队列扩展。要获得更高级的功能,例如设置多个扩展策略或设置缩小和横向扩展冷却时间,请使用。 AWS CLI在运行扩展策略命令之前,您必须将实例集注册为可扩展目标。为此,请使用以下register-scalable-target命令:

aws application-autoscaling register-scalable-target --service-namespace appstream \ --resource-id fleet/fleetname \ --scalable-dimension appstream:fleet:DesiredCapacity \ --min-capacity 1 --max-capacity 5

示例 1:根据容量利用率应用扩展策略

此 AWS CLI示例设置了一个扩展策略,如果利用率 >= 75%,则将队列扩展 25%。

以下put-scaling-policy命令定义了基于利用率的扩展策略:

aws application-autoscaling put-scaling-policy --cli-input-json file://scale-out-utilization.json

文件 scale-out-utilization.json 的内容如下所示:

{ "PolicyName": "policyname", "ServiceNamespace": "appstream", "ResourceId": "fleet/fleetname", "ScalableDimension": "appstream:fleet:DesiredCapacity", "PolicyType": "StepScaling", "StepScalingPolicyConfiguration": { "AdjustmentType": "PercentChangeInCapacity", "StepAdjustments": [ { "MetricIntervalLowerBound": 0, "ScalingAdjustment": 25 } ], "Cooldown": 120 } }

如果命令成功,则输出类似于以下内容,但一些详细信息是您的账户和区域独有的。在本示例中,策略标识符为 e3425d21-16f0-d701-89fb-12f98dac64af

{"PolicyARN": "arn:aws:autoscaling:us-west-2:123456789012:scalingPolicy:e3425d21-16f0-d701-89fb-12f98dac64af:resource/appstream/fleet/SampleFleetName:policyName/scale-out-utilization-policy"}

现在,为该政策设置 CloudWatch 警报。使用适用于您的名称、区域、账号和策略标识符。您可以使用上一个命令ARN返回的策略作为--alarm-actions参数。

aws cloudwatch put-metric-alarm --alarm-name alarmname \ --alarm-description "Alarm when Capacity Utilization exceeds 75 percent" \ --metric-name CapacityUtilization \ --namespace AWS/AppStream \ --statistic Average \ --period 300 \ --threshold 75 \ --comparison-operator GreaterThanOrEqualToThreshold \ --dimensions "Name=Fleet,Value=fleetname" \ --evaluation-periods 1 --unit Percent \ --alarm-actions "arn:aws:autoscaling:your-region-code:account-number-without-hyphens:scalingPolicy:policyid:resource/appstream/fleet/fleetname:policyName/policyname"

示例 2:根据“容量不足”错误应用扩展策略

此 AWS CLI示例设置了一个扩展策略,如果队列返回InsufficientCapacityError错误,则该策略会将队列扩展 1。

以下命令定义一个基于“容量不足”的扩展策略:

aws application-autoscaling put-scaling-policy --cli-input-json file://scale-out-capacity.json

文件 scale-out-capacity.json 的内容如下所示:

{ "PolicyName": "policyname", "ServiceNamespace": "appstream", "ResourceId": "fleet/fleetname", "ScalableDimension": "appstream:fleet:DesiredCapacity", "PolicyType": "StepScaling", "StepScalingPolicyConfiguration": { "AdjustmentType": "ChangeInCapacity", "StepAdjustments": [ { "MetricIntervalLowerBound": 0, "ScalingAdjustment": 1 } ], "Cooldown": 120 } }

如果命令成功,则输出类似于以下内容,但一些详细信息是您的账户和区域独有的。在本示例中,策略标识符为 f4495f21-0650-470c-88e6-0f393adb64fc

{"PolicyARN": "arn:aws:autoscaling:us-west-2:123456789012:scalingPolicy:f4495f21-0650-470c-88e6-0f393adb64fc:resource/appstream/fleet/SampleFleetName:policyName/scale-out-insufficient-capacity-policy"}

现在,为该政策设置 CloudWatch 警报。使用适用于您的名称、区域、账号和策略标识符。您可以使用上一个命令ARN返回的策略作为--alarm-actions参数。

aws cloudwatch put-metric-alarm --alarm-name alarmname \ --alarm-description "Alarm when out of capacity is > 0" \ --metric-name InsufficientCapacityError \ --namespace AWS/AppStream \ --statistic Maximum \ --period 300 \ --threshold 0 \ --comparison-operator GreaterThanThreshold \ --dimensions "Name=Fleet,Value=fleetname" \ --evaluation-periods 1 --unit Count \ --alarm-actions "arn:aws:autoscaling:your-region-code:account-number-without-hyphens:scalingPolicy:policyid:resource/appstream/fleet/fleetname:policyName/policyname"

示例 3:根据低容量利用率应用扩展策略

此 AWS CLI 示例设置了一个扩展策略,该策略可以在队列中进行扩展,以便在实际容量CapacityUtilization较低时减少实际容量。

以下命令定义一个基于“容量过多”的扩展策略:

aws application-autoscaling put-scaling-policy --cli-input-json file://scale-in-capacity.json

文件 scale-in-capacity.json 的内容如下所示:

{ "PolicyName": "policyname", "ServiceNamespace": "appstream", "ResourceId": "fleet/fleetname", "ScalableDimension": "appstream:fleet:DesiredCapacity", "PolicyType": "StepScaling", "StepScalingPolicyConfiguration": { "AdjustmentType": "PercentChangeInCapacity", "StepAdjustments": [ { "MetricIntervalUpperBound": 0, "ScalingAdjustment": -25 } ], "Cooldown": 360 } }

如果命令成功,则输出类似于以下内容,但一些详细信息是您的账户和区域独有的。在本示例中,策略标识符为 12ab3c4d-56789-0ef1-2345-6ghi7jk8lm90

{"PolicyARN": "arn:aws:autoscaling:us-west-2:123456789012:scalingPolicy:12ab3c4d-56789-0ef1-2345-6ghi7jk8lm90:resource/appstream/fleet/SampleFleetName:policyName/scale-in-utilization-policy"}

现在,为该政策设置 CloudWatch 警报。使用适用于您的名称、区域、账号和策略标识符。您可以使用上一个命令ARN返回的策略作为--alarm-actions参数。

aws cloudwatch put-metric-alarm --alarm-name alarmname \ --alarm-description "Alarm when Capacity Utilization is less than or equal to 25 percent" \ --metric-name CapacityUtilization \ --namespace AWS/AppStream \ --statistic Average \ --period 120 \ --threshold 25 \ --comparison-operator LessThanOrEqualToThreshold \ --dimensions "Name=Fleet,Value=fleetname" \ --evaluation-periods 10 --unit Percent \ --alarm-actions "arn:aws:autoscaling:your-region-code:account-number-without-hyphens:scalingPolicy:policyid:resource/appstream/fleet/fleetname:policyName/policyname"

示例 4:基于计划更改实例集容量

通过基于计划更改实例集容量,您能够扩展实例集容量以响应可预测的需求变化。例如,在工作日的开始,您可能预期有特定数量的用户一次性请求流式传输连接。要根据计划更改队列容量,可以使用 Application Auto Scaling PutScheduledActionAPI操作或put-scheduled-action AWS CLI命令。

在更改舰队容量之前,您可以使用 AppStream 2.0 desc ribe- AWS CLI fleets 命令列出当前的舰队容量。

aws appstream describe-fleets --name fleetname

当前机队容量将类似于以下输出(以JSON格式显示):

{ { "ComputeCapacityStatus": { "Available": 1, "Desired": 1, "Running": 1, "InUse": 0 }, }

然后,使用 put-scheduled-action 命令创建计划操作以更改实例集容量。例如,以下命令每天上午 9:00 将最小容量更改为 3,最大容量更改为 5 UTC。

注意

对于 cron 表达式,请指定何时在中执行操作。UTC有关更多信息,请参阅 Cron 表达式

aws application-autoscaling put-scheduled-action --service-namespace appstream \ --resource-id fleet/fleetname \ --schedule="cron(0 9 * * ? *)" \ --scalable-target-action MinCapacity=3,MaxCapacity=5 \ --scheduled-action-name ExampleScheduledAction \ --scalable-dimension appstream:fleet:DesiredCapacity

要确认更改舰队容量的计划操作已成功创建,请运行该describe-scheduled-actions命令。

aws application-autoscaling describe-scheduled-actions --service-namespace appstream --resource-id fleet/fleetname

如果已成功创建计划的操作,则输出类似于以下内容。

{ "ScheduledActions": [ { "ScalableDimension": "appstream:fleet:DesiredCapacity", "Schedule": "cron(0 9 * * ? *)", "ResourceId": "fleet/ExampleFleet", "CreationTime": 1518651232.886, "ScheduledActionARN": "<arn>", "ScalableTargetAction": { "MinCapacity": 3, "MaxCapacity": 5 }, "ScheduledActionName": "ExampleScheduledAction", "ServiceNamespace": "appstream" } ] }

有关更多信息,请参阅《Application Auto Scaling 用户指南》中的计划扩展

示例 5:应用目标跟踪扩展策略

通过目标跟踪扩展,您可以为实例集指定容量利用率级别。

创建目标跟踪扩展策略时,Application Auto Scaling 会自动创建和管理触发扩展策略的 CloudWatch 警报。扩展策略根据需要增加或减少容量,将容量利用率保持在指定的目标值或接近指定的目标值。为了确保应用程序可用性,实例集针对此指标尽快按比例向外扩展,但会逐渐向内扩展。

以下put-scaling-policy命令定义了目标跟踪扩展策略,该策略尝试保持 AppStream 2.0 队列的 75% 容量利用率。

aws application-autoscaling put-scaling-policy --cli-input-json file://config.json

文件 config.json 的内容如下所示:

{ "PolicyName":"target-tracking-scaling-policy", "ServiceNamespace":"appstream", "ResourceId":"fleet/fleetname", "ScalableDimension":"appstream:fleet:DesiredCapacity", "PolicyType":"TargetTrackingScaling", "TargetTrackingScalingPolicyConfiguration":{ "TargetValue":75.0, "PredefinedMetricSpecification":{ "PredefinedMetricType":"AppStreamAverageCapacityUtilization" }, "ScaleOutCooldown":300, "ScaleInCooldown":300 } }

如果命令成功,则输出类似于以下内容,但一些详细信息是您的账户和区域独有的。在此示例中,策略标识符是 6d8972f3-efc8-437c-92d1-6270f29a66e7。

{ "PolicyARN": "arn:aws:autoscaling:us-west-2:123456789012:scalingPolicy:6d8972f3-efc8-437c-92d1-6270f29a66e7:resource/appstream/fleet/fleetname:policyName/target-tracking-scaling-policy", "Alarms": [ { "AlarmARN": "arn:aws:cloudwatch:us-west-2:123456789012:alarm:TargetTracking-fleet/fleetname-AlarmHigh-d4f0770c-b46e-434a-a60f-3b36d653feca", "AlarmName": "TargetTracking-fleet/fleetname-AlarmHigh-d4f0770c-b46e-434a-a60f-3b36d653feca" }, { "AlarmARN": "arn:aws:cloudwatch:us-west-2:123456789012:alarm:TargetTracking-fleet/fleetname-AlarmLow-1b437334-d19b-4a63-a812-6c67aaf2910d", "AlarmName": "TargetTracking-fleet/fleetname-AlarmLow-1b437334-d19b-4a63-a812-6c67aaf2910d" } ] }

有关更多信息,请参阅《Application Auto Scaling 用户指南》中的目标跟踪扩展策略