

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

# 在 Amazon SageMaker HyperPod 中設定自訂 Kubernetes 標籤和污點
<a name="sagemaker-hyperpod-eks-custom-labels-and-taints"></a>

具有 Amazon Elastic Kubernetes Service (Amazon EKS) 協調器的 Amazon SageMaker HyperPod 叢集支援執行個體群組內節點的自訂 Kubernetes 標籤和污點。標籤和污點是 Kubernetes 中的基本排程和組織機制，可讓您精細控制 Pod 放置和資源使用率。

標籤是可以連接到 Kubernetes 物件的鍵/值對，可讓您根據屬性組織和選取資源。與容錯搭配使用的污點是節點特定的屬性，可透過拒絕沒有相符容錯的 Pod 來影響 Pod 排程。這些機制共同可讓您隔離工作負載、根據硬體規格指派工作負載，以及確保最佳資源使用率。

## 常用案例
<a name="sagemaker-hyperpod-eks-custom-labels-and-taints-use-cases"></a>

以下是自訂標籤和污點有益的常見案例：
+ **在昂貴的執行個體上防止系統 Pod** - 將污點套用至 GPU 執行個體，以防止系統 Pod 和其他非關鍵工作負載耗用昂貴的運算資源
+ **與現有工具整合** - 套用符合您組織已建立的基礎設施模式和節點親和性組態的標籤

## 設定標籤和污點
<a name="sagemaker-hyperpod-eks-custom-labels-and-taints-configure"></a>

您可以使用叢集組態中的 `KubernetesConfig` 參數，在執行個體群組層級設定自訂 Kubernetes 標籤和污點。標籤和污點會套用至執行個體群組中的所有節點，並在整個叢集生命週期中保留。

`KubernetesConfig` 參數為宣告式，表示您為執行個體群組指定標籤和污點的完整所需狀態。SageMaker HyperPod 接著會協調節點的實際狀態，以符合此所需狀態。
+ **新增標籤或污點** - 在 中包含新標籤或污點，`KubernetesConfig`以及您要保留的任何現有標籤或污點
+ **更新標籤或污點** - 針對您要變更`KubernetesConfig`的標籤或污點修改 中的值，並包含您要保留的所有其他值
+ **移除標籤或污點** - 省略要從 移除的標籤或污點`KubernetesConfig`，只保留要保留的標籤或污點

### 建立具有標籤和污點的叢集
<a name="sagemaker-hyperpod-eks-custom-labels-and-taints-create"></a>

建立新的 SageMaker HyperPod 叢集時，請在執行個體群組組態中包含 `KubernetesConfig` 參數。下列範例示範如何建立具有自訂標籤和污點的叢集：

```
{
    "ClusterName": "my-cluster",
    "InstanceGroups": [{
        "InstanceGroupName": "worker-group-1",
        "InstanceType": "ml.p4d.24xlarge",
        "InstanceCount": 4,
        "LifeCycleConfig": {
            "SourceS3Uri": "s3://my-bucket/lifecycle-config.sh",
            "OnCreate": "on-create.sh"
        },
        "ExecutionRole": "arn:aws:iam::123456789012:role/HyperPodExecutionRole",
        "ThreadsPerCore": 1,
        "KubernetesConfig": { 
            "Labels": {
                "env": "prod",
                "team": "ml-training",
                "gpu-type": "a100"
            },
            "Taints": [{
                "key": "gpu",
                "value": "true",
                "effect": "NoSchedule"
            },
            {
                "key": "dedicated",
                "value": "ml-workloads",
                "effect": "NoExecute"
            }]
        }
    }],
    "VpcConfig": {
        "SecurityGroupIds": ["sg-0123456789abcdef0"],
        "Subnets": ["subnet-0123456789abcdef0", "subnet-0123456789abcdef1"]
    },
    "Orchestrator": {
        "Eks": {
            "ClusterArn": "arn:aws:eks:us-west-2:123456789012:cluster/my-eks-cluster"
        }
    }
}
```

在此範例中：
+ **標籤** - 套用三個自訂標籤：`team=ml-training`、 `env=prod`和 `gpu-type=a100`
+ **污點** - 設定兩個污點以防止不必要的 Pod 排程

### 更新現有叢集上的標籤和污點
<a name="sagemaker-hyperpod-eks-custom-labels-and-taints-update"></a>

您可以使用 `UpdateCluster` API 修改現有叢集上的標籤和污點。下列範例示範如何更新執行個體群組`KubernetesConfig`的 ：

```
{
    "ClusterName": "my-cluster",
    "InstanceGroups": [{
        "InstanceGroupName": "worker-group-1",
        "KubernetesConfig": { 
            "Labels": {
                "env": "prod",
                "team": "ml-training",
                "gpu-type": "a100",
                "cost-center": "ml-ops"
            },
            "Taints": [{
                "key": "gpu",
                "value": "true",
                "effect": "NoSchedule"
            }]
        }
    }]
}
```

當您更新標籤和污點時，SageMaker HyperPod 會將變更套用至執行個體群組中的所有節點。服務會管理從目前到所需狀態的轉換，您可以使用 `DescribeCluster` API 來監控。

## 監控標籤和污點應用程式
<a name="sagemaker-hyperpod-eks-custom-labels-and-taints-monitor"></a>

SageMaker HyperPod 提供 APIs可在標籤和污點套用至叢集節點時監控標籤和污點的狀態。

### 檢查叢集層級狀態
<a name="sagemaker-hyperpod-eks-custom-labels-and-taints-describe-cluster"></a>

使用 `DescribeCluster` API 在執行個體群組層級檢視標籤和污點的目前和所需狀態。下列範例顯示回應結構：

```
{
    "ClusterName": "my-cluster",
    "ClusterStatus": "InService",
    "InstanceGroups": [{
        "InstanceGroupName": "worker-group-1",
        "InstanceType": "ml.p4d.24xlarge",
        "CurrentInstanceCount": 4,
        "TargetInstanceCount": 4,
        "KubernetesConfig": {
            "CurrentLabels": {
                "env": "prod",
                "team": "ml-training",
                "gpu-type": "a100"
            },
            "DesiredLabels": {
                "env": "prod",
                "team": "ml-training",
                "gpu-type": "a100"
            },
            "CurrentTaints": [{
                "key": "gpu",
                "value": "true",
                "effect": "NoSchedule"
            }],
            "DesiredTaints": [{
                "key": "gpu",
                "value": "true",
                "effect": "NoSchedule"
            }]
        }
    }]
}
```

當`CurrentLabels`相符`DesiredLabels`和`CurrentTaints`相符 時`DesiredTaints`，執行個體群組中的所有節點都會套用指定的組態。如果不同，叢集仍在套用變更的過程中。

### 檢查個別節點狀態
<a name="sagemaker-hyperpod-eks-custom-labels-and-taints-describe-node"></a>

如需節點層級的詳細資訊，請使用 `DescribeClusterNode` API 來檢查個別節點的標籤和污點組態。下列範例顯示回應結構：

```
{
    "NodeDetails": { 
        "InstanceId": "i-0123456789abcdef0",
        "InstanceGroupName": "worker-group-1",
        "InstanceType": "ml.p4d.24xlarge",
        "InstanceStatus": {
            "Status": "Running",
            "Message": "Node is healthy"
        },
        "LifeCycleConfig": {
            "SourceS3Uri": "s3://my-bucket/lifecycle-config.sh",
            "OnCreate": "on-create.sh"
        },
        "LaunchTime": 1699564800.0,
        "KubernetesConfig": {
            "CurrentLabels": {
                "env": "prod",
                "team": "ml-training",
                "gpu-type": "a100"
            },
            "DesiredLabels": {
                "env": "prod",
                "team": "ml-training",
                "gpu-type": "a100"
            },
            "CurrentTaints": [{
                "key": "gpu",
                "value": "true",
                "effect": "NoSchedule"
            }],
            "DesiredTaints": [{
                "key": "gpu",
                "value": "true",
                "effect": "NoSchedule"
            }]
        }
    }
}
```

當標籤或污點未正確套用至特定節點，或您需要驗證特定執行個體的組態時，節點層級監控對於故障診斷很有用。

## 預留字首
<a name="sagemaker-hyperpod-eks-custom-labels-and-taints-reserved-prefixes"></a>

某些字首保留供系統使用，不應用於自訂標籤或污點。保留下列字首：
+ `kubernetes.io/` - 預留給 Kubernetes 核心元件
+ `k8s.io/` - 預留給 Kubernetes 核心元件
+ `sagemaker.amazonaws.com/` - 預留給 SageMaker HyperPod
+ `eks.amazonaws.com/` - 預留給 Amazon EKS
+ `k8s.aws/` - 預留給 Amazon EKS
+ `karpenter.sh/` - 預留給 Karpenter 自動調整規模

具有這些字首的標籤和污點由系統元件管理，不應被自訂值覆寫。