在 GKE 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,取得最新版本。
  • 使用執行 1.33.1-gke.1107000 以上版本,並已加入 Rapid 發布管道的 GKE Standard 叢集。如要建立新叢集,請參閱建立區域叢集
  • 為避免工作負載遭到拒絕,請瞭解 Autopilot 的規定和安全性限制。詳情請參閱「Autopilot 節點的預先定義設定」。

需求條件

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

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

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

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

限制

  • 僅支援快速發布管道。
  • 如要更新叢集中的現有 ComputeClass 資源,以使用 Autopilot 模式,您必須使用更新的規格重新建立該 ComputeClass。詳情請參閱「為現有自訂 ComputeClass 啟用 Autopilot」。
  • 您無法在自己的 ComputeClass 中使用 podFamily 優先順序規則。這項規則僅適用於內建的 Autopilot ComputeClass。
  • 內建的 Autopilot ComputeClass 不支援為整個叢集啟用 Confidential GKE Nodes。如果為叢集啟用機密 GKE 節點,選取內建 Autopilot ComputeClass 的任何新 Pod 都會無限期處於 Pending 狀態。

必要角色和權限

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

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

在工作負載中選取 Autopilot ComputeClass

如要在工作負載中選取 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: "4Gi"

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

  2. 部署工作負載:

    kubectl apply -f autopilot-cc-deployment.yaml
    
  3. 如要確認 Pod 使用 Autopilot,請檢查主機節點的名稱:

    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

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

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

建立新的自訂 Autopilot ComputeClass

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

    apiVersion: cloud.google.com/v1
    kind: ComputeClass
    metadata:
      name: autopilot-n2-class
    spec:
      autopilot:
        enabled: true
      priorities:
      - machineFamily: n2
        spot: true
        minCores: 64
      - machineFamily: n2
        spot: true
      - machineFamily: n2
        spot: false
      activeMigration:
        optimizeRulePriority: true
      whenUnsatisfiable: DoNotScaleUp
    

    這個資訊清單包含下列欄位:

    • autopilot:為 ComputeClass 啟用 Autopilot 模式。如果您在部署至 Autopilot 叢集的 ComputeClass 中指定這個欄位,GKE 會忽略該欄位。
    • priorities:定義三種不同 N2 機器系列設定的陣列。
    • activeMigration:當資源可用時,GKE 會將 Pod 遷移至優先順序清單中較高的設定。
    • whenUnsatisfiable:這個欄位中的 DoNotScaleUp 值會強制 GKE 對新節點使用 priorities 欄位中的其中一項規則。如果無法滿足任何優先順序規則,GKE 會將 Pod 留在 Pending 狀態,直到資源可用為止。DoNotScaleUp 是這個欄位的預設和建議值。
  2. 部署 ComputeClass:

    kubectl apply -f autopilot-n2-class.yaml
    
  3. 確認 ComputeClass 是否存在:

    kubectl get computeclasses
    

    輸出結果會與下列內容相似:

    NAME                  AGE
    autopilot-n2-class    3s
    

為現有自訂 ComputeClass 啟用 Autopilot

您可以在現有的自訂 ComputeClass 中啟用 Autopilot,前提是這些 ComputeClass 位於 Standard 叢集中,且規格集中的 nodePoolAutoCreation.enabled 欄位設為 true。在 Autopilot 叢集的運算級別中啟用 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

  • 使用 kubectl get 指令取得叢集中的 Pod 清單:

    kubectl get pods --output=custom-columns=Pod:.metadata.name,Node:.spec.nodeName
    

    輸出結果會與下列內容相似:

    Pod                         Node
    helloweb-84c865764b-nzhxt   gk3-cluster-1-pool-3-68fc8dca-t54b
    # lines omitted for clarity
    

    節點名稱中的 gk3- 前置字串表示 Pod 是以 Autopilot 模式執行。

預設套用 Autopilot ComputeClass

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

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

後續步驟