

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 Canary 流量轉移
<a name="deployment-guardrails-blue-green-canary"></a>

透過 Canary 流量轉移，您可以在新機群上測試部分端點流量，而舊機群則為剩餘的流量提供服務。此測試步驟是安全防護機制，可在將所有流量轉移到新機群之前驗證新機群的功能。您仍然可以享有藍/綠部署的優點，而新增的 Canary 功能可讓您確保新的 (綠色) 機群能夠提供推論，然後再讓它處理 100% 的流量。

開啟接收流量的綠色機群的部分稱為 Canary，您可以選擇此 Canary 的大小。請注意，Canary 大小應小於或等於新容量的 50%。製作中期間結束，且觸發沒有預先指定的 Amazon CloudWatch 警示時，其餘流量就會從舊機群 (藍色) 轉移到綠色機群。Canary 流量轉移可在部署期間為您提供更高的安全性，因為更新模型的任何問題都只會影響 Canary。

下圖顯示了 Canary 流量轉移如何管理藍色和綠色機群之間的流量分佈。

![\[成功的兩步驟 Canary 流量從舊機群轉移到新機群。\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/deployment-guardrails-blue-green-canary.png)


SageMaker AI 佈建綠色機群後，SageMaker AI 會將部分傳入流量 (例如 25%) 路由至 Canary。然後製作中期間開始，在此期間，您的 CloudWatch 警示會監控綠色機群的效能。在此期間，藍色機群和綠色機群都處於部分作用中狀態，並接收流量。如果製作中期間觸發任何警示，則 SageMaker AI 會啟動復原，且所有流量都會傳回藍色機群。如果沒有觸發任何警報，則所有流量都會轉移到綠色機群，並且有最後的製作中期間。如果最後的製作中期間結束時，沒有觸發任何警示，則綠色機群會為所有流量提供服務，SageMaker AI 會終止藍色機群。

## 先決條件
<a name="deployment-guardrails-blue-green-canary-prereqs"></a>

在設定使用 Canary 流量轉移的部署之前，您必須先建立 Amazon CloudWatch 警示，以從端點監控指標。警示在製作中期間處於作用中狀態，如果觸發任何警示，則所有端點流量都會回復到藍色機群。要瞭解如何在終端節點上設置 CloudWatch 警報，請參閱先決條件頁面[自動回復組態與監控](deployment-guardrails-configuration.md)。如需進一步了解 CloudWatch 警示，請參閱 *Amazon CloudWatch 使用者指南*中的[使用 Amazon CloudWatch 警示](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html)。

## 設定 Canary 流量轉移
<a name="deployment-guardrails-blue-green-canary-configure"></a>

一旦準備好進行部署，並為您的端點設定 Amazon CloudWatch 警示後，您可以使用 Amazon SageMaker AI [UpdateEndpoint](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UpdateEndpoint.html) API 或 AWS CLI 中的 [update-endpoint](https://docs.aws.amazon.com/cli/latest/reference/sagemaker/update-endpoint.html) 命令來啟動部署。

**Topics**
+ [如何更新端點 (API)](#deployment-guardrails-blue-green-canary-configure-api-update)
+ [如何使用現有的藍色/綠色更新政策 (API) 來更新端點](#deployment-guardrails-blue-green-canary-configure-api-existing)
+ [如何更新端點 (CLI)](#deployment-guardrails-blue-green-canary-configure-cli-update)

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

下列 [UpdateEndpoint](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UpdateEndpoint.html) API 範例顯示如何使用 Canary 流量轉移來更新端點。

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

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

若要設定 Canary 流量轉移選項，請執行以下操作：
+ 針對 `EndpointName`，請使用您要更新的現有端點名稱。
+ 在 `EndpointConfigName` 中，使用您要使用的端點組態名稱。
+ 在 `DeploymentConfig` 和 `BlueGreenUpdatePolicy` 下，在 `TrafficRoutingConfiguration` 中，將 `Type` 參數設定為 `CANARY`。這會指定使用 Canary 流量轉移的部署。
+ 在 `CanarySize` 欄位中，您可以修改 `Type` 和 `Value` 參數來變更 Canary 的大小。在 `Type` 中，使用 `CAPACITY_PERCENT`，表示您要用作 Canary 的綠色機群的百分比，然後設定 `Value` 為 `30`。在此範例中，您使用綠色機群 30% 的容量作為 Canary。請注意，Canary 大小應等於或小於綠色機群容量的 50％。
+ 對於 `WaitIntervalInSeconds`，請使用 `600`。參數會告知 SageMaker AI 等待每個間隔轉移之間的指定時間 (以秒為單位)。此間隔是 Canary 製作中期間的持續時間。在上述範例中，SageMaker AI 會在 Canary 轉移後等待 10 分鐘，然後完成第二個也是最後一個流量轉移。
+ 對於 `TerminationWaitInSeconds`，請使用 `600`。參數會告知 SageMaker AI 在綠色機群完全作用後等待指定的時間量 (以秒為單位)，然後才終止藍色機群中的執行個體。在此範例中，SageMaker AI 會在最後製作中期間後等待 10 分鐘，然後才終止藍色機群。
+ 對於 `MaximumExecutionTimeoutInSeconds`，請使用 `1800`。此參數設定部署在逾時前可以執行的時間上限。在上述範例中，您的部署時間上限為 30 分鐘。
+ 在 `AutoRollbackConfiguration` 中，`Alarms` 欄位，您可以依據名稱新增您的 CloudWatch 警示。為您要使用的每個警示建立一個 `AlarmName: <your-cw-alarm>` 項目。

### 如何使用現有的藍色/綠色更新政策 (API) 來更新端點
<a name="deployment-guardrails-blue-green-canary-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-canary-configure-cli-update"></a>

如果您使用的是 AWS CLI，下列範例會示範如何使用 [update-endpoint](https://docs.aws.amazon.com/cli/latest/reference/sagemaker/update-endpoint.html) 命令，來啟動藍/綠 Canary 部署。

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

若要設定 Canary 流量轉移選項，請執行下列動作：
+ 在 `endpoint-name` 中，使用您要更新的端點名稱。
+ 在 `endpoint-config-name` 中，使用您要使用的端點組態名稱。
+ 在 `deployment-config` 中，使用 [BlueGreenUpdatePolicy](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_BlueGreenUpdatePolicy.html) JSON 物件。

**注意**  
如果您希望將 JSON 對象保存在文件中，請參閱[正在生成AWS CLI骨架和輸入參數](https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-skeleton.html)在*AWS CLI用戶指南*。