在 Standard 叢集的 Autopilot 模式中執行工作負載

叢集管理員和應用程式運算子可以在 Standard 模式叢集中,享有 Google Kubernetes Engine (GKE) Autopilot 的優點,例如價格和預先設定的設定。本文說明如何使用 ComputeClasses 在標準叢集中部署 Autopilot 工作負載。您應該已熟悉下列概念:

關於 Autopilot 運算級別

GKE 提供名為 ComputeClasses 的 Kubernetes 自訂資源,可像其他 Kubernetes 資源一樣部署在叢集中。ComputeClass 會定義節點設定清單,例如機器類型或 Spot VM。您可以在工作負載中選取 ComputeClass,向 GKE 指出任何新節點都應使用該清單中的其中一個設定。

如果工作負載選取的 ComputeClass 已啟用 autopilot 欄位,GKE 會在 Autopilot 模式中執行 Pod。GKE 建立的節點由 Google 管理,並包含許多預設的 Autopilot 功能和安全性設定。如要進一步瞭解在 Standard 叢集中執行 Autopilot 工作負載的影響,包括部署這些工作負載時可能注意到的差異,請參閱「關於 GKE Standard 中的 Autopilot 模式工作負載」。

Autopilot 運算級別類型

GKE 提供內建的 Autopilot ComputeClass,適用於大多數一般用途工作負載。您也可以設定新的或現有的自訂 ComputeClass,以使用 Autopilot 模式。您使用的 Autopilot ComputeClass 類型取決於工作負載是否需要特定硬體,如下所示:

  • 一般用途工作負載:使用其中一個內建的 Autopilot ComputeClass,將 Pod 放置在容器最佳化運算平台上。
  • 需要特定硬體的工作負載:為任何自訂 ComputeClass 啟用 Autopilot 模式,將該 ComputeClass 部署至叢集,並在工作負載中選取該 ComputeClass。

如要進一步瞭解這些選項、使用時機和各選項的價格,請參閱「Autopilot 運算級別中的硬體選擇」。

定價

如果工作負載和節點使用 Autopilot ComputeClass,則適用 GKE Autopilot 定價。適用的價格模式取決於您使用的是內建 Autopilot ComputeClass 還是自訂 Autopilot ComputeClass。詳情請參閱「定價」一節,瞭解「About Autopilot mode workloads in GKE Standard」(關於 GKE Standard 中的 Autopilot 模式工作負載)。

事前準備

開始之前,請確認您已完成下列工作:

  • 啟用 Google Kubernetes Engine API。
  • 啟用 Google Kubernetes Engine API
  • 如要使用 Google Cloud CLI 執行這項工作,請安裝初始化 gcloud CLI。如果您先前已安裝 gcloud CLI,請執行 gcloud components update 指令,取得最新版本。較舊的 gcloud CLI 版本可能不支援執行本文中的指令。
  • 使用執行 1.33.1-gke.1107000 以上版本,並已加入 Rapid 發布管道的 GKE Standard 叢集。如要建立新叢集,請參閱「建立區域叢集」。
  • 為避免工作負載遭到拒絕,請瞭解 Autopilot 的需求和安全性限制。詳情請參閱「Autopilot 節點的預先定義設定」。

需求條件

  • 叢集中至少須有一個沒有節點 taint 的節點集區。

    這個節點集區必須執行 GKE Standard 系統 Pod,因為 GKE 會將 taint 新增至這些節點,導致這些 Pod 無法在 Standard 叢集的 Autopilot 節點上執行。

  • 受防護的 GKE 節點為必要功能,預設會啟用。

  • 您必須使用 VPC 原生叢集。

  • 如果您使用 Kubernetes NetworkPolicy,叢集必須使用 GKE Dataplane V2。根據預設,所有新叢集都會使用 GKE Dataplane V2。

    如果叢集未使用 GKE Dataplane V2,請務必停用網路政策強制執行

限制

  • 僅支援快速發布管道。
  • 如要更新叢集中的現有 ComputeClass,改用 Autopilot 模式,您必須使用更新的規格重新建立這些 ComputeClass。詳情請參閱「為現有自訂 ComputeClass 啟用 Autopilot」。
  • 您無法在自己的 ComputeClasses 中使用 podFamily 優先順序規則。這項規則僅適用於內建的 Autopilot ComputeClass。
  • 內建的 Autopilot ComputeClasses 不支援為整個叢集啟用 Confidential GKE Nodes。如果為叢集啟用機密 GKE 節點,選取內建 Autopilot ComputeClass 的任何新 Pod 都會無限期處於 Pending 狀態。
  • 系統不支援強制執行 Calico 網路政策。您必須使用 GKE Dataplane V2,或停用強制執行網路政策。
  • ComputeClass 的名稱開頭不得為 gkeautopilot,這些是保留的前置字元。

必要角色和權限

如要取得部署 ComputeClass 所需的權限,請要求管理員授予叢集或專案的 Kubernetes Engine 開發人員 (roles/container.developer) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。

您或許也能透過自訂角色或其他預先定義的角色,取得必要權限。

修改叢集,使其符合 Autopilot 需求

您可以使用 Google Cloud 主控台檢查 Standard 叢集是否符合在 Autopilot 模式下執行工作負載的所有需求。您也可以使用 Google Cloud 控制台修改叢集,以符合這些規定。

修改現有叢集

  1. 在 Google Cloud 控制台中,前往「Kubernetes clusters」(Kubernetes 叢集) 頁面。

    前往 Kubernetes 叢集

  2. 在要修改的叢集所在資料列中,依序按一下 「更多動作」> 「編輯」。「叢集詳細資料」頁面隨即開啟。

  3. 在「叢集基本資訊」部分中,找到「Autopilot 運算級別相容性」部分。

    如果這個部分顯示「已啟用」,代表叢集已與 Autopilot 相容。請跳至「在工作負載中選取 Autopilot ComputeClass」一節。

  4. 如果「Autopilot 運算級別相容性」部分顯示「已停用」,請按一下 「編輯 Autopilot 運算級別相容性」

    如果無法編輯這個部分,表示叢集使用的永久設定與 Autopilot 模式不相容。舉例來說,叢集建立後,您就無法修改叢集,使其成為虛擬私有雲原生叢集。如果無法與「Autopilot 運算級別相容性」部分互動,請建立新的叢集

  5. 在開啟的「Autopilot 運算級別相容性」窗格中,查看需要變更的叢集設定,以符合 Autopilot 模式的需求。

  6. 按一下「啟用 Autopilot 運算級別」。GKE 會視需要修改叢集。

修改新叢集

  1. 在 Google Cloud 控制台中,前往「Create a Kubernetes cluster」(建立 Kubernetes 叢集) 頁面。

    前往「建立 Kubernetes 叢集」

  2. 在「叢集基本資訊」頁面中,找到「使用 Autopilot 運算類別,充分擴充部署選項」部分。本節說明需要變更的叢集設定,以符合 Autopilot 模式的需求。

  3. 按一下「啟用 Autopilot 運算級別」。GKE 會視需要修改叢集。

  4. 根據需求設定其他叢集設定。如果修改的設定會導致叢集與 Autopilot 不相容,系統會顯示警告訊息。

在工作負載中選取 Autopilot ComputeClass

如要在 Standard 叢集中以 Autopilot 模式執行工作負載,請選取使用 Autopilot 模式的 ComputeClass。如要在 Autopilot 模式中執行工作負載,請選取下列其中一個選項:

控制台

  1. 前往 Google Cloud 控制台的 GKE「Workloads」(工作負載) 頁面。

    前往「Workloads」(工作負載)

  2. 按一下「Deploy」(部署) 或「Create Job」(建立工作)。系統會顯示「Deployment」或「Job」的工作負載建立頁面。

  3. 在「節點」部分,選取「Autopilot 運算級別」

  4. 在「選取運算類別」部分,於「運算類別」下拉式清單中,選取使用 Autopilot 模式的 ComputeClass。這個 ComputeClass 可以是下列任一項目:

  5. 設定及建立工作負載。

kubectl CLI

如要在工作負載中選取 Autopilot ComputeClass,請使用 cloud.google.com/compute-class 標籤的節點選取器。這個標籤與您在 GKE 中選取任何其他 ComputeClass 時使用的標籤相同。下列步驟說明如何建立選取 ComputeClass 的範例 Deployment,並確認 Pod 在 Autopilot 模式下執行:

  1. 將下列 Deployment 範例儲存為 autopilot-cc-deployment.yaml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: helloweb
      labels:
        app: hello
    spec:
      selector:
        matchLabels:
          app: hello
      template:
        metadata:
          labels:
            app: hello
        spec:
          nodeSelector:
            # Replace with the name of a compute class
            cloud.google.com/compute-class: COMPUTE_CLASS 
          containers:
          - name: hello-app
            image: us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0
            ports:
            - containerPort: 8080
            resources:
              requests:
                cpu: "250m"
                memory: "1Gi"

    COMPUTE_CLASS 替換為要使用的運算類別名稱。這個值可以是下列任一項目:

  2. 部署工作負載:

    kubectl apply -f autopilot-cc-deployment.yaml
    

設定自訂 Autopilot ComputeClass

您可以設定自訂 ComputeClass,以便使用 Autopilot。如果工作負載需要特定硬體 (例如 GPU 或特定 Compute Engine 系列機器) 才能以最佳狀態執行,請使用自訂 Autopilot ComputeClass。

如果工作負載不需要特定硬體,建議改用內建的 Autopilot ComputeClass。如要選取內建的 Autopilot ComputeClass,請參閱前一節「在工作負載中選取 Autopilot ComputeClass」。

建立新的自訂 Autopilot ComputeClass

  1. 將下列範例 ComputeClass 資訊清單儲存為 n4-class.yaml

    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 遷移至優先順序清單中較高的設定。
  2. 部署 ComputeClass:

    kubectl apply -f n4-class.yaml
    
  3. 確認 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 模式,請按照下列步驟操作:

  1. 在文字編輯器中,更新現有 ComputeClass 的資訊清單檔案,新增 spec.autopilot 欄位:

    spec:
      autopilot:
        enabled: true
    
  2. 使用更新後的規格,取代 Kubernetes API 中現有的 ComputeClass 資源:

    kubectl replace --force -f PATH_TO_UPDATED_MANIFEST
    

    請將 PATH_TO_UPDATED_MANIFEST 換成更新後資訊清單檔案的路徑。

  3. 如要觸發建立新節點,請重新建立使用運算類別的任何工作負載。

套用更新後的資訊清單後,GKE 為這個 ComputeClass 建立的節點都會使用 Autopilot。GKE 不會修改更新前建立的任何現有節點。

確認工作負載使用 Autopilot

選取下列選項之一:

控制台

  1. 前往 Google Cloud 控制台的 GKE「Workloads」(工作負載) 頁面。

    前往「Workloads」(工作負載)

  2. 請檢查工作負載的「節點類型」欄中的值。如果工作負載使用 Autopilot 模式,這個值會是 Autopilot 管理

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>

在這個輸出內容中,「節點」欄中的 gk3- 前置字元表示節點是由 Autopilot 管理。

預設套用 Autopilot ComputeClass

GKE 可讓您將 ComputeClass 設為命名空間的預設值。如果命名空間中的 Pod 未明確選取其他 ComputeClass,系統就會套用命名空間預設類別。將 Autopilot ComputeClass 設為預設值,表示命名空間中的所有 Pod 預設都會以 Autopilot 模式執行,除非工作負載選取其他選項。

詳情請參閱「為命名空間設定預設 ComputeClass」。

後續步驟