您可以啟用預設 ComputeClass、Autopilot 叢集安全性政策,以及叢集將資源調度率降至零等叢集設定,讓 Google Kubernetes Engine (GKE) Standard 叢集的運作方式與 Autopilot 模式叢集類似。
本文適用對象為平台管理員和資安工程師,他們希望在 Standard 叢集中享有 Autopilot 模式的所有優點,並能靈活修改或停用個別設定。如果您不需要這項彈性,請改為建立專屬 Autopilot 叢集。
您應該已熟悉下列概念:
Autopilot 叢集設定
如要將 Standard 叢集設為與 Autopilot 叢集相同,請調整下列所有設定:
- Autopilot ComputeClass 做為叢集預設值:設定其中一個內建 Autopilot ComputeClass。未選取 ComputeClass 的 Pod 會使用指定的預設 ComputeClass 執行。內建的 ComputeClass 都使用 Autopilot 模式和 Pod 計費模式。
- 將叢集自動調度資源至零個節點:設定叢集自動配置器,在叢集中沒有任何有效使用者工作負載時,將叢集縮減至零個節點。您可以視需要調整延遲時間,讓自動配置器在延遲時間過後,將叢集縮減至零個節點。
- 零初始節點:建立叢集時,防止系統自動建立預設節點集區。如果是現有叢集,請刪除所有手動建立的節點集區。
- Autopilot 叢集安全性政策:強制執行所有 Autopilot 安全性政策。這些政策有助於保護系統工作負載和身分。
您可以修改叢集,單獨變更其中任何設定,而不影響其他設定。舉例來說,如果您需要手動建立節點集區,可以停用對應的 Autopilot 叢集安全性政策,而不變更任何其他設定。
彈性和安全性考量
建立 Autopilot 叢集後,叢集模式即無法變更。叢集設定 (例如將資源調度率降至零節點、禁止手動建立節點集區,以及各種安全性限制) 均不可變更。不過,如果將 Standard 叢集設為使用 Autopilot 叢集的所有設定,則可變更這些設定。凡是有權修改叢集設定的使用者,都能變更或停用個別設定。
如要在整個組織、資料夾或專案中強制執行這些設定,組織管理員可以使用自訂組織政策限制。詳情請參閱本文的「使用自訂組織政策強制執行 Autopilot 設定」一節。
限制
如果您在叢集層級設定機密 GKE 節點,GKE 會忽略使用 Autopilot ComputeClass 做為叢集預設值的指令。
事前準備
開始之前,請確認您已執行下列工作:
- 啟用 Google Kubernetes Engine API。 啟用 Google Kubernetes Engine API
- 如要使用 Google Cloud CLI 執行這項工作,請安裝並初始化 gcloud CLI。如果您先前已安裝 gcloud CLI,請執行
gcloud components update指令,取得最新版本。較舊的 gcloud CLI 版本可能不支援執行本文件中的指令。
- 確認您有符合所有需求條件的標準叢集。
需求條件
如要在 Standard 叢集使用所有 Autopilot 功能,叢集必須符合下列規定:
叢集必須執行 GKE 1.37.1-gke.3020000 以上版本。個別設定的版本需求如下:
- 如要將內建 Autopilot ComputeClass 設為叢集預設值,叢集必須執行 GKE 1.37.1-gke.1380000 以上版本。
- 如要建立初始節點數量為零的叢集,叢集必須執行 GKE 1.37.1-gke.1153000 以上版本。
- 如要設定叢集自動配置器,將叢集縮減至零個節點,叢集必須執行 GKE 1.37.1-gke.3020000 以上版本。
如要將叢集升級至相容版本,請參閱「手動升級叢集的控制層或節點集區」。
叢集必須符合使用 Autopilot ComputeClass 的所有需求條件。如要確認叢集是否符合這些需求,請參閱「在 Standard 叢集以 Autopilot 模式執行工作負載」。
必要角色和權限
如要取得建立或更新叢集,以便使用 Autopilot 模式所需的權限,請要求管理員在 Google Cloud 專案中授予您下列 IAM 角色:
-
建立及更新叢集和節點集區:Kubernetes Engine 叢集管理員 (
roles/container.clusterAdmin) -
管理叢集中的 ComputeClass 和工作負載:Kubernetes Engine 開發人員 (
roles/container.developer)
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
建立使用 Autopilot 模式的 Standard 叢集
如要建立使用所有 Autopilot 設定的新 Standard 叢集,請執行下列指令:
gcloud container clusters create CLUSTER_NAME \
--location=CONTROL_PLANE_LOCATION \
--num-nodes=0 \
--default-compute-class=BUILT_IN_COMPUTECLASS_NAME \
--enable-cluster-scale-to-zero \
--autopilot-cluster-policies=no-system-mutation,no-system-impersonation,no-unsafe-webhooks,no-standard-node-pools
更改下列內容:
CLUSTER_NAME:新叢集的名稱。CONTROL_PLANE_LOCATION:叢集控制層的區域,例如us-central1。BUILT_IN_COMPUTECLASS_NAME:要用作叢集預設值的內建 ComputeClass,例如autopilot。
您也可以視需要使用 --cluster-scale-to-zero-delay 旗標,調整叢集自動配置器將叢集縮減至零個節點前的延遲時間:
gcloud container clusters create CLUSTER_NAME \
--location=CONTROL_PLANE_LOCATION \
--num-nodes=0 \
--default-compute-class=BUILT_IN_COMPUTECLASS_NAME \
--enable-cluster-scale-to-zero \
--cluster-scale-to-zero-delay=SCALE_TO_ZERO_DELAY \
--autopilot-cluster-policies=no-system-mutation,no-system-impersonation,no-unsafe-webhooks,no-standard-node-pools
將 SCALE_TO_ZERO_DELAY 替換為延遲時間長度。這個值必須指定整數和時間單位,例如 0s、1m 或 3h。延遲時間上限為 3 小時。如未指定 --cluster-scale-to-zero-delay 旗標,預設延遲時間為 3 小時。
如果叢集建立指令失敗並顯示錯誤訊息,請查看錯誤訊息瞭解詳情。最常見的錯誤如下:
- 叢集使用的版本不支援其中一個指定旗標。如要解決這項錯誤,請將叢集更新至「需求條件」一節中指定的最低必要版本。
- 您在叢集建立指令中指定了節點集區設定旗標 (例如
--machine-type旗標)。由於新叢集沒有初始節點集區,因此您無法指定節點集區設定旗標。如要解決這項錯誤,請從指令中移除節點集區設定旗標。 - 您為
--default-compute-class旗標指定了自訂 ComputeClass 的名稱。如要解決這項錯誤,請指定其中一個內建 Autopilot ComputeClass。
更新現有的 Standard 叢集,改用 Autopilot 模式
您可以修改現有的 Standard 叢集,改用 Autopilot 模式。這項程序可能會對工作負載造成干擾,例如刪除手動建立的節點集區。請在排定的維護期間執行這些步驟。
確認叢集符合本文件中的所有需求條件。
請檢查叢集並進行必要變更,以免與 Autopilot 叢集政策發生衝突。如要進一步瞭解可能發生的變更,請參閱「支援的政策」。
將 Autopilot ComputeClass 設為叢集預設值後,未選取其他 ComputeClass 的傳入 Pod 會成為 Autopilot 工作負載。檢查是否有任何工作負載未針對下列屬性選取 ComputeClass:
- 確認工作負載符合 Autopilot 安全性限制,或為特殊權限工作負載設定許可清單。
- 找出並移除 Pod 的
cloud.google.com/gke-nodepool節點選取器或節點親和性規則。 - 檢查是否有任何命名空間具有
cloud.google.com/default-compute-class或cloud.google.com/default-compute-class-non-daemonset標籤。命名空間層級預設 ComputeClass 的優先順序高於叢集層級預設 ComputeClass。
檢查叢集是否有自訂 ComputeClass,名稱為
default:kubectl get computeclass default -o yaml如果輸出內容空白,請跳到下一個步驟。如果
defaultComputeClass 存在,輸出內容會大致如下:apiVersion: cloud.google.com/v1 kind: ComputeClass metadata: annotations: cloud.google.com/gke-managed-default-compute-class: "true" name: default # Multiple lines are omitted here.在輸出內容中,如果
cloud.google.com/gke-managed-default-compute-class: "true"註解位於metadata.annotations欄位,請跳到下一個步驟。如果沒有這個註解,請刪除 ComputeClass:kubectl delete computeclass default在叢集中找出並刪除手動建立的節點集區。
啟用叢集的 Autopilot 設定:
將叢集預設 ComputeClass 設為內建 ComputeClass:
gcloud container clusters update CLUSTER_NAME \ --location=CONTROL_PLANE_LOCATION \ --default-compute-class=BUILT_IN_COMPUTECLASS_NAME更改下列內容:
CLUSTER_NAME:現有 Standard 叢集的名稱。CONTROL_PLANE_LOCATION:叢集控制層的區域。BUILT_IN_COMPUTECLASS_NAME:要用作叢集預設值的內建 Autopilot ComputeClass,例如autopilot。
啟用叢集自動調度資源功能,讓自動配置器將叢集縮減至零個節點:
gcloud container clusters update CLUSTER_NAME \ --location=CONTROL_PLANE_LOCATION \ --enable-autoscaling \ --enable-cluster-scale-to-zero您也可以視需要使用
--cluster-scale-to-zero-delay旗標,調整叢集自動配置器將叢集縮減至零個節點前的延遲時間:gcloud container clusters update CLUSTER_NAME \ --location=CONTROL_PLANE_LOCATION \ --enable-autoscaling \ --enable-cluster-scale-to-zero \ --cluster-scale-to-zero-delay=SCALE_TO_ZERO_DELAY將
SCALE_TO_ZERO_DELAY替換為延遲時間長度。這個值必須指定整數和時間單位,例如0s、1m或3h。延遲時間上限為 3 小時。如未指定--cluster-scale-to-zero-delay旗標,預設延遲時間為 3 小時。啟用 Autopilot 叢集政策:
gcloud container clusters update CLUSTER_NAME \ --location=CONTROL_PLANE_LOCATION \ --autopilot-cluster-policies=no-system-mutation,no-system-impersonation,no-unsafe-webhooks,no-standard-node-pools
如果任何叢集更新指令失敗並顯示錯誤訊息,請查看錯誤訊息,瞭解詳情。最常見的錯誤如下:
- 叢集使用的版本不支援其中一個指定旗標。如要解決這項錯誤,請將叢集升級至「需求條件」一節中指定的最低必要版本。
- 您為
--default-compute-class旗標指定了自訂 ComputeClass 的名稱。如要解決這項錯誤,請指定其中一個內建 Autopilot ComputeClass。 - 您的叢集有手動建立的節點集區,因此 GKE 無法強制執行
no-standard-node-pools政策。如要解決這個錯誤,請刪除手動建立的節點集區。
確認叢集使用 Autopilot 模式
設定新的或現有叢集後,請確認 Autopilot 設定已啟用:
確認叢集已啟用預設 ComputeClass、將資源調度率降至零,以及叢集政策設定:
gcloud container clusters describe CLUSTER_NAME \ --location=CONTROL_PLANE_LOCATION \ --format="yaml(autoscaling.defaultComputeClassConfig,autoscaling.scaleToZeroConfig,autopilot.clusterPolicyConfig)"輸出結果會與下列內容相似:
autoscaling: defaultComputeClassConfig: computeClass: autopilot enabled: true scaleToZeroConfig: enabled: true delay: 3h autopilot: clusterPolicyConfig: noManualNodePools: true noSystemImpersonation: true noSystemMutation: true noUnsafeWebhooks: true確認 GKE 已在叢集中建立代管
defaultComputeClass:kubectl get computeclass default -o yaml輸出結果會與下列內容相似:
如果輸出內容空白,請跳到下一個步驟。如果
defaultComputeClass 存在,輸出內容會大致如下:apiVersion: cloud.google.com/v1 kind: ComputeClass metadata: annotations: cloud.google.com/gke-managed-default-compute-class: "true" name: default # Multiple lines are omitted here. spec: autopilot: enabled: true # Multiple lines are omitted here.在這個輸出內容中,
cloud.google.com/gke-managed-default-compute-class: "true"註解表示 GKE 建立了 ComputeClass。部署 Pod 時,如果未選取其他 ComputeClass,請確認 Pod 是在 Autopilot 代管節點上執行:
kubectl get pods -o wide在輸出內容中,由 Autopilot 管理的節點在
NODE欄中會有gk3-前置字元。
修改或停用個別 Autopilot 設定
Standard 叢集中的每項 Autopilot 功能都是模組化,因此您隨時可以自訂或停用個別設定。如果您是安全性管理員,可以使用自訂組織政策限制強制執行這些設定。詳情請參閱「使用自訂組織政策強制執行 Autopilot 設定」。
- 停用叢集中的特定 Autopilot 政策,例如
no-standard-node-pools。 - 停用叢集層級預設 ComputeClass。或者,您也可以建立名為
default的 ComputeClass,將自訂 ComputeClass 設為叢集預設值。 防止叢集自動調度資源至零個節點:
gcloud container clusters update CLUSTER_NAME \ --location=CONTROL_PLANE_LOCATION \ --no-enable-cluster-scale-to-zero
使用自訂組織政策強制執行 Autopilot 設定
具備叢集更新權限的使用者可以修改或停用 Standard 叢集中的 Autopilot 設定,因此組織管理員可以使用組織政策服務中的自訂組織政策限制,在組織、資料夾或專案中強制執行這些設定。以下範例限制會拒絕建立或更新未強制執行所有 Autopilot 設定的叢集:
name: organizations/ORGANIZATION_ID/customConstraints/custom.enforceAutopilotSettings
resourceTypes:
- container.googleapis.com/Cluster
methodTypes:
- CREATE
condition: >-
"resource.ClusterPolicyConfig.no_system_mutation != true ||
resource.ClusterPolicyConfig.no_system_impersonation != true ||
resource.ClusterPolicyConfig.no_unsafe_webhooks != true ||
resource.ClusterPolicyConfig.no_standard_node_pools != true ||
resource.ScaleToZeroConfig.enabled != true ||
!(resource.DefaultComputeClassConfig.computeClass in [
'autopilot', 'autopilot-spot', 'autopilot-arm', 'autopilot-arm-spot'
])"
actionType: DENY
displayName: Enforce Autopilot cluster policies in Standard clusters
description: All new clusters must use all Autopilot settings by default.
請將 ORGANIZATION_ID 替換成組織 ID。
後續步驟
- 進一步瞭解 GKE Standard 中的 Autopilot 工作負載
- 在 Standard 叢集以 Autopilot 模式執行工作負載
- 預設將 ComputeClasses 套用至 Pod
- 在 Standard 叢集強制執行 Autopilot 安全性政策
- 自動調度叢集資源