本文說明如何使用自訂 ComputeClass,根據工作負載的特定需求,控管 Google Kubernetes Engine (GKE) 叢集的運算基礎架構和自動調度資源行為。
本文適用對象為想以宣告方式定義節點自動調度資源設定檔的平台管理員,以及想在特定 ComputeClass 執行工作負載的叢集操作員。
關於自訂 ComputeClass
自訂 ComputeClass 是 Kubernetes 自訂資源,可讓您定義 GKE 在佈建節點以執行工作負載時應遵循的優先順序。您可以使用自訂 ComputeClass 執行下列操作:
- 為 GKE 設定一組優先順序,在佈建節點時依序遵循,每個節點都有特定參數,例如 Compute Engine 機器系列或最低資源容量
- 定義自動調度資源門檻和參數,移除使用率偏低的節點,並在現有運算容量中有效整合工作負載
- 指示 GKE 自動將較不偏好的節點設定替換為較偏好的節點設定,以達到最佳工作負載效能
如要瞭解所有設定選項,以及這些選項如何與其他選項、GKE Autopilot 模式和 GKE Standard 模式互動,請參閱「關於自訂 ComputeClass」。
定價
在 GKE 中,ComputeClass 自訂資源不需額外付費。定價注意事項如下:
GKE Autopilot 模式:採用節點計費模式。詳情請參閱「Autopilot 模式定價」。
GKE Standard 模式:請參閱「Standard 模式定價」。
限制
ComputeClass 名稱開頭不得為 gke 或 autopilot。
事前準備
開始之前,請確認您已執行下列工作:
- 啟用 Google Kubernetes Engine API。 啟用 Google Kubernetes Engine API
- 如要使用 Google Cloud CLI 執行這項工作,請安裝並初始化 gcloud CLI。如果您先前已安裝 gcloud CLI,請執行
gcloud components update指令,取得最新版本。較舊的 gcloud CLI 版本可能不支援執行本文中的指令。
- 確認您有現有的 GKE 叢集,且執行 1.30.3-gke.1451000 以上版本。詳情請參閱「建立 Autopilot 叢集」。
如果您使用的是標準模式叢集,請確認您符合下列其中一項規定:
- 在叢集中至少一個節點集區啟用自動調度資源功能。
- 如果 Standard 叢集執行的版本低於 1.33.3-gke.1136000,且未註冊至快速發布管道,請啟用叢集層級的自動佈建節點功能。
ComputeClasses 情境範例
本文提供定義自訂 ComputeClass 的範例情境。在實際操作時,請考量特定工作負載和組織的需求,並定義符合這些需求的 ComputeClass。如需 ComputeClass 所有選項的完整說明和特殊考量,請參閱「關於自訂 ComputeClass」。
請參考以下範例情境:
- 您的目標是盡可能降低工作負載的執行成本
- 工作負載具備容錯能力,不需要正常關機或延長執行階段
- 工作負載至少需要 64 個 vCPU 才能以最佳狀態運作
- 只能使用 N4 Compute Engine 機器系列
根據範例情境,您決定要建立一個 ComputeClass,執行下列操作:
- 優先採用至少有 64 個 vCPU 的 N4 Spot 節點
- 允許 GKE 改用任何 N4 Spot 節點,無論運算能力為何
- 如果沒有可用的 N4 Spot 節點,則允許 GKE 使用隨選 N4 節點
- 指示 GKE 在 Spot 節點再次可用時,將工作負載移至這些節點
在 Autopilot 模式中設定 ComputeClass
在 GKE Autopilot 中,您可以定義 ComputeClass、將其部署至叢集,並在工作負載中要求該 ComputeClass。GKE 會為您執行任何節點設定步驟,例如套用標籤和污點。
將下列資訊清單儲存為 compute-class.yaml:
apiVersion: cloud.google.com/v1
kind: ComputeClass
metadata:
name: cost-optimized
spec:
priorities:
- machineFamily: n4
spot: true
minCores: 64
- machineFamily: n4
spot: true
- machineFamily: n4
spot: false
activeMigration:
optimizeRulePriority: true
nodePoolAutoCreation:
enabled: true
在 Standard 模式中設定 ComputeClass
在 GKE Standard 模式叢集中,您可能需要手動設定,確保 ComputeClass Pod 按照預期排程。手動設定取決於節點集區是否自動建立,如下所示:
- 自動建立的節點集區:不需手動設定。GKE 會自動為您執行 ComputeClass 設定步驟。詳情請參閱「自動建立節點集區和 ComputeClasses」。
- 手動建立的節點集區:必須手動設定。您必須在手動建立的節點集區中,新增節點標籤和節點污點,才能將節點與特定 ComputeClass 建立關聯。詳情請參閱「設定手動建立的節點集區,供 ComputeClass 使用」。
如要讓 GKE 自動為 ComputeClass 建立節點集區,請按照下列步驟操作:
- 如果 Standard 模式叢集執行的版本早於 1.33.3-gke.1136000,且未註冊快速發布管道,請啟用叢集層級的自動佈建節點功能。
將下列範例資訊清單儲存為
compute-class.yaml:apiVersion: cloud.google.com/v1 kind: ComputeClass metadata: name: cost-optimized spec: priorities: - machineFamily: n4 spot: true minCores: 64 - machineFamily: n4 spot: true - machineFamily: n4 spot: false activeMigration: optimizeRulePriority: true nodePoolAutoCreation: enabled: true部署要求這個範例 ComputeClass 的 Pod 時,如果需要建立新節點,GKE 會優先按照
priorities欄位中的項目順序建立節點。如有需要,GKE 會建立符合 ComputeClass 硬體需求的新節點集區。
您也可以在優先順序中指定精確的自訂機型。如要使用自訂機型,必須使用 GKE 1.33.2-gke.1111000 以上版本。下列範例會設定 ComputeClass,優先為 n4-custom-8-20480 自訂機型使用 Spot VM,並在 Spot 容量不足時,改用同類型的隨選 VM:
apiVersion: cloud.google.com/v1
kind: ComputeClass
metadata:
name: custom-machine-type
spec:
priorities:
- machineType: n4-custom-8-20480
spot: true
- machineType: n4-custom-8-20480
spot: false
nodePoolAutoCreation:
enabled: true
如要在資源受限時盡可能提高資源可用性,請定義涵蓋多個機器系列和世代的 ComputeClass。如果是無狀態或可容忍中斷的工作負載,您也可以啟用主動遷移功能,在容量恢復時將工作負載移回偏好的硬體:
apiVersion: cloud.google.com/v1
kind: ComputeClass
metadata:
name: multi-family-fallback
spec:
priorities:
- machineFamily: c4d
- machineFamily: c3d
- machineFamily: c4
- machineFamily: c3
- machineFamily: n4
- machineFamily: n2d
activeMigration:
optimizeRulePriority: true
nodePoolAutoCreation:
enabled: true
C4、C4D 和 N4 等較新的機器系列僅支援 Hyperdisk,而 N2D 等較舊的系列則使用永久磁碟。如果具狀態工作負載使用的 ComputeClass 橫跨兩個世代,請停用主動遷移,並在 GKE 1.35.3-gke.1290000 以上版本中,使用內建的 dynamic-rwo StorageClass 或自訂 StorageClass,並啟用自動選取磁碟類型 (parameters.type: dynamic)、use-allowed-disk-topology: "true" 和 volumeBindingMode: WaitForFirstConsumer。佈建新磁碟區時,GKE 會選取相容的磁碟類型,並只在相容的機器系列上排定現有磁碟區。
搭配使用 ComputeClass 與手動建立的節點集區
本節說明如何在僅使用手動建立節點集區的叢集中,定義 ComputeClass。
將下列資訊清單儲存為
compute-class.yaml:apiVersion: cloud.google.com/v1 kind: ComputeClass metadata: name: cost-optimized spec: priorities: - machineFamily: n4 spot: true minCores: 64 - machineFamily: n4 spot: true - machineFamily: n4 spot: false activeMigration: optimizeRulePriority: true建立使用 Spot VM 的新自動調度資源節點集區,並與 ComputeClass 建立關聯:
gcloud container node-pools create cost-optimized-pool \ --location=LOCATION \ --cluster=CLUSTER_NAME \ --machine-type=n4-standard-64 \ --spot \ --enable-autoscaling \ --max-nodes=9 \ --node-labels="cloud.google.com/compute-class=cost-optimized" \ --node-taints="cloud.google.com/compute-class=cost-optimized:NoSchedule"更改下列內容:
LOCATION:叢集的位置。CLUSTER_NAME:現有叢集的名稱。
建立新的自動調度資源節點集區,並與 ComputeClass 建立關聯:
gcloud container node-pools create on-demand-pool \ --location=LOCATION \ --cluster=CLUSTER_NAME \ --machine-type=n4-standard-64 \ --enable-autoscaling \ --max-nodes=9 \ --num-nodes=0 \ --node-labels="cloud.google.com/compute-class=cost-optimized" \ --node-taints="cloud.google.com/compute-class=cost-optimized:NoSchedule"
當您部署要求這個 ComputeClass 的 Pod,且需要建立新節點時,GKE 會優先在 cost-optimized-pool 節點集區中建立節點。如果無法建立新節點,GKE 會在 on-demand-pool 節點集區中建立節點。
如要進一步瞭解手動建立的節點集區如何與自訂 ComputeClass 互動,請參閱「設定手動建立的節點集區,以便與 ComputeClass 搭配使用」。
自訂節點整併的自動調度資源門檻
根據預設,GKE 會移除未充分利用的節點,並將工作負載重新排程至其他可用節點。您可以使用 ComputeClass 定義中的 autoscalingPolicy 欄位,進一步自訂節點成為移除候選節點的門檻和時間,如以下範例所示:
apiVersion: cloud.google.com/v1
kind: ComputeClass
metadata:
name: cost-optimized
spec:
priorities:
- machineFamily: n4
spot: true
minCores: 64
- machineFamily: n4
spot: true
- machineFamily: n4
spot: false
activeMigration:
optimizeRulePriority: true
autoscalingPolicy:
consolidationDelayMinutes : 5
consolidationThreshold : 70
在這個範例中,如果節點的可用 CPU 和記憶體容量使用率低於 70% 超過五分鐘,就會成為移除候選節點。如需可用參數清單,請參閱「設定節點整併的自動調度資源參數」。
在叢集中部署 ComputeClass
定義 ComputeClass 後,請將其部署至叢集:
kubectl apply -f compute-class.yaml
這個 ComputeClass 已可在叢集中使用。您可以在 Pod 規格中要求 ComputeClass,也可以選擇將其設為特定命名空間中的預設 ComputeClass。
在工作負載中要求 ComputeClass
如要在工作負載中要求 ComputeClass,請在資訊清單中為該 ComputeClass 新增節點選取器,步驟如下:
將下列資訊清單儲存為
cc-workload.yaml:apiVersion: apps/v1 kind: Deployment metadata: name: custom-workload spec: replicas: 2 selector: matchLabels: app: custom-workload template: metadata: labels: app: custom-workload spec: nodeSelector: cloud.google.com/compute-class: cost-optimized containers: - name: test image: registry.k8s.io/pause resources: requests: cpu: 1.5 memory: "4Gi"部署工作負載:
kubectl apply -f cc-workload.yaml
部署這項工作負載時,GKE 會自動在 Pod 新增容忍度,對應至所要求 ComputeClass 的節點污點。這項容忍設定可確保只有要求 ComputeClass 的 Pod,才會在 ComputeClass 節點上執行。
依 ComputeClass 篩選節點
與 ComputeClasses 相關聯的任何節點都有 cloud.google.com/compute-class 標籤。
如要查看與特定 ComputeClass 相關聯的所有節點,請執行下列指令:
kubectl get nodes -l cloud.google.com/compute-class=COMPUTECLASS_NAME
將 COMPUTECLASS_NAME 替換為 ComputeClass 的名稱 (例如 cost-optimized)。
輸出結果會與下列內容相似:
NAME STATUS ROLES AGE VERSION
gke-cluster-1-cost-optimized-pool-f213e529-rx7d Ready <none> 5m v1.32.4-gke.1236007
gke-cluster-1-cost-optimized-pool-f213e529-ab12 Ready <none> 5m v1.32.4-gke.1236007
如要顯示叢集中與 ComputeClass 相關聯的所有節點,請執行下列指令:
kubectl get nodes -L cloud.google.com/compute-class
更新已部署的 ComputeClass
如要更新已部署的 ComputeClass,請修改 ComputeClass 的 YAML 資訊清單。接著,執行下列指令來部署修改後的資訊清單:
kubectl apply -f PATH_TO_FILE
將 PATH_TO_FILE 替換為修改後的資訊清單路徑。請確認 name 欄位中的值保持不變。
部署更新後的 ComputeClass 時,GKE 會使用更新後的設定建立新節點。GKE 不會根據更新後的設定修改任何現有節點。
如果 ComputeClass 使用主動遷移,且現有 Pod 符合遷移條件,GKE 可能會逐漸將現有 Pod 移至使用更新設定的節點。