

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

# JumpStart 使用 kubectl 部署模型
<a name="sagemaker-hyperpod-model-deployment-deploy-js-kubectl"></a>

以下步骤向您展示了如何使用 kubect JumpStart l 将模型部署到 HyperPod 集群。

以下说明包含代码单元以及设计为在终端运行的命令。在执行这些命令之前，请确保已使用 AWS 凭据配置您的环境。

## 先决条件
<a name="kubectl-prerequisites"></a>

在开始之前，请确认您已经：
+ 在您的 Amazon SageMaker HyperPod 集群上设置推理功能。有关更多信息，请参阅 [设置 HyperPod 集群以进行模型部署](sagemaker-hyperpod-model-deployment-setup.md)。
+ 安装了 [kubectl](https://kubernetes.io/docs/reference/kubectl/) 实用程序并在终端中配置了 [jq](https://jqlang.org/)。

## 设置和配置
<a name="kubectl-prerequisites-setup-and-configuration"></a>

1. 选择您的区域。

   ```
   export REGION=<region>
   ```

1. 查看所有 SageMaker 公共中心模型和 HyperPod 集群。

1. 从 JumpstartPublic Hub `JumpstartModel` 中选择一个。 JumpstartPublic hub 有大量可用的模型，因此您可以使用它`NextToken`来迭代列出公共中心中的所有可用模型。

   ```
   aws sagemaker list-hub-contents --hub-name SageMakerPublicHub --hub-content-type Model --query '{Models: HubContentSummaries[].{ModelId:HubContentName,Version:HubContentVersion}, NextToken: NextToken}' --output json
   ```

   ```
   export MODEL_ID="deepseek-llm-r1-distill-qwen-1-5b"
   export MODEL_VERSION="2.0.4"
   ```

1. 将所选模型 ID 和集群名称配置到下方的变量中。
**注意**  
请与您的集群管理员核实，确保已为该角色或用户授予权限。您可以运行 `!aws sts get-caller-identity --query "Arn"` 来检查您在终端使用的角色或用户。

   ```
   aws sagemaker list-clusters --output table
   
   # Select the cluster name where you want to deploy the model.
   export HYPERPOD_CLUSTER_NAME="<insert cluster name here>"
   
   # Select the instance that is relevant for your model deployment and exists within the selected cluster.
   # List availble instances in your HyperPod cluster
   aws sagemaker describe-cluster --cluster-name=$HYPERPOD_CLUSTER_NAME --query "InstanceGroups[].{InstanceType:InstanceType,Count:CurrentCount}" --output table
   
   # List supported instance types for the selected model
   aws sagemaker describe-hub-content --hub-name SageMakerPublicHub --hub-content-type Model --hub-content-name "$MODEL_ID" --output json | jq -r '.HubContentDocument | fromjson | {Default: .DefaultInferenceInstanceType, Supported: .SupportedInferenceInstanceTypes}'
   
   
   # Select and instance type from the cluster that is compatible with the model. 
   # Make sure that the selected instance is either default or supported instance type for the jumpstart model 
   export INSTANCE_TYPE="<Instance_type_In_cluster"
   ```

1. 向集群管理员确认您可使用的命名空间。管理员应已在您的命名空间中创建一个 `hyperpod-inference` 服务账户。

   ```
   export CLUSTER_NAMESPACE="default"
   ```

1. 为要创建的端点和自定义对象设置名称。

   ```
   export SAGEMAKER_ENDPOINT_NAME="deepsek-qwen-1-5b-test"
   ```

1. 下面是 JumpStart 中 `deepseek-llm-r1-distill-qwen-1-5b` 模型部署的示例。根据您在上述步骤中选择的模型创建类似的部署 yaml 文件。
**注意**  
如果您的集群使用带有 MIG 的 GPU 分区，则可以通过将该`acceleratorPartitionType`字段添加到服务器规格中来请求特定的 MIG 配置文件。有关更多信息，请参阅 [使用 MIG 提交任务](sagemaker-hyperpod-eks-gpu-partitioning-task-submission.md)。

   ```
   cat << EOF > jumpstart_model.yaml
   ---
   apiVersion: inference.sagemaker.aws.amazon.com/v1
   kind: JumpStartModel
   metadata:
     name: $SAGEMAKER_ENDPOINT_NAME
     namespace: $CLUSTER_NAMESPACE 
   spec:
     sageMakerEndpoint:
       name: $SAGEMAKER_ENDPOINT_NAME
     model:
       modelHubName: SageMakerPublicHub
       modelId: $MODEL_ID
       modelVersion: $MODEL_VERSION
     server:
       instanceType: $INSTANCE_TYPE
       # Optional: Specify GPU partition profile for MIG-enabled instances
       # acceleratorPartitionType: "1g.10gb"
     metrics:
       enabled: true
     environmentVariables:
       - name: SAMPLE_ENV_VAR
         value: "sample_value"
     maxDeployTimeInSeconds: 1800
     autoScalingSpec:
       cloudWatchTrigger:
         name: "SageMaker-Invocations"
         namespace: "AWS/SageMaker"
         useCachedMetrics: false
         metricName: "Invocations"
         targetValue: 10
         minValue: 0.0
         metricCollectionPeriod: 30
         metricStat: "Sum"
         metricType: "Average"
         dimensions:
           - name: "EndpointName"
             value: "$SAGEMAKER_ENDPOINT_NAME"
           - name: "VariantName"
             value: "AllTraffic"
   EOF
   ```

## 部署模型
<a name="kubectl-deploy-your-model"></a>

**更新您的 kubernetes 配置并部署模型**

1. 将 kubectl 配置为连接到由 Amazon EKS 编排的 HyperPod 集群。

   ```
   export EKS_CLUSTER_NAME=$(aws --region $REGION sagemaker describe-cluster --cluster-name $HYPERPOD_CLUSTER_NAME \
     --query 'Orchestrator.Eks.ClusterArn' --output text | \
     cut -d'/' -f2)
   aws eks update-kubeconfig --name $EKS_CLUSTER_NAME --region $REGION
   ```

1. 部署您的 JumpStart 模型。

   ```
   kubectl apply -f jumpstart_model.yaml
   ```

**监控模型部署的状态**

1. 确认已成功部署模型。

   ```
   kubectl describe JumpStartModel $SAGEMAKER_ENDPOINT_NAME -n $CLUSTER_NAMESPACE
   ```

1. 确认已成功创建端点。

   ```
   aws sagemaker describe-endpoint --endpoint-name=$SAGEMAKER_ENDPOINT_NAME --output table
   ```

1. 调用模型端点。您可通过编程方式从 `JumpStartModel` 对象中检索示例有效载荷。

   ```
   aws sagemaker-runtime invoke-endpoint \
     --endpoint-name $SAGEMAKER_ENDPOINT_NAME \
     --content-type "application/json" \
     --body '{"inputs": "What is AWS SageMaker?"}' \
     --region $REGION \
     --cli-binary-format raw-in-base64-out \
     /dev/stdout
   ```

## 管理您的部署
<a name="kubectl-manage-your-deployment"></a>

一旦不再需要 JumpStart 模型部署，请将其删除。

```
kubectl delete JumpStartModel $SAGEMAKER_ENDPOINT_NAME -n $CLUSTER_NAMESPACE
```

**问题排查**

如果部署无法按预期工作，请使用以下调试命令。

1. 检查 Kubernetes 部署的状态。此命令可检查底层 Kubernetes 部署对象，该对象管理运行模型的容器组（pod）。使用此命令可解决容器组（pod）调度、资源分配和容器启动问题。

   ```
   kubectl describe deployment $SAGEMAKER_ENDPOINT_NAME -n $CLUSTER_NAMESPACE
   ```

1. 检查 JumpStart 模型资源的状态。此命令可检查自定义 `JumpStartModel` 资源，该资源将管理高级模型配置和部署生命周期。使用它来解决特定于模型的问题，例如配置错误或 SageMaker AI 端点创建问题。

   ```
   kubectl describe JumpStartModel $SAGEMAKER_ENDPOINT_NAME -n $CLUSTER_NAMESPACE
   ```

1. 检查所有 Kubernetes 对象的状态。此命令全面概述了命名空间中所有相关的 Kubernetes 资源。使用此命令可进行快速运行状况检查，以查看与您的模型部署关联的容器组（pod）、服务、部署和自定义资源的整体状态。

   ```
   kubectl get pods,svc,deployment,JumpStartModel,sagemakerendpointregistration -n $CLUSTER_NAMESPACE
   ```