

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

# 将端点扩展到零个实例
<a name="endpoint-auto-scaling-zero-instances"></a>

在为端点设置自动扩缩时，可以支持横向缩减流程将使用中实例的数量减少到零。这样一来，在端点不处理推理请求因而不需要任何活动实例的时段内，您就可以节省成本。

但是，在横向缩减到零个实例后，您的端点在预调配至少一个实例之前，无法响应任何传入的推理请求。要自动执行预调配过程，请使用 Application Auto Scaling 创建步进扩展策略。然后，您将策略分配给 Amazon CloudWatch 警报。

设置步进扩展策略和警报后，端点将在收到它无法响应的推理请求后立即自动预调配一个实例。请注意，预调配过程需要几分钟时间。在此期间，任何调用端点的尝试都将生成错误。

以下过程说明如何为端点设置自动扩缩，以便端点可以横向缩减到零个实例和从零个实例进行横向扩展。这些过程通过 Amazon CLI使用命令。

**开始前的准备工作**

端点必须先满足以下要求，然后才能横向缩减到零个实例和从零个实例横向扩展：
+ 它已投入使用。
+ 它托管一个或多个推理组件。仅当端点托管推理组件时，它才能横向缩减到零个实例和从零个实例横向扩展。

  有关在 SageMaker AI 终端节点上托管推理组件的信息，请参阅[为实时推理部署模型](realtime-endpoints-deploy-models.md)。
+ 在端点配置中，对于生产变体 `ManagedInstanceScaling` 对象，您已将 `MinInstanceCount` 参数设置为 `0`。

  有关此参数的参考信息，请参见[ProductionVariantManagedInstanceScaling](https://docs.amazonaws.cn/sagemaker/latest/APIReference/API_ProductionVariantManagedInstanceScaling.html)。

**使端点能够横向缩减到零个实例（Amazon CLI）**

对于端点托管的每个推理组件，执行以下操作：

1. 将推理组件注册为可扩展的目标。当注册它时，请将最小容量设置为 `0`，如以下命令所示：

   ```
   aws application-autoscaling register-scalable-target \
     --service-namespace sagemaker \
     --resource-id inference-component/{{inference-component-name}} \
     --scalable-dimension sagemaker:inference-component:DesiredCopyCount \
     --min-capacity 0 \
     --max-capacity {{n}}
   ```

   在此示例中，{{inference-component-name}}替换为推理组件的名称。{{n}}替换为在扩展时要配置的最大推理组件副本数。

   有关此命令及其每个参数的更多信息，请参阅《*Amazon CLI 命令参考*》[register-scalable-target](https://docs.amazonaws.cn/cli/latest/reference/application-autoscaling/register-scalable-target.html)中的。

1. 将目标跟踪策略应用于推理组件，如以下命令所示：

   ```
   aws application-autoscaling put-scaling-policy \
     --policy-name my-scaling-policy \
     --policy-type TargetTrackingScaling \
     --resource-id inference-component/{{inference-component-name}} \
     --service-namespace sagemaker \
     --scalable-dimension sagemaker:inference-component:DesiredCopyCount \
     --target-tracking-scaling-policy-configuration file://config.json
   ```

   在此示例中，{{inference-component-name}}替换为推理组件的名称。

   在示例中，`config.json` 文件包含目标跟踪策略配置，如下所示：

   ```
   {
     "PredefinedMetricSpecification": {
         "PredefinedMetricType": "SageMakerInferenceComponentInvocationsPerCopy"
     },
     "TargetValue": 1,
     "ScaleInCooldown": 300,
     "ScaleOutCooldown": 300
   }
   ```

   有关跟踪策略配置的更多示例，请参阅[定义扩展策略](endpoint-auto-scaling-add-code-define.md)。

   有关此命令及其每个参数的更多信息，请参阅《*Amazon CLI 命令参考*》[put-scaling-policy](https://docs.amazonaws.cn/cli/latest/reference/application-autoscaling/put-scaling-policy.html)中的。

**使端点能够从零个实例横向扩展（Amazon CLI）**

对于端点托管的每个推理组件，执行以下操作：

1. 将步进扩展策略应用于推理组件，如以下命令所示：

   ```
   aws application-autoscaling put-scaling-policy \
     --policy-name {{my-scaling-policy}} \
     --policy-type StepScaling \
     --resource-id inference-component/{{inference-component-name}} \
     --service-namespace sagemaker \
     --scalable-dimension sagemaker:inference-component:DesiredCopyCount \
     --step-scaling-policy-configuration file://config.json
   ```

   在此示例中，{{my-scaling-policy}}替换为策略的唯一名称。{{inference-component-name}}替换为推理组件的名称。

   在示例中，`config.json` 文件包含步进扩展策略配置，如下所示：

   ```
   {
       "AdjustmentType": "ChangeInCapacity",
       "MetricAggregationType": "Maximum",
       "Cooldown": 60,
       "StepAdjustments":
         [
            {
              "MetricIntervalLowerBound": 0,
              "ScalingAdjustment": 1
            }
         ]
   }
   ```

   触发此分步扩展策略时， SageMaker AI 会预配置必要的实例来支持推理组件的副本。

   创建步进扩展策略后，记下其 Amazon 资源名称（ARN）。在下一步中，您需要 CloudWatch 警报的 ARN。

   有关步进扩展策略的更多信息，请参阅《Application Auto Scaling 用户指南》**中的[步进扩展策略](https://docs.amazonaws.cn/autoscaling/application/userguide/application-auto-scaling-step-scaling-policies.html)。

1. 创建 CloudWatch 警报并为其分配步进缩放策略，如以下示例所示：

   ```
   aws cloudwatch put-metric-alarm \
   --alarm-actions {{step-scaling-policy-arn}} \
   --alarm-description "Alarm when SM IC endpoint invoked that has 0 instances." \
   --alarm-name {{ic-step-scaling-alarm}} \
   --comparison-operator GreaterThanThreshold  \
   --datapoints-to-alarm 1 \
   --dimensions "Name=InferenceComponentName,Value={{inference-component-name}}" \
   --evaluation-periods 1 \
   --metric-name NoCapacityInvocationFailures \
   --namespace AWS/SageMaker \
   --period 60 \
   --statistic Sum \
   --threshold 1
   ```

   在此示例中，{{step-scaling-policy-arn}}替换为分步扩展策略的 ARN。{{ic-step-scaling-alarm}}用您选择的名字替换。{{inference-component-name}}替换为推理组件的名称。

   此示例将`--metric-name`参数设置为`NoCapacityInvocationFailures`。 SageMaker 当终端节点收到推理请求但该终端节点没有活动实例可以处理该请求时，AI 会发出此指标。当该事件发生时，警报会启动上一步中的步进扩展策略。

   有关此命令及其每个参数的更多信息，请参阅《*Amazon CLI 命令参考*》[put-metric-alarm](https://docs.amazonaws.cn/cli/latest/reference/cloudwatch/put-metric-alarm.html)中的。