

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

# 教程：配置自动扩缩以处理繁重的工作负载
<a name="application-auto-scaling-tutorial"></a>

在本教程中，您将了解当应用程序的工作负载比正常工作负载重的情况下，如何根据时间范围进行横向扩展和横向缩减。当您的应用程序可能会突然有大量定期或季节性的访问者时，这会很有帮助。

您可以将目标跟踪扩缩策略与计划的扩缩一起使用来处理额外负载。计划的扩缩会根据您指定的计划代表您自动启动对 `MinCapacity` 和 `MaxCapacity` 的更改。当目标跟踪扩缩策略在资源上处于活动状态时，它可以根据当前资源利用率在新的最小容量和最大容量范围内动态扩展。

完成本教程后，您将了解如何：
+ 使用计划的扩缩添加额外容量，以在达到限值之前满足重负载，然后在不再需要时删除额外容量。
+ 使用目标跟踪扩缩策略根据当前资源利用率扩展您的应用程序。

**Topics**
+ [前提条件](#tutorial-prerequisites)
+ [步骤 1：注册您的可扩展目标](#tutorial-register-scalable-target)
+ [步骤 2：根据您的要求设置计划的操作](#tutorial-create-scheduled-actions)
+ [步骤 3：添加目标跟踪扩缩策略](#tutorial-create-target-tracking-policy)
+ [步骤 4：后续步骤](#tutorial-next-steps)
+ [第 5 步：清理](#tutorial-scaling-clean-up)

## 前提条件
<a name="tutorial-prerequisites"></a>

本教程假定您已执行以下操作：
+ 创建了一个 Amazon Web Services 账户.
+ 已安装并配置 Amazon CLI。
+ 授予了使用 Application Auto Scaling 将资源注册和取消注册为可扩展目标的必要权限。此外，还授予了创建扩展策略和计划操作的必要权限。有关更多信息，请参阅 [适用于 Application Auto Scaling 的 Identity and Access Management](auth-and-access-control.md)。
+ 创建了在非生产环境中可用于本教程的受支持资源。如果您还没有，请立即创建一个。有关使用 Application Auto Scaling 的 Amazon 服务和资源的信息，请参阅 [Amazon Web Services 服务 可以与 Application Auto Scaling 一起使用](integrated-services-list.md) 部分。

**注意**  
完成本教程时有两个步骤，您可在这两个步骤中将最大和最小容量值设置为 0，以将当前容量重置为 0。根据您当前通过 Application Auto Scaling 使用的资源，您可能无法在这些步骤中将当前容量设置为 0。为了帮助您解决该问题，输出中将显示一条消息，指示最小容量不能小于指定值，并将提供 Amazon 资源可以接受的最小容量值。

## 步骤 1：注册您的可扩展目标
<a name="tutorial-register-scalable-target"></a>

首先使用 Application Auto Scaling 将您的资源注册为可扩展目标。可扩展目标是 Application Auto Scaling 可以横向扩展或横向缩减的资源。

**向 Application Auto Scaling 注册您的可扩展目标**
+ 使用以下 [register-scalable-target](https://docs.amazonaws.cn/cli/latest/reference/application-autoscaling/register-scalable-target.html) 命令注册新的可扩展目标。将 `--min-capacity` 和 `--max-capacity` 值设置为 0 以将当前容量重置为 0。

  将 `--service-namespace` 中的示例文本替换为您通过 Application Auto Scaling 使用的 Amazon 服务的命名空间，将 `--scalable-dimension` 替换为与您注册的资源关联的可扩展维度，并将 `--resource-id` 替换为资源的标识符。这些值因使用的资源以及资源 ID 的构造方式而异。有关更多信息，请参阅 [Amazon Web Services 服务 可以与 Application Auto Scaling 一起使用](integrated-services-list.md) 部分中的主题。这些主题包括向您显示如何使用 Application Auto Scaling 注册可扩展目标的示例命令。

  **Linux、macOS 或 Unix**

  ```
  aws application-autoscaling register-scalable-target \
    --service-namespace {{namespace}} \
    --scalable-dimension {{dimension}} \
    --resource-id {{identifier}} \
    --min-capacity 0 --max-capacity 0
  ```

  **Windows**

  ```
  aws application-autoscaling register-scalable-target --service-namespace {{namespace}} --scalable-dimension {{dimension}} --resource-id {{identifier}} --min-capacity 0 --max-capacity 0
  ```

  如果成功，该命令会返回可扩展目标的 ARN。

  ```
  {
      "ScalableTargetARN": "arn:aws:application-autoscaling:{{region}}:{{account-id}}:scalable-target/1234abcd56ab78cd901ef1234567890ab123"
  }
  ```

## 步骤 2：根据您的要求设置计划的操作
<a name="tutorial-create-scheduled-actions"></a>

您可以使用 [put-scheduled-action](https://docs.amazonaws.cn/cli/latest/reference/application-autoscaling/put-scheduled-action.html) 命令创建配置为满足业务需求的计划操作。在本教程中，我们重点介绍一种配置，通过将容量减少到 0 来停止在工作时间以外消耗资源。

**创建在噪声横向扩展的计划操作**

1. 要横向扩展可扩展目标，请使用以下 [put-scheduled-action](https://docs.amazonaws.cn/cli/latest/reference/application-autoscaling/put-scheduled-action.html) 命令。使用 cron 表达式将 `--schedule` 参数包含在采用 UTC 时间的定期计划中。

   根据指定的计划（UTC 时间每天上午 9:00），Application Auto Scaling 会将 `MinCapacity` 和 `MaxCapacity` 值更新为 1-5 个容量单位的所需范围。

   **Linux、macOS 或 Unix**

   ```
   aws application-autoscaling put-scheduled-action \
     --service-namespace {{namespace}} \
     --scalable-dimension {{dimension}} \
     --resource-id {{identifier}} \
     --scheduled-action-name {{my-first-scheduled-action}} \
     --schedule "cron({{0 9 * * ? *}})" \
     --scalable-target-action MinCapacity={{1}},MaxCapacity={{5}}
   ```

   **Windows**

   ```
   aws application-autoscaling put-scheduled-action --service-namespace {{namespace}} --scalable-dimension {{dimension}} --resource-id {{identifier}} --scheduled-action-name {{my-first-scheduled-action}} --schedule "cron({{0 9 * * ? *}})" --scalable-target-action MinCapacity={{1}},MaxCapacity={{5}}
   ```

   如果此命令成功执行，将不会返回任何输出。

1. 要确认您的计划操作是否存在，请使用以下 [describe-scheduled-actions](https://docs.amazonaws.cn/cli/latest/reference/application-autoscaling/describe-scheduled-actions.html) 命令。

   **Linux、macOS 或 Unix**

   ```
   aws application-autoscaling describe-scheduled-actions \
     --service-namespace {{namespace}} \ 
     --query 'ScheduledActions[?ResourceId==`{{identifier}}`]'
   ```

   **Windows**

   ```
   aws application-autoscaling describe-scheduled-actions --service-namespace {{namespace}} --query "ScheduledActions[?ResourceId==`{{identifier}}`]"
   ```

   下面是示例输出。

   ```
   [
       {
           "ScheduledActionName": "my-first-scheduled-action",
           "ScheduledActionARN": "{{arn}}",
           "Schedule": "cron(0 9 * * ? *)",
           "ScalableTargetAction": {
               "MinCapacity": 1,
               "MaxCapacity": 5
           },
           ...
       }
   ]
   ```

**创建在夜间横向缩减的计划操作**

1. 重复上述过程以创建另一个计划的操作，Application Auto Scaling 使用该操作在当天结束时进行横向缩减。

   根据指定的计划（UTC 时间每天晚上 8:00），Application Auto Scaling 会将目标的 `MinCapacity` 和 `MaxCapacity` 更新为 0，如以下 [put-scheduled-action](https://docs.amazonaws.cn/cli/latest/reference/application-autoscaling/put-scheduled-action.html) 命令所示。

   **Linux、macOS 或 Unix**

   ```
   aws application-autoscaling put-scheduled-action \
     --service-namespace {{namespace}} \
     --scalable-dimension {{dimension}} \
     --resource-id {{identifier}} \
     --scheduled-action-name {{my-second-scheduled-action}} \
     --schedule "cron({{0 20 * * ? *}})" \
     --scalable-target-action MinCapacity={{0}},MaxCapacity={{0}}
   ```

   **Windows**

   ```
   aws application-autoscaling put-scheduled-action --service-namespace {{namespace}} --scalable-dimension {{dimension}} --resource-id {{identifier}} --scheduled-action-name {{my-second-scheduled-action}} --schedule "cron({{0 20 * * ? *}})" --scalable-target-action MinCapacity={{0}},MaxCapacity={{0}}
   ```

1. 要确认您的计划操作是否存在，请使用以下 [describe-scheduled-actions](https://docs.amazonaws.cn/cli/latest/reference/application-autoscaling/describe-scheduled-actions.html) 命令。

   **Linux、macOS 或 Unix**

   ```
   aws application-autoscaling describe-scheduled-actions \
     --service-namespace {{namespace}} \ 
     --query 'ScheduledActions[?ResourceId==`{{identifier}}`]'
   ```

   **Windows**

   ```
   aws application-autoscaling describe-scheduled-actions --service-namespace {{namespace}} --query "ScheduledActions[?ResourceId==`{{identifier}}`]"
   ```

   下面是示例输出。

   ```
   [
       {
           "ScheduledActionName": "my-first-scheduled-action",
           "ScheduledActionARN": "{{arn}}",
           "Schedule": "cron(0 9 * * ? *)",
           "ScalableTargetAction": {
               "MinCapacity": 1,
               "MaxCapacity": 5
           },
           ...
       },
       {
           "ScheduledActionName": "my-second-scheduled-action",
           "ScheduledActionARN": "{{arn}}",
           "Schedule": "cron(0 20 * * ? *)",
           "ScalableTargetAction": {
               "MinCapacity": 0,
               "MaxCapacity": 0
           },
           ...
       }
   ]
   ```

## 步骤 3：添加目标跟踪扩缩策略
<a name="tutorial-create-target-tracking-policy"></a>

现在，您已经准备好基本计划，请添加一个目标跟踪扩缩策略，以根据当前资源利用率进行扩展。

通过目标跟踪，Application Auto Scaling 会将策略中的目标值与指定指标的当前值进行比较。如果两个值在一段时间内不相等，Application Auto Scaling 会添加或删除容量以保持稳定的性能。随着应用程序负载和指标值的增加，Application Auto Scaling 会尽可能快地增加容量，而不会超过 `MaxCapacity`。当 Application Auto Scaling 由于负载最小而删除容量时，它会在不低于 `MinCapacity` 时执行此操作。通过根据使用情况调整容量，您只需支付应用程序所需的费用。

如果由于您的应用程序没有任何负载而导致指标数据不足，则 Application Auto Scaling 不会添加或删除容量。换句话说，Application Auto Scaling 在信息不足的情况下会优先考虑可用性。

您可以添加多个扩缩策略，但请确保不会添加冲突的分步扩缩策略，这可能会导致不良行为。例如，如果步进扩展策略在目标跟踪策略准备执行缩减之前启动缩减活动，则不会阻止缩减活动。在横向缩减活动完成后，目标跟踪策略可能会指示 Application Auto Scaling 再次横向扩展。

**创建目标跟踪扩展策略**

1. 使用以下 [put-scaling-policy](https://docs.amazonaws.cn/cli/latest/reference/application-autoscaling/put-scaling-policy.html) 命令创建策略。

   最常用于目标跟踪的指标是预定义的，您可以在不提供 CloudWatch 的完整指标规范的情况下使用这些指标。有关可用预定义指标的更多信息，请参阅 [Application Auto Scaling 的目标跟踪扩缩策略](application-auto-scaling-target-tracking.md)。

   在运行此命令之前，请确保您的预定义指标需要目标值。例如，要在 CPU 利用率达到 50% 时横向扩展，请将目标值指定为 50.0。或者，要在使用率达到 70% 时横向扩展 Lambda 预置并发，请将目标值指定为 0.7。有关特定资源目标值的信息，请参阅服务提供的有关如何配置目标跟踪的文档。有关更多信息，请参阅 [Amazon Web Services 服务 可以与 Application Auto Scaling 一起使用](integrated-services-list.md)。

   **Linux、macOS 或 Unix**

   ```
   aws application-autoscaling put-scaling-policy \
     --service-namespace {{namespace}} \
     --scalable-dimension {{dimension}} \
     --resource-id {{identifier}} \
     --policy-name {{my-scaling-policy}} --policy-type TargetTrackingScaling \
     --target-tracking-scaling-policy-configuration '{ "TargetValue": {{50.0}}, "PredefinedMetricSpecification": { "PredefinedMetricType": "{{predefinedmetric}}" }}'
   ```

   **Windows**

   ```
   aws application-autoscaling put-scaling-policy --service-namespace {{namespace}} --scalable-dimension {{dimension}} --resource-id {{identifier}} --policy-name {{my-scaling-policy}} --policy-type TargetTrackingScaling --target-tracking-scaling-policy-configuration "{ \"TargetValue\": {{50.0}}, \"PredefinedMetricSpecification\": { \"PredefinedMetricType\": \"{{predefinedmetric}}\" }}"
   ```

   如果成功，此命令将返回代表您创建的两个 CloudWatch 警报的 ARN 和名称。

1. 要确认您的计划操作是否存在，请使用以下 [describe-scaling-policies](https://docs.amazonaws.cn/cli/latest/reference/application-autoscaling/describe-scaling-policies.html) 命令。

   **Linux、macOS 或 Unix**

   ```
   aws application-autoscaling describe-scaling-policies --service-namespace {{namespace}} \
     --query 'ScalingPolicies[?ResourceId==`{{identifier}}`]'
   ```

   **Windows**

   ```
   aws application-autoscaling describe-scaling-policies --service-namespace {{namespace}} --query "ScalingPolicies[?ResourceId==`{{identifier}}`]"
   ```

   下面是示例输出。

   ```
   [
       {
           "PolicyARN": "{{arn}}",
           "TargetTrackingScalingPolicyConfiguration": {
               "PredefinedMetricSpecification": {
                   "PredefinedMetricType": "{{predefinedmetric}}"
               },
               "TargetValue": 50.0
           },
           "PolicyName": "my-scaling-policy",
           "PolicyType": "TargetTrackingScaling",
           "Alarms": [],
           ...
       }
   ]
   ```

## 步骤 4：后续步骤
<a name="tutorial-next-steps"></a>

发生扩缩活动时，您可以在可扩展目标的扩缩活动输出中看到该活动的记录，例如：

```
Successfully set desired count to 1. Change successfully fulfilled by ecs.
```

要使用 Application Auto Scaling 监控扩缩活动，您可以使用以下 [describe-scaling-activities](https://docs.amazonaws.cn/cli/latest/reference/application-autoscaling/describe-scaling-activities.html) 命令。

**Linux、macOS 或 Unix**

```
aws application-autoscaling describe-scaling-activities 
  --service-namespace {{namespace}} \
  --scalable-dimension {{dimension}} \
  --resource-id {{identifier}}
```

**Windows**

```
aws application-autoscaling describe-scaling-activities --service-namespace {{namespace}} --scalable-dimension {{dimension}} --resource-id {{identifier}}
```

## 第 5 步：清理
<a name="tutorial-scaling-clean-up"></a>

为防止您的账户对主动扩缩时创建的资源产生费用，您可以按如下方式清理关联的扩缩配置。

删除扩展配置不会删除底层 Amazon 资源。该操作也不会恢复为其原来的容量。您可以使用创建资源的服务的控制台来删除该资源或调整其容量。

**删除计划的操作**  
以下 [delete-scheduled-action](https://docs.amazonaws.cn/cli/latest/reference/application-autoscaling/delete-scheduled-action.html) 命令可删除指定的计划操作。如果您要保留创建的计划操作，您可以跳过此步骤。

**Linux、macOS 或 Unix**

```
aws application-autoscaling delete-scheduled-action \
  --service-namespace {{namespace}} \
  --scalable-dimension {{dimension}} \
  --resource-id {{identifier}} \
  --scheduled-action-name {{my-second-scheduled-action}}
```

**Windows**

```
aws application-autoscaling delete-scheduled-action --service-namespace {{namespace}} --scalable-dimension {{dimension}} --resource-id {{identifier}} --scheduled-action-name {{my-second-scheduled-action}}
```

**删除扩缩策略**  
以下 [delete-scaling-policy](https://docs.amazonaws.cn/cli/latest/reference/application-autoscaling/delete-scaling-policy.html) 命令可删除指定的目标跟踪扩缩策略。如果您要保留创建的扩缩策略，您可以跳过此步骤。

**Linux、macOS 或 Unix**

```
aws application-autoscaling delete-scaling-policy \
  --service-namespace {{namespace}} \
  --scalable-dimension {{dimension}} \
  --resource-id {{identifier}} \
  --policy-name {{my-scaling-policy}}
```

**Windows**

```
aws application-autoscaling delete-scaling-policy --service-namespace {{namespace}} --scalable-dimension {{dimension}} --resource-id {{identifier}} --policy-name {{my-scaling-policy}}
```

**撤消可扩展目标的注册**  
使用以下 [deregister-scalable-target](https://docs.amazonaws.cn/cli/latest/reference/application-autoscaling/deregister-scalable-target.html) 命令可取消注册可扩展目标。如果您有任何您创建的扩展策略或尚未删除的计划操作，这条命令会将它们删除。如果您要将此可扩展目标保留供将来使用，您可以跳过此操作。

**Linux、macOS 或 Unix**

```
aws application-autoscaling deregister-scalable-target \
  --service-namespace {{namespace}} \
  --scalable-dimension {{dimension}} \
  --resource-id {{identifier}}
```

**Windows**

```
aws application-autoscaling deregister-scalable-target --service-namespace {{namespace}} --scalable-dimension {{dimension}} --resource-id {{identifier}}
```