本頁面說明如何使用自訂 ComputeClass,根據工作負載的特定需求,控管 Google Kubernetes Engine (GKE) 叢集的運算基礎架構和自動調度行為。您應該已熟悉自訂 ComputeClass 的概念。詳情請參閱「關於自訂 ComputeClass」。
如果您是平台管理員,想以宣告方式定義節點的自動調度設定檔,或是叢集運算子想在特定 ComputeClass 上執行工作負載,歡迎參閱這個頁面。
關於自訂 ComputeClass
自訂 ComputeClass 是 Kubernetes Custom Resources,可讓您定義 GKE 在佈建節點以執行工作負載時遵循的優先順序。您可以使用自訂 ComputeClass 執行下列操作:
- 為 GKE 提供一組優先順序,以便在佈建節點時依序遵循,每個節點都有特定參數,例如 Compute Engine 機器系列或最低資源容量
- 定義自動調度資源的門檻和參數,以便移除未充分利用的節點,並在現有運算容量上有效整合工作負載
- 指示 GKE 自動將較不適合的節點設定,替換為更適合的節點設定,以獲得最佳工作負載效能
如要瞭解所有設定選項,以及這些選項如何彼此互動,並與 GKE Autopilot 模式和 GKE Standard 模式互動,請參閱「關於自訂 ComputeClass」。
定價
在 GKE 中,ComputeClass
自訂資源免費提供。請注意下列價格注意事項:
GKE Autopilot 模式:系統會採用以節點為準的計費模式。詳情請參閱 Autopilot 模式定價。
GKE Standard 模式:請參閱標準模式定價。
事前準備
開始之前,請確認您已完成下列工作:
- 啟用 Google Kubernetes Engine API。 啟用 Google Kubernetes Engine API
- 如要使用 Google Cloud CLI 執行這項工作,請安裝並初始化 gcloud CLI。如果您先前已安裝 gcloud CLI,請執行
gcloud components update
,取得最新版本。
- 請確認您現有的 GKE 叢集執行的是 1.30.3-gke.1451000 以上版本。詳情請參閱「建立 Autopilot 叢集」。
- 如果您使用標準模式叢集,請確保至少有一個節點集區已啟用自動調度資源,或叢集使用節點自動佈建。
ComputeClasses 的情境範例
本頁提供範例情境,說明如何定義自訂運算類別。在實務上,您應考量特定工作負載和機構的需求,並定義符合這些需求的 ComputeClass。如需 ComputeClasses 所有選項的完整說明和特殊考量事項,請參閱「關於自訂 ComputeClasses」。
請參考下列情境範例:
- 您的目標是盡可能提高工作負載的執行成本效益
- 工作負載具備容錯能力,不需要安全關機或延長執行時間
- 工作負載至少需要 64 個 vCPU,才能以最佳狀態執行
- 您只能使用 N4 Compute Engine 機器系列
根據範例情境,您決定要建立可執行下列動作的 ComputeClass:
- 優先使用至少有 64 個 vCPU 的 N4 Spot 節點
- 讓 GKE 回退至任何 N4 Spot 節點,無論運算容量為何
- 如果沒有可用的 N4 Spot 節點,請讓 GKE 使用隨選 N4 節點
- 指示 GKE 在 Spot 節點再次可用時,將工作負載移至 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
在標準模式中設定 ComputeClass
在 GKE Standard 模式叢集中,您定義運算類別後,可能必須手動設定,確保 ComputeClass Pod 會如預期排程。手動設定取決於節點集區是否自動佈建,如下所示:
- 由節點自動佈建功能管理的節點集區:無須手動設定。節點自動佈建功能會自動為您執行 ComputeClass 設定步驟。詳情請參閱節點自動佈建和 ComputeClass。
- 手動建立的節點集區:必須手動設定。您必須在手動建立的節點集區中新增節點標籤和節點汙點,才能將節點與特定 ComputeClass 建立關聯。詳情請參閱「為使用 ComputeClass 手動建立節點集區」。
搭配手動建立的節點集區使用 ComputeClasses
本節說明如何在僅使用手動建立節點集區的叢集中定義 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
:現有叢集的名稱。
使用隨選 VM 建立新的自動調度節點集區,並將其與 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」。
搭配自動佈建節點集區使用 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
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
如要進一步瞭解節點自動佈建如何搭配自訂運算類別運作,請參閱節點自動佈建和 ComputeClasses。
自訂節點整併的自動調度門檻
根據預設,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,請在資訊清單中為該運算類別新增節點選取器。
將下列資訊清單儲存為
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: gcr.io/google_containers/pause resources: requests: cpu: 1.5 memory: "4Gi"
部署工作負載:
kubectl apply -f cc-workload.yaml
部署這項工作負載時,GKE 會自動將容許條件新增至 Pod,對應所要求 ComputeClass 的節點 taint。這項容許度可確保只有要求運算類別的 Pod 會在 ComputeClass 節點上執行。
更新已部署的 ComputeClass
如要更新已部署的 ComputeClass,請修改 ComputeClass 的 YAML 資訊清單。然後執行下列指令,部署修改後的資訊清單:
kubectl apply -f PATH_TO_FILE
將 PATH_TO_FILE
替換為修改後資訊清單的路徑。確認 name
欄位中的值維持不變。
部署更新後的 ComputeClass 時,GKE 會使用更新後的設定建立新節點。GKE 不會使用更新後的設定修改任何現有節點。
如果 ComputeClass 使用主動遷移,且現有 Pod 符合遷移資格,GKE 可能會將現有 Pod 遷移至使用更新設定的節點。