

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

# 将资源迁移到最新 Operator
<a name="kubernetes-sagemaker-operators-migrate"></a>

我们将停止对 [Kubernetes SageMaker 操作](https://github.com/aws/amazon-sagemaker-operator-for-k8s/tree/master)员的原始版本的开发和技术支持。

如果您当前使用的是 [Kubernetes SageMaker 操作员版本`v1.2.2`或更](https://github.com/aws/amazon-sagemaker-operator-for-k8s/tree/master)低版本，我们建议您将资源迁移到适用于 Amazon 的 [ACK 服务控制](https://github.com/aws-controllers-k8s/sagemaker-controller)器。 SageMakerACK 服务控制器是基于 Kubernetes 控制[AWS 器 (ACK) 的新一代 Kuber](https://aws-controllers-k8s.github.io/community/) netes SageMaker 操作员。

有关终止对 Kubernetes SageMaker 操作员原始版本支持的常见问题解答，请参阅 [宣布终止对 Kubernetes SageMaker 人工智能运算符原始版本的支持](kubernetes-sagemaker-operators-eos-announcement.md)

使用以下步骤迁移您的资源，并使用 ACK 通过 Amazon A SageMaker I 训练、调整和部署机器学习模型。

**注意**  
适用于 Kubernetes 的最新 SageMaker 人工智能运算符不向后兼容。

**Topics**
+ [前提条件](#migrate-resources-to-new-operators-prerequisites)
+ [采用资源](#migrate-resources-to-new-operators-steps)
+ [清理旧资源](#migrate-resources-to-new-operators-cleanup)
+ [使用适用于 Kubernetes 的全新 SageMaker AI 运算符](#migrate-resources-to-new-operators-tutorials)

## 前提条件
<a name="migrate-resources-to-new-operators-prerequisites"></a>

要成功将资源迁移到适用于 Kubernetes 的最新 SageMaker 人工智能操作员，您必须执行以下操作：

1. 安装适用于 Kubernet SageMaker es 的最新人工智能运算符。有关 step-by-step说明，请参阅*使用 ACK SageMaker AI 控制器在 Machine Learning* 中进行[设置](https://aws-controllers-k8s.github.io/community/docs/tutorials/sagemaker-example/#setup)。

1. 如果您使用的是 [HostingAutoscalingPolicy 资源](#migrate-resources-to-new-operators-hap)，请安装新的 Application Auto Scaling Operator。有关 step-by-step说明，请参阅[使用 App *lication Auto Scaling 扩展 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>

适用于 Kubernetes 的全新 SageMaker AI 操作员能够采用最初不是由 ACK 服务控制器创建的资源。有关更多信息，请参阅 ACK 文档中的[采用现有 AWS 资源](https://aws-controllers-k8s.github.io/community/docs/user-docs/adopted-resource/)。

以下步骤展示了适用于 Kubernetes 的新 SageMaker 人工智能运营商如何采用现有 SageMaker 的人工智能终端节点。将以下示例代码另存为名为 `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) 资源由多个 Application Auto Scaling 资源组成：`ScalableTarget` 和 `ScalingPolicy`。在 ACK 中采用 HAP 资源时，请先安装 [Application Auto Scaling controller 控制器](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 中采用 A SageMaker I 端点，则需要`Model`分别采用`Endpoint``EndpointConfig`、和。可在 `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>

在适用于 Kubernetes 的新 SageMaker AI 操作员采用您的资源后，您可以卸载旧的运算符并清理旧资源。

### 步骤 1：卸载旧 Operator
<a name="migrate-resources-to-new-operators-uninstall"></a>

要卸载旧 Operator，请参阅[删除 Operator](kubernetes-sagemaker-operators-end-of-support.md#delete-operators)。

**警告**  
删除任何旧资源之前，请先卸载旧 Operator。

### 第 2 步：删除终结器并删除旧资源
<a name="migrate-resources-to-new-operators-delete-resources"></a>

**警告**  
删除旧资源之前，请确保已卸载旧 Operator。

卸载旧 Operator 后，必须明确删除终结器才能删除旧 Operator 资源。以下示例脚本显示如何删除给定命名空间中由旧 Operator 管理的所有训练作业。新 Operator 采用其他资源后，您可以使用类似的模式删除这些资源。

**注意**  
必须使用完整的资源名称才能获取资源。例如，使用 `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
```

## 使用适用于 Kubernetes 的全新 SageMaker AI 运算符
<a name="migrate-resources-to-new-operators-tutorials"></a>

有关使用适用于 Kubernetes 的全新 SageMaker AI 运算符的深入指南，请参阅 [为 Kubernetes 使用 SageMaker 人工智能运算符](kubernetes-sagemaker-operators-ack.md#kubernetes-sagemaker-operators-ack-use)