

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

# 使用一次性全部流量转移
<a name="deployment-guardrails-blue-green-all-at-once"></a>

使用一次性全部流量转移，您可以通过蓝绿部署这种安全防护机制，快速推出端点更新。您可以使用此流量转移选项来尽可能缩短更新的持续时间，同时仍可利用蓝绿部署提供的可用性保障。烘焙期功能可协助您在终止旧实例之前监控新实例的性能和功能性，确保您的新实例集可以完全正常工作。

下图显示了一次性全部流量转移如何管理新旧实例集。

![\[100% 的流量从旧实例集成功转移到新实例集。\]](http://docs.aws.amazon.com/zh_cn/sagemaker/latest/dg/images/deployment-guardrails-blue-green-all-at-once.png)


当您使用一次性全部流量转移时，SageMaker AI 会将 100% 的流量路由到新的实例集（绿色实例集）。一旦绿色实例集开始接收流量，烘焙期即开始。烘焙期是设定的一段时间，在该时段内，预先指定的 Amazon CloudWatch 警报监控绿色实例集的性能。如果在烘焙期间没有触发警报，SageMaker AI 将终止旧实例集（蓝色实例集）。如果在烘焙期间触发了任何警报，则会启动自动回滚，将 100% 的流量转移回蓝色实例集。

## 先决条件
<a name="deployment-guardrails-blue-green-all-at-once-prereqs"></a>

在设置采用一次性全部流量转移的部署之前，您必须创建 Amazon CloudWatch 警报以监视端点中的指标。如果在烘焙期间触发了任何警报，则流量将回滚到您的蓝色实例集。要了解如何在端点上设置 CloudWatch 警报，请参阅先决条件页面[自动回滚配置和监控](deployment-guardrails-configuration.md)。有关 CloudWatch 警报的更多信息，请参阅《Amazon CloudWatch 用户指南》**中的[使用 Amazon CloudWatch 警报](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html)。

## 配置一次性全部流量转移
<a name="deployment-guardrails-blue-green-all-at-once-configure"></a>

在准备好部署并为端点设置了 CloudWatch 警报之后，您可以使用 SageMaker AI [UpdateEndpoint](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UpdateEndpoint.html) API 或 AWS Command Line Interface 中的 [update-endpoint](https://docs.aws.amazon.com/cli/latest/reference/sagemaker/update-endpoint.html) 命令来启动部署。

**Topics**
+ [如何更新端点 (API)](#deployment-guardrails-blue-green-all-at-once-configure-api-update)
+ [如何使用现有的蓝绿更新政策更新端点 (API)](#deployment-guardrails-blue-green-all-at-once-configure-api-existing)
+ [如何更新端点 (CLI)](#deployment-guardrails-blue-green-all-at-once-configure-cli-update)

### 如何更新端点 (API)
<a name="deployment-guardrails-blue-green-all-at-once-configure-api-update"></a>

以下示例展示了如何使用 Amazon SageMaker API 中的 [UpdateEndpoint](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UpdateEndpoint.html)，对您的端点进行一次性全部流量转移更新。

```
import boto3
client = boto3.client("sagemaker")

response = client.update_endpoint(
    EndpointName="<your-endpoint-name>",
    EndpointConfigName="<your-config-name>",
    DeploymentConfig={
        "BlueGreenUpdatePolicy": {
            "TrafficRoutingConfiguration": {
                "Type": "ALL_AT_ONCE"
            },
            "TerminationWaitInSeconds": 600,
            "MaximumExecutionTimeoutInSeconds": 1800
        },
        "AutoRollbackConfiguration": {
            "Alarms": [
                {
                    "AlarmName": "<your-cw-alarm>"
                },
            ]
        }
    }
)
```

若要配置一次性全部流量转移选项，请执行下列操作：
+ 对于 `EndpointName`，请使用要更新的现有端点的名称。
+ 对于 `EndpointConfigName`，请使用要使用的端点配置的名称。
+ 在 `DeploymentConfig` 和 `BlueGreenUpdatePolicy` 下的 `TrafficRoutingConfiguration` 中，将 `Type` 参数设置为 `ALL_AT_ONCE`。这指定了部署使用一次性全部流量转移模式。
+ 对于 `TerminationWaitInSeconds`，请使用 `600`。此参数告知 SageMaker AI 在绿色实例集完全激活后等待指定的时间长度（以秒为单位），然后再终止蓝色实例集内的实例。在此示例中，SageMaker AI 在最后烘焙期结束后等待了 10 分钟，然后再终止蓝色实例集。
+ 对于 `MaximumExecutionTimeoutInSeconds`，请使用 `1800`。此参数设置部署在超时之前可以运行的最长时间。在前面的示例中，您的部署的完成时间限制为 30 分钟。
+ 在 `AutoRollbackConfiguration` 的 `Alarms` 字段中，您可以按名称添加 CloudWatch 警报。为要使用的每个警报创建一个 `AlarmName: <your-cw-alarm>` 条目。

### 如何使用现有的蓝绿更新政策更新端点 (API)
<a name="deployment-guardrails-blue-green-all-at-once-configure-api-existing"></a>

使用 [CreateEndpoint](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateEndpoint.html) API 创建端点时，您可以选择指定部署配置，以便在以后的端点更新中重复使用。您可以使用与之前的 UpdateEndpoint API 示例中相同的 `DeploymentConfig` 选项。CreateEndpoint API 的行为不会改变。指定部署配置不会自动在端点上执行蓝绿更新。

使用 [UpdateEndpoint](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UpdateEndpoint.html) API 更新您的端点时，会出现使用以前部署配置的选项。更新端点时，您可以使用 `RetainDeploymentConfig` 选项，以保留创建端点时您指定的部署配置。

调用 [UpdateEndpoint](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UpdateEndpoint.html) API 时，请将 `RetainDeploymentConfig` 设置为 `True`，以保留您的原始端点配置的 `DeploymentConfig` 选项。

```
response = client.update_endpoint(
    EndpointName="<your-endpoint-name>",
    EndpointConfigName="<your-config-name>",
    RetainDeploymentConfig=True
)
```

### 如何更新端点 (CLI)
<a name="deployment-guardrails-blue-green-all-at-once-configure-cli-update"></a>

如果您使用的是 AWS CLI，以下示例展示了如何使用 [update-endpoint](https://docs.aws.amazon.com/cli/latest/reference/sagemaker/update-endpoint.html) 命令启动蓝绿一次性全部部署。

```
update-endpoint
--endpoint-name <your-endpoint-name> 
--endpoint-config-name <your-config-name> 
--deployment-config '"BlueGreenUpdatePolicy": {"TrafficRoutingConfiguration": {"Type": "ALL_AT_ONCE"},
    "TerminationWaitInSeconds": 600, "MaximumExecutionTimeoutInSeconds": 1800},
    "AutoRollbackConfiguration": {"Alarms": [{"AlarmName": "<your-alarm>"}]}'
```

若要配置一次性全部流量转移选项，请执行下列操作：
+ 对于 `endpoint-name`，请使用要更新的端点的名称。
+ 对于 `endpoint-config-name`，请使用要使用的端点配置的名称。
+ 对于 `deployment-config`，请使用 [BlueGreenUpdatePolicy](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_BlueGreenUpdatePolicy.html) JSON 对象。

**注意**  
如果您希望将 JSON 对象保存在文件中，请参阅《AWS CLI 用户指南》**中的[生成 AWS CLI 骨架和输入参数](https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-skeleton.html)。