叢集管理員和應用程式操作員可在 Standard 模式叢集中,享有 Google Kubernetes Engine (GKE) Autopilot 的優點,例如定價和預先設定。本文說明如何使用 ComputeClasses,在 Standard 叢集中部署 Autopilot 工作負載。您應該已熟悉下列概念:
關於 Autopilot ComputeClass
GKE 提供名為 ComputeClasses 的 Kubernetes 自訂資源,可像其他 Kubernetes 資源一樣部署在叢集中。ComputeClass 會定義節點設定清單,例如機型或 Spot VM。您可以在工作負載中選取 ComputeClass,向 GKE 指出任何新節點都應使用該清單中的其中一項設定。
如果工作負載選取已啟用 autopilot 欄位的 ComputeClass,GKE 會在 Autopilot 模式下執行 Pod。GKE 建立的節點由 Google 管理,並包含許多預設的 Autopilot 功能和安全性設定。如要進一步瞭解在 Standard 叢集中執行 Autopilot 工作負載的影響,包括部署這些工作負載時可能注意到的差異,請參閱「關於 GKE Standard 中的 Autopilot 模式工作負載」。
Autopilot ComputeClass 類型
GKE 提供內建的 Autopilot ComputeClass,可用於大多數一般用途工作負載。您也可以設定新的或現有的自訂 ComputeClass,以使用 Autopilot 模式。您使用的 Autopilot ComputeClass 類型取決於工作負載是否需要特定硬體,如下所示:
- 一般用途工作負載:使用其中一個內建 Autopilot ComputeClass,將 Pod 放在容器最佳化運算平台。
- 需要特定硬體的工作負載:為任何自訂 ComputeClass 啟用 Autopilot 模式,將該 ComputeClass 部署至叢集,並在工作負載中選取該 ComputeClass。
如要進一步瞭解這些選項、使用時機和各選項的定價,請參閱「Autopilot ComputeClasses 中的硬體選項」。
定價
GKE Autopilot 定價適用於使用 Autopilot ComputeClass 的工作負載和節點。適用的計價模式取決於 GKE 建立工作負載節點時,採用的 ComputeClass 優先順序規則。詳情請參閱「關於 GKE Standard 中的 Autopilot 模式工作負載」一文的「定價」一節。
事前準備
開始之前,請確認您已執行下列工作:
- 啟用 Google Kubernetes Engine API。 啟用 Google Kubernetes Engine API
- 如要使用 Google Cloud CLI 執行這項工作,請安裝並初始化 gcloud CLI。如果您先前已安裝 gcloud CLI,請執行
gcloud components update指令,取得最新版本。較舊的 gcloud CLI 版本可能不支援執行本文件中的指令。
- 使用符合所有規定的 GKE Standard 叢集。如要建立新叢集,請參閱「建立區域叢集」。
- 為避免工作負載遭拒,請瞭解 Autopilot 的需求和安全性限制。詳情請參閱「Autopilot 節點的預先定義設定」。
需求條件
叢集必須執行下列其中一個 GKE 版本:
- 如要在 ComputeClasses 中使用 Autopilot,叢集必須執行 1.34.1-gke.1829001 以上版本。
- 如要在自訂 Autopilot ComputeClass 中使用
podFamily優先順序規則,叢集必須執行 1.35.2-gke.1485000 以上版本。
叢集中至少須有一個節點集區沒有節點污點。
這個節點集區用於執行 GKE Standard 系統 Pod,這些 Pod 無法在 Standard 叢集的 Autopilot 節點上執行,因為 GKE 會在這些節點中新增污點。
必須啟用受防護的 GKE 節點,預設為啟用。
您必須使用 VPC 原生叢集。
如果您使用 Kubernetes NetworkPolicies,叢集必須使用 GKE Dataplane V2。根據預設,所有新叢集都會使用 GKE Dataplane V2。
如果叢集未使用 GKE Dataplane V2,則必須停用網路政策強制執行功能。
限制
general-purpose-armPod 系列、autopilot-arm和autopilot-arm-spotComputeClass 支援 Autopilot 叢集,以及採用 Autopilot ComputeClass 的 Standard 叢集,但版本需求不同。如要進一步瞭解版本需求,請參閱「在 Arm 架構上部署 Autopilot 工作負載」。- 如要更新叢集中的現有 ComputeClass,以便使用 Autopilot 模式,您必須以更新後的規格重新建立這些 ComputeClass。詳情請參閱「為現有自訂 ComputeClass 啟用 Autopilot」。
- 內建的 Autopilot ComputeClass 不支援為整個叢集啟用機密 GKE 節點。如果為叢集啟用機密 GKE 節點,則選取內建 Autopilot ComputeClass 的任何新 Pod 都會無限期維持在
Pending狀態。 - 不支援 Calico 網路政策強制執行功能。您必須使用 GKE Dataplane V2,或停用網路政策強制執行功能。
- ComputeClass 的名稱開頭不得為
gke或autopilot,這些是保留的前置字串。
必要角色和權限
如要取得部署 ComputeClass 所需的權限,請要求管理員授予您叢集或專案的 Kubernetes Engine 開發人員 (roles/container.developer) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
修改叢集以符合 Autopilot 需求
您可以使用 Google Cloud 控制台,檢查 Standard 叢集是否符合在 Autopilot 模式下執行工作負載的所有需求。您也可以使用 Google Cloud 控制台修改叢集,以符合這些需求。
修改現有叢集
在 Google Cloud 控制台中,前往「Kubernetes clusters」(Kubernetes 叢集) 頁面。
在要修改的叢集資料列中,依序按一下 「More actions」(更多動作) > 「Edit」(編輯)。「Cluster details」(叢集詳細資料) 頁面隨即開啟。
在「Cluster basics」(叢集基本資訊) 部分,找到「Autopilot compute class compatibility」(Autopilot 運算級別相容性) 部分。
如果這個部分顯示「Enabled」(已啟用),表示叢集已與 Autopilot 相容。請跳至「在工作負載中選取 Autopilot ComputeClass」一節。
如果「Autopilot compute class compatibility」(Autopilot 運算級別相容性) 區段顯示「Disabled」(已停用),請按一下 「Edit Autopilot compute class compatibility」(編輯 Autopilot 運算級別相容性)。
如果無法編輯這個部分,代表叢集使用的永久設定與 Autopilot 模式不相容。舉例來說,叢集建立後,您無法將其修改為虛擬私有雲原生叢集。如果無法與「Autopilot compute class compatibility」(Autopilot 運算級別相容性) 專區互動,請建立新的叢集。
在開啟的「Autopilot compute class compatibility」(Autopilot 運算級別相容性) 窗格中,查看需要變更哪些叢集設定,才能符合 Autopilot 模式的需求。
按一下「啟用 Autopilot 運算級別」。GKE 會視需要修改叢集。
修改新叢集
在 Google Cloud 控制台中,前往「Create a Kubernetes cluster」(建立 Kubernetes 叢集) 頁面。
在「Cluster basics」(叢集基本資訊) 頁面,找到「Maximize deployment options with Autopilot compute class」(使用 Autopilot 運算級別,享有最多的部署選項) 部分。本節說明必須變更哪些叢集設定,才能符合 Autopilot 模式的需求。
按一下「啟用 Autopilot 運算級別」。GKE 會視需要修改叢集。
根據需求調整其他叢集設定。如果您修改的設定會導致叢集與 Autopilot 不相容,系統會顯示警告訊息。
在工作負載中選取 Autopilot ComputeClass
只要選取使用 Autopilot 模式的 ComputeClass,即可在 Standard 叢集中以 Autopilot 模式執行工作負載。如要在 Autopilot 模式下執行工作負載,請選取下列其中一個選項:
控制台
前往 Google Cloud 控制台的 GKE「Workloads」(工作負載) 頁面。
按一下「部署」或「建立工作」。系統會顯示 Deployment 或 Job 的工作負載建立頁面。
在「節點」部分,選取「Autopilot 運算級別」。
在「Select compute class」(選取運算類別) 區段的「Compute class」(運算類別) 下拉式選單中,選取使用 Autopilot 模式的 ComputeClass。這個 ComputeClass 可以是下列任一項:
下列其中一個內建 Autopilot ComputeClass,可將一般用途工作負載放在 Autopilot 容器最佳化運算平台:
autopilotautopilot-spot
您建立的 ComputeClass,例如設定自訂 Autopilot ComputeClass 一節所述的
n4-classComputeClass。
設定並建立工作負載。
kubectl CLI
如要在工作負載中選取 Autopilot ComputeClass,請使用 cloud.google.com/compute-class 標籤的節點選取器。這個標籤與您在 GKE 中選取任何其他 ComputeClass 時使用的標籤相同。下列步驟說明如何建立選取 ComputeClass 的範例 Deployment,並驗證 Pod 是否在 Autopilot 模式下運作:
將下列 Deployment 範例儲存為
autopilot-cc-deployment.yaml:將
COMPUTE_CLASS替換為要使用的運算類別名稱。這個值可以是下列任一項:下列其中一個內建 Autopilot ComputeClass,可將一般用途工作負載放在 Autopilot 容器最佳化運算平台:
autopilotautopilot-spot
您建立的 ComputeClass,例如設定自訂 Autopilot ComputeClass 一節所述的
n4-classComputeClass。
部署工作負載:
kubectl apply -f autopilot-cc-deployment.yaml
設定自訂 Autopilot ComputeClass
您可以設定自訂 ComputeClass 來使用 Autopilot。在下列情況下,請使用自訂 Autopilot ComputeClass:
- 工作負載需要特定硬體才能以最佳狀態執行,例如 GPU 或特定 Compute Engine 機器系列。
- 您想在使用 Autopilot 容器最佳化運算平台時,調整可用區或污點等節點設定。
如果工作負載沒有這些需求,建議改用內建的 Autopilot ComputeClass。如要選取內建的 Autopilot ComputeClass,請參閱前述的「在工作負載中選取 Autopilot ComputeClass」一節。
建立新的自訂 Autopilot ComputeClass
儲存下列其中一個 ComputeClass 資訊清單範例:
選取特定機器:
apiVersion: cloud.google.com/v1 kind: ComputeClass metadata: name: n4-class spec: autopilot: enabled: true priorities: - machineFamily: n4 spot: true minCores: 16 - machineFamily: n4 spot: true - machineFamily: n4 spot: false activeMigration: optimizeRulePriority: true這個資訊清單包含下列欄位:
autopilot:為 ComputeClass 啟用 Autopilot 模式。如果您在部署至 Autopilot 叢集的 ComputeClass 中指定這個欄位,GKE 會忽略該欄位。priorities:定義三個不同 N4 機器家族設定的陣列。activeMigration:允許 GKE 在資源可用時,將 Pod 遷移至優先順序較高的設定。
使用容器最佳化運算平台,並進行修改:
apiVersion: cloud.google.com/v1 kind: ComputeClass metadata: name: general-purpose-class spec: autopilot: enabled: true priorities: - podFamily: general-purpose priorityDefaults: location: zones: ['us-central1-a','us-central1-b','us-central1-f']這個資訊清單包含下列欄位:
autopilot:為 ComputeClass 啟用 Autopilot 模式。如果您在部署至 Autopilot 叢集的 ComputeClass 中指定這個欄位,GKE 會忽略該欄位。priorities.podFamily:使用podFamily優先順序規則,在 Autopilot 容器最佳化運算平台上執行 Pod。priorityDefaults.location:定義 GKE 應在哪些可用區,為使用 ComputeClass 的 Pod 建立節點。
部署 ComputeClass:
kubectl apply -f PATH_TO_COMPUTECLASS_MANIFEST將
PATH_TO_COMPUTECLASS_MANIFEST替換為上一個步驟中 ComputeClass 資訊清單的路徑。確認 ComputeClass 是否存在:
kubectl get computeclasses輸出結果會與下列內容相似:
NAME AGE n4-class 3s
為現有的自訂 ComputeClass 啟用 Autopilot
您可以在 Standard 叢集的現有自訂 ComputeClass 中啟用 Autopilot。在 Autopilot 叢集的 ComputeClass 中啟用 Autopilot 沒有任何作用,因為整個叢集都會使用 Autopilot 模式。
為現有 ComputeClass 啟用 Autopilot 後,GKE 會使用 Autopilot 執行選取 ComputeClass 的新 Pod。如果 Standard 節點上現有的 Pod 選取 Autopilot ComputeClass,則只有在重新建立這些 Pod 時,才會使用 Autopilot。
如要更新現有的自訂 ComputeClass,以便使用 Autopilot 模式,請按照下列步驟操作:
在文字編輯器中,更新現有 ComputeClass 的資訊清單檔案,新增
spec.autopilot欄位:spec: autopilot: enabled: true將 Kubernetes API 中的現有
ComputeClass資源替換為更新後的規格:kubectl replace --force -f PATH_TO_UPDATED_MANIFEST將
PATH_TO_UPDATED_MANIFEST替換為更新後的資訊清單檔案路徑。如要觸發建立新節點,請重新建立任何使用運算類別的工作負載。
套用更新後的資訊清單後,GKE 為這個 ComputeClass 建立的所有「新」節點都會使用 Autopilot。GKE 不會修改更新前建立的任何現有節點。
確認工作負載使用 Autopilot
選取下列選項之一:
控制台
前往 Google Cloud 控制台的 GKE「Workloads」(工作負載) 頁面。
查看工作負載的「Node type」(節點類型) 欄中的值。如果工作負載使用 Autopilot 模式,這個值會是「Autopilot-managed」。
kubectl CLI
查看執行 Pod 的節點名稱:
kubectl get pods -l=app=hello -o wide
輸出結果會與下列內容相似:
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
helloweb-79b9f6f75-5wwc9 1/1 Running 0 152m 10.102.1.135 gk3-cluster-1-nap-10abc8ya1-f66c6cef-wg5g <none> <none>
helloweb-79b9f6f75-9skb9 1/1 Running 0 4d3h 10.102.0.140 gk3-cluster-1-nap-10abc8ya1-632bac02-hjl6 <none> <none>
helloweb-79b9f6f75-h7bdv 1/1 Running 0 152m 10.102.1.137 gk3-cluster-1-nap-10abc8ya1-f66c6cef-wg5g <none> <none>
在這個輸出內容中,「Node」欄中的 gk3- 前置字元表示節點是由 Autopilot 管理。
預設套用 Autopilot ComputeClass
您可以將內建或自訂 ComputeClass 設為命名空間或整個叢集的預設 ComputeClass。如果 Pod 未明確選取其他 ComputeClass,則會套用預設 ComputeClass。如果將 Autopilot ComputeClass 設為預設,除非工作負載選取其他選項,否則所有 Pod 都會以 Autopilot 模式執行。
如果您設為預設的 ComputeClass 使用 podFamily 優先順序規則,未選取其他 ComputeClass 的 Pod 就能以一般用途 Autopilot Pod 的形式執行。這個方法可讓您在叢集或命名空間中,預設使用以 Pod 為基礎的計費模式,如果許多工作負載沒有特殊硬體需求,就適合使用這個方法。
詳情請參閱「根據預設將 ComputeClass 套用至 Pod」。
後續步驟
- 瞭解 ComputeClass 的最佳做法。
- 如要瞭解可在 ComputeClasses 中指定的參數,請參閱「ComputeClass CustomResourceDefinition」。