

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

# 將資源遷移到最新的運算子
<a name="kubernetes-sagemaker-operators-migrate"></a>

我們正在停止對 [SageMaker Operators for Kubernetes](https://github.com/aws/amazon-sagemaker-operator-for-k8s/tree/master) 原始版本的開發和技術支援。

如果您目前使用的是 [SageMaker Operators for Kubernetes](https://github.com/aws/amazon-sagemaker-operator-for-k8s/tree/master) 的 `v1.2.2` 或以下版本，我們建議您將資源遷移到 [Amazon SageMaker 的 ACK 服務控制器](https://github.com/aws-controllers-k8s/sagemaker-controller)。ACK 服務控制器是新一代的 SageMaker Operators for Kubernetes，以 [AWS Kubernetes 專用控制器 (ACK)](https://aws-controllers-k8s.github.io/community/) 為基礎。

如需與終止支援 SageMaker Operators for Kubernetes 原始版本相關的常見問題的答案，請參閱[宣布終止支援 SageMaker AI Operators for Kubernetes 原始版本](kubernetes-sagemaker-operators-eos-announcement.md)

使用下列步驟遷移您的資源，並使用 ACK 搭配 Amazon SageMaker AI 訓練、調整和部署機器學習模型。

**注意**  
最新版本的 SageMaker AI Operators for Kubernetes 不具回溯相容性。

**Topics**
+ [先決條件](#migrate-resources-to-new-operators-prerequisites)
+ [採用資源](#migrate-resources-to-new-operators-steps)
+ [清除舊資源](#migrate-resources-to-new-operators-cleanup)
+ [使用新版 SageMaker AI Operators for Kubernetes](#migrate-resources-to-new-operators-tutorials)

## 先決條件
<a name="migrate-resources-to-new-operators-prerequisites"></a>

若要成功將資源遷移至最新版本的 SageMaker AI Operators for Kubernetes，您必須執行下列操作：

1. 安裝最新的 SageMaker AI Operators for Kubernetes。如需逐步指示，請參閱 *使用 ACK SageMaker AI 控制器進行機器學習*中的[設定](https://aws-controllers-k8s.github.io/community/docs/tutorials/sagemaker-example/#setup)。

1. 如果您正在使用 [HostingAutoscalingPolicy 資源](#migrate-resources-to-new-operators-hap)，請安裝新的應用程式自動擴展運算子。如需逐步指示，請參閱*使用應用程式自動擴展來擴展 SageMaker AI 工作負載*中的[設定](https://aws-controllers-k8s.github.io/community/docs/tutorials/autoscaling-example/#setup)。如果您不使用 HostingAutoScalingPolicy 資源，則此步驟為選用步驟。

如果許可設定正確，則 ACK SageMaker AI 服務控制器可以判斷 AWS 資源的規格和狀態，並協調資源，就像 ACK 控制器最初建立它一樣。

## 採用資源
<a name="migrate-resources-to-new-operators-steps"></a>

新版 SageMaker AI Operators for Kubernetes 能夠採用原本不是由 ACK 服務控制器建立的資源。如需詳細資訊，請參閱 ACK 文件中的[採用現有 AWS 資源](https://aws-controllers-k8s.github.io/community/docs/user-docs/adopted-resource/)。

下列步驟展示新版 SageMaker AI Operators for Kubernetes 如何採用現有的 SageMaker AI 端點。將下列範例儲存為名為 `adopt-endpoint-sample.yaml` 的檔案。

```
apiVersion: services.k8s.aws/v1alpha1
kind: AdoptedResource
metadata:
  name: adopt-endpoint-sample
spec:  
  aws:
    # resource to adopt, not created by ACK
    nameOrID: xgboost-endpoint
  kubernetes:
    group: sagemaker.services.k8s.aws
    kind: Endpoint
    metadata:
      # target K8s CR name
      name: xgboost-endpoint
```

使用 `kubectl apply` 提交自訂資源 (CR)：

```
kubectl apply -f adopt-endpoint-sample.yaml
```

使用 `kubectl describe` 檢查所採用資源的狀態條件。

```
kubectl describe adoptedresource adopt-endpoint-sample
```

確認 `ACK.Adopted` 條件為 `True`。輸出應類似以下範例：

```
---
kind: AdoptedResource
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: '{"apiVersion":"services.k8s.aws/v1alpha1","kind":"AdoptedResource","metadata":{"annotations":{},"name":"xgboost-endpoint","namespace":"default"},"spec":{"aws":{"nameOrID":"xgboost-endpoint"},"kubernetes":{"group":"sagemaker.services.k8s.aws","kind":"Endpoint","metadata":{"name":"xgboost-endpoint"}}}}'
  creationTimestamp: '2021-04-27T02:49:14Z'
  finalizers:
  - finalizers.services.k8s.aws/AdoptedResource
  generation: 1
  name: adopt-endpoint-sample
  namespace: default
  resourceVersion: '12669876'
  selfLink: "/apis/services.k8s.aws/v1alpha1/namespaces/default/adoptedresources/adopt-endpoint-sample"
  uid: 35f8fa92-29dd-4040-9d0d-0b07bbd7ca0b
spec:
  aws:
    nameOrID: xgboost-endpoint
  kubernetes:
    group: sagemaker.services.k8s.aws
    kind: Endpoint
    metadata:
      name: xgboost-endpoint
status:
  conditions:
  - status: 'True'
    type: ACK.Adopted
```

檢查您的資源是否存在於叢集中：

```
kubectl describe endpoints.sagemaker xgboost-endpoint
```

### HostingAutoscalingPolicy 資源
<a name="migrate-resources-to-new-operators-hap"></a>

`HostingAutoscalingPolicy` (HAP) 資源由多個應用程式自動擴展資源組成：`ScalableTarget` 和 `ScalingPolicy`。採用使用 ACK 的 HAP 資源時，請先安裝 [應用程式自動擴展控制器](https://github.com/aws-controllers-k8s/applicationautoscaling-controller)。要採用 HAP 資源，您需要採用 `ScalableTarget` 和 `ScalingPolicy` 資源。您可以在 `HostingAutoscalingPolicy` 資源 (`status.ResourceIDList`) 的狀態中找到這些資源的資源識別碼。

### HostingDeployment 資源
<a name="migrate-resources-to-new-operators-hosting-deployment"></a>

`HostingDeployment` 資源包含多個 SageMaker AI 資源：`Endpoint`、`EndpointConfig` 和每個 `Model`。如果您在 ACK 中採用 SageMaker AI 端點，則需要分別採用 `Endpoint`、`EndpointConfig` 和每個 `Model`。您可以在 `HostingDeployment` 資源 (`status.endpointName`、`status.endpointConfigName` 和 `status.modelNames`) 的狀態中找到 `Endpoint`、`EndpointConfig` 和 `Model` 名稱。

如需所有受支援 SageMaker AI 資源的清單，請參閱 [ACK API 參考](https://aws-controllers-k8s.github.io/community/reference/)。

## 清除舊資源
<a name="migrate-resources-to-new-operators-cleanup"></a>

新版 SageMaker AI Operators for Kubernetes 採用您的資源後，您可以解除安裝舊的運算子並清除舊資源。

### 第 1 步：解除安裝舊運算子
<a name="migrate-resources-to-new-operators-uninstall"></a>

若要解除安裝舊運算子，請參閱[刪除運算子](kubernetes-sagemaker-operators-end-of-support.md#delete-operators)。

**警告**  
刪除任何舊資源之前，請先解除安裝舊運算子。

### 第 2 步：移除終結器並刪除舊資源
<a name="migrate-resources-to-new-operators-delete-resources"></a>

**警告**  
刪除舊資源之前，請確定您已解除安裝舊運算子。

解除安裝舊操作符後，您必須明確移除終結器以刪除舊運算子資源。下列範例指令碼展示如何刪除指定命名空間中由舊運算子所管理的所有訓練工作。當新運算子採用其他資源時，您可以使用類似的模式來刪除其他資源。

**注意**  
您必須使用完整的資源名稱才能取得資源。例如，使用 `kubectl get trainingjobs.sagemaker.aws.amazon.com` 代替 `kubectl get trainingjob`。

```
namespace=sagemaker_namespace
training_jobs=$(kubectl get trainingjobs.sagemaker.aws.amazon.com -n $namespace -ojson | jq -r '.items | .[] | .metadata.name')
 
for job in $training_jobs
do
    echo "Deleting $job resource in $namespace namespace"
    kubectl patch trainingjobs.sagemaker.aws.amazon.com $job -n $namespace -p '{"metadata":{"finalizers":null}}' --type=merge
    kubectl delete trainingjobs.sagemaker.aws.amazon.com $job -n $namespace
done
```

## 使用新版 SageMaker AI Operators for Kubernetes
<a name="migrate-resources-to-new-operators-tutorials"></a>

如需使用新版 SageMaker AI Operators for Kubernetes 的深度指南，請參閱[使用 SageMaker AI Operators for Kubernetes](kubernetes-sagemaker-operators-ack.md#kubernetes-sagemaker-operators-ack-use)