本頁面說明如何使用 Google Cloud 機構政策服務中的自訂限制,限制貴機構中 Google Kubernetes Engine (GKE) 資源的特定作業。您可以運用限制,確保叢集資源符合特定需求,協助貴機構滿足法規遵循、安全性和政策要求。本頁說明如何建立自訂限制,並將其套用至叢集資源。
本頁內容適用於安全專家,他們負責確保機構符合法規遵循、安全性和政策規定,方法是限制或要求叢集資源的特定設定。如要進一步瞭解 Google Cloud 內容中提及的常見角色和範例工作,請參閱常見的 GKE Enterprise 使用者角色和工作。
閱讀本頁面之前,請先熟悉機構政策。
關於機構政策和限制
Google Cloud 機構政策可讓您透過程式以集中方式控管機構的資源。身為機構政策管理員,您可以定義機構政策,也就是一組稱為限制的限制,適用於Google Cloud 資源階層中的 Google Cloud 資源和這些資源的子系。您可以在機構、資料夾或專案層級強制執行機構政策。
機構政策提供各種 Google Cloud 服務的預先定義限制。不過,如要進一步自訂組織政策中受限的特定欄位,也可以建立自訂限制,並在自訂組織政策中使用這些限制。
GKE 支援的資源
如果是 GKE,您可以在 Google Kubernetes Engine API v1 的 Cluster
或 NodePool
資源中,為 CREATE
或 UPDATE
方法的任何欄位建立自訂限制,但輸出專用欄位和下列欄位除外:
projects.locations.clusters.masterAuth.clientKey
projects.locations.clusters.masterAuth.password
政策繼承
根據預設,政策會由您強制執行政策的資源子系繼承。舉例來說,如果您對資料夾強制執行政策,Google Cloud 會對資料夾中的所有專案強制執行政策。如要進一步瞭解這項行為及如何變更,請參閱「階層評估規則」。
定價
機構政策和限制免費提供。
事前準備
開始之前,請確認你已完成下列工作:
- 啟用 Google Kubernetes Engine API。 啟用 Google Kubernetes Engine API
- 如要使用 Google Cloud CLI 執行這項工作,請安裝並初始化 gcloud CLI。如果您先前已安裝 gcloud CLI,請執行
gcloud components update
,取得最新版本。
-
如要取得建立限制和強制執行組織政策所需的權限,請要求管理員為您授予組織的組織政策管理員 (
roles/orgpolicy.policyAdmin
) 身分與存取權管理角色。 Google Cloud 如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。 - 請確認您知道機構 ID。
建立自訂限制
如要建立新的自訂限制,請在 YAML 檔案中定義限制,然後使用 Google Cloud CLI 在機構中套用自訂限制。
建立自訂限制的 YAML 檔案:
name: organizations/ORGANIZATION_ID/customConstraints/custom.CONSTRAINT_NAME resourceTypes: - container.googleapis.com/RESOURCE_NAME methodTypes: - METHOD1 - METHOD2 condition: "resource.OBJECT_NAME.FIELD_NAME == VALUE" actionType: ACTION displayName: DISPLAY_NAME description: DESCRIPTION
更改下列內容:
ORGANIZATION_ID
:您的機構 ID,例如123456789
。CONSTRAINT_NAME
:新自訂限制的名稱。自訂限制的開頭必須為custom.
,且只能包含大寫英文字母、小寫英文字母或數字,例如custom.enableGkeAutopilot
。這個欄位的長度上限為 70 個字元,不含前置字元,例如organizations/123456789/customConstraints/custom.
。RESOURCE_NAME
:GKE API REST 資源的名稱 (而非 URI),其中包含您要限制的物件和欄位。例如Cluster
或NodePool
。
METHOD1,METHOD2,...
:要強制執行限制的 RESTful 方法清單。可以是CREATE
或CREATE
和UPDATE
。condition
:用來驗證要求的條件,以一般運算語言 (CEL) 撰寫。這個欄位的長度上限為 1000 個字元。運算式必須包含下列欄位,並支援&&
和||
等邏輯運算子:OBJECT_NAME
:要限制的 GKE API 物件名稱,格式為 PascalCase。例如:privateClusterConfig
。FIELD_NAME
:要限制的 GKE API 欄位名稱,格式為 PascalCase。例如:enablePrivateNodes
。VALUE
:欄位的值。如果是布林欄位,請使用true
或false
。如果是字串欄位,請使用"STRING"
。
ACTION
:如果符合condition
,則要採取的動作。可以是ALLOW
或DENY
。DISPLAY_NAME
:限制條件的易記名稱。這個欄位的長度上限為 200 個字元。DESCRIPTION
:違反政策時,要以錯誤訊息形式顯示的限制說明,方便使用者閱讀。這個欄位的長度上限為 2000 個字元。
套用自訂限制:
gcloud org-policies set-custom-constraint PATH_TO_FILE
請將
PATH_TO_FILE
替換為自訂限制定義的檔案路徑。確認自訂限制是否存在:
gcloud org-policies list-custom-constraints --organization=ORGANIZATION_ID
輸出結果會與下列內容相似:
CONSTRAINT LIST_POLICY BOOLEAN_POLICY ETAG custom.enableGkeAutopilot - SET COCsm5QGENiXi2E= ...
強制執行自訂限制
如要強制執行新的自訂限制,請建立參照該限制的機構政策,然後套用該政策。
建立機構組織政策的 YAML 檔案:
name: RESOURCE_HIERARCHY/policies/POLICY_NAME spec: rules: - enforce: true
更改下列內容:
RESOURCE_HIERARCHY
:新政策的適用範圍,這會影響強制執行的範圍。請參考Google Cloud 資源階層。舉例來說,如要在特定專案中強制執行政策,請使用projects/PROJECT_ID
。如要在特定機構中強制執行政策,請使用organizations/ORGANIZATION_ID
。POLICY_NAME
:新政策的名稱。
強制執行政策:
gcloud org-policies set-policy PATH_TO_POLICY
請將
PATH_TO_POLICY
替換為政策定義檔案的路徑。確認政策存在:
gcloud org-policies list \ --RESOURCE_FLAG=RESOURCE_ID
更改下列內容:
RESOURCE_FLAG
:您強制執行政策的 Google Cloud 資源。例如project
或folder
。RESOURCE_ID
:您強制執行政策的資源 ID。例如資料夾 ID。 Google Cloud
如需引數清單,請參閱
gcloud org-policies list
。輸出結果會與下列內容相似:
CONSTRAINT LIST_POLICY BOOLEAN_POLICY ETAG iam.disableWorkloadIdentityClusterCreation - SET CO3UkJAGEOj1qsQB custom.enableGkeAutopilot - SET COCsm5QGENiXi2E= custom.enableBinAuth - SET CJfKiZUGEJju7LUD
範例:建立自訂限制並強制執行政策
以下範例會建立自訂限制和政策,要求特定專案中的所有新叢集都必須是 Autopilot 叢集。
開始之前,請先瞭解下列事項:
- 您的機構 ID
- 專案 ID
建立限制
將下列檔案儲存為
constraint-enable-autopilot.yaml
:name: organizations/ORGANIZATION_ID/customConstraints/custom.enableGkeAutopilot resourceTypes: - container.googleapis.com/Cluster methodTypes: - CREATE condition: "resource.autopilot.enabled == false" actionType: DENY displayName: Enable GKE Autopilot description: All new clusters must be Autopilot clusters.
這項限制定義了每個新叢集,如果叢集模式不是 Autopilot,就會拒絕作業。
套用限制:
gcloud org-policies set-custom-constraint ~/constraint-enable-autopilot.yaml
確認限制條件是否存在:
gcloud org-policies list-custom-constraints --organization=ORGANIZATION_ID
輸出結果會與下列內容相似:
CUSTOM_CONSTRAINT ACTION_TYPE METHOD_TYPES RESOURCE_TYPES DISPLAY_NAME custom.enableGkeAutopilot DENY CREATE container.googleapis.com/Cluster Enable GKE Autopilot ...
建立政策
將下列檔案儲存為
policy-enable-autopilot.yaml
:name: projects/PROJECT_ID/policies/custom.enableGkeAutopilot spec: rules: - enforce: true
將
PROJECT_ID
替換為您的專案 ID。套用政策:
gcloud org-policies set-policy ~/policy-enable-autopilot.yaml
確認政策存在:
gcloud org-policies list --project=PROJECT_ID
輸出結果會與下列內容相似:
CONSTRAINT LIST_POLICY BOOLEAN_POLICY ETAG custom.enableGkeAutopilot - SET COCsm5QGENiXi2E=
套用政策後,請等待約兩分鐘, Google Cloud 就會開始強制執行政策。
測試政策
嘗試在專案中建立 GKE Standard 叢集:
gcloud container clusters create org-policy-test \
--project=PROJECT_ID \
--location=CONTROL_PLANE_LOCATION \
--num-nodes=1
更改下列內容:
PROJECT_ID
:政策所屬專案的專案 ID。CONTROL_PLANE_LOCATION
:叢集控制層的 Compute Engine 位置。為地區叢集提供地區,或為區域叢集提供區域。
輸出內容如下:
Operation denied by custom org policies: ["customConstraints/custom.enableGkeAutopilot": "All new clusters must be Autopilot clusters."]
探索常見用途的自訂限制範例
以下範例提供一些實用的自訂限制語法,例如在新叢集上強制執行 GKE 適用的工作負載身分聯盟。如要使用這些範例,請視需要修改範例,以符合特定用途。 然後按照本頁的指示,將這些設定套用至機構。
說明 | 限制語法 |
---|---|
只允許在啟用二進位授權時建立叢集 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.gkeBinaryAuthorization resourceTypes: - container.googleapis.com/Cluster methodTypes: - CREATE condition: "resource.binaryAuthorization.enabled == true || resource.binaryAuthorization.evaluationMode=='PROJECT_SINGLETON_POLICY_ENFORCE'" action: ALLOW displayName: Enable GKE Binary Authorization description: All new clusters must enable Binary Authorization. |
請勿為新的節點集區停用節點自動升級功能 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.enableAutoUpgrade resourceTypes: - container.googleapis.com/NodePool methodTypes: - CREATE condition: "resource.management.autoUpgrade == true" actionType: ALLOW displayName: Enable node auto-upgrade description: All node pools must have node auto-upgrade enabled. |
為新叢集啟用 Workload Identity Federation for GKE |
name: organizations/ORGANIZATION_ID/customConstraints/custom.enableWorkloadIdentity resourceTypes: - container.googleapis.com/Cluster methodTypes: - CREATE condition: "has(resource.workloadIdentityConfig.workloadPool) || resource.workloadIdentityConfig.workloadPool.size() > 0" actionType: ALLOW displayName: Enable Workload Identity on new clusters description: All new clusters must use Workload Identity. |
請勿在現有叢集上停用 Cloud Logging |
name: organizations/ORGANIZATION_ID/customConstraints/custom.enableLogging resourceTypes: - container.googleapis.com/Cluster methodTypes: - UPDATE condition: "resource.loggingService == 'none'" actionType: DENY displayName: Do not disable Cloud Logging description: You cannot disable Cloud Logging on existing GKE cluster. |
僅在停用舊版中繼資料端點時,允許建立或更新標準節點集區 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.nodeConfigMetadata resourceTypes: - container.googleapis.com/NodePool methodTypes: - CREATE - UPDATE condition: "'disable-legacy-endpoints' in resource.config.metadata && resource.config.metadata['disable-legacy-endpoints'] == 'true'" actionType: ALLOW displayName: Disable legacy metadata endpoints description: You can only create or update node pools if you disable legacy metadata endpoints. 這個限制條件範例說明如何對地圖值設定自訂限制條件。 |