安裝 Policy Controller

本頁說明如何安裝 Policy Controller。Policy Controller 會檢查、稽核叢集,並強制叢集遵循與安全性、法規或商業規則相關的政策。

如果您是 IT 管理員和營運人員,想確保雲端平台中執行的所有資源都符合機構的法規遵循規定,請參閱本頁面,瞭解如何提供及維護自動化稽核或強制執行功能。如要進一步瞭解我們在內容中提及的常見角色和範例工作,請參閱「常見的 GKE Enterprise 使用者角色和工作」。 Google Cloud

如果您使用 Google Kubernetes Engine (GKE) Enterprise 版,即可使用 Policy Controller。詳情請參閱 Google Kubernetes Engine (GKE) Enterprise 版定價

事前準備

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

  1. 安裝並初始化 Google Cloud CLI,其中提供這些操作說明中使用的 gcloudkubectlnomos 指令。如果您先前已安裝 gcloud CLI,請執行 gcloud components update,取得最新版本。如果使用 Cloud Shell,Google Cloud CLI 會預先安裝。
  2. 請確認叢集未安裝開放原始碼 Open Policy Agent Gatekeeper。如果是,請先解除安裝 Gatekeeper,再安裝 Policy Controller。

  3. 啟用必要的 API:

    主控台

    1. 啟用 GKE Enterprise API

      啟用 GKE Enterprise API

    2. 啟用 Policy Controller API。

      啟用 Policy Controller API

    gcloud

    執行下列指令:

    gcloud services enable anthos.googleapis.com anthospolicycontroller.googleapis.com
    
  4. 建立或確認您有權存取執行 Kubernetes 1.14.x 以上版本的叢集。Policy Controller 可能會顯示在 Kubernetes 1.14.x 之前的版本上執行,但產品無法正常運作。

  5. 將必要的 IAM 角色授予註冊叢集的使用者

  6. 如果您打算使用 Google Cloud CLI 設定 Policy Controller,請立即向機群註冊叢集。如要使用 Google Cloud 控制台,請在安裝 Policy Controller 時註冊叢集。

  7. 如果您使用 GKE 連結的叢集,請確保 AKS 叢集沒有 Azure Policy 外掛程式,並避免使用 control-plane 鍵標記命名空間。

  8. 如果您在 VMware 或裸機上使用 Google Distributed Cloud,請務必在使用者叢集上安裝 Policy Controller。Policy Controller 無法安裝在管理叢集上。

安裝 Policy Controller

控制台

如要在 Google Cloud 控制台中安裝 Policy Controller,請完成下列步驟:

  1. 在 Google Cloud 控制台中,前往「Posture Management」(狀態管理) 專區下方的 GKE Enterprise「Policy」(政策) 頁面。

    前往「政策」

  2. 按一下「設定 Policy Controller」

  3. 選用:如要變更預設的車隊設定,請按一下「自訂車隊設定」。然後在隨即顯示的對話方塊中,執行以下操作:

    1. 在「新增/編輯政策套裝組合」部分,按一下相關切換按鈕,即可納入或排除政策套裝組合。
    2. 在「編輯 Policy Controller 設定」部分中,執行下列操作:

      1. 如要啟用異動 Webhook,請勾選「啟用異動 Webhook」核取方塊。這項功能與 Autopilot 叢集不相容。
      2. 在「稽核間隔」方塊中,輸入連續兩項稽核作業之間的間隔秒數。
      3. 在「Exemptible namespaces」(可豁免的命名空間) 方塊中,輸入命名空間清單。 政策控制器會忽略這些命名空間中的物件。這項功能不適用於 Autopilot 叢集。

        最佳做法

        豁免系統命名空間,避免環境發生錯誤。如要瞭解如何排除命名空間,以及查看 Google Cloud 服務建立的常見命名空間清單,請前往「排除命名空間」頁面。

      4. 如要啟用參照限制,請選取「Enable Constraint Templates that reference to objects other than the object currently being evaluated」(啟用參照物件並非當前評估項目的限制範本) 核取方塊。

      5. 在「版本」清單中,選取要使用的 Policy Controller 版本。

    3. 按一下 [儲存變更]。

  4. 按一下 [設定]

  5. 在確認對話方塊中,按一下「確認」。如果您先前未啟用 Policy Controller,按一下「確認」會啟用 anthospolicycontroller.googleapis.com API,並在叢集上安裝 Policy Controller。

  6. 選用步驟:將現有叢集與預設設定同步:

    1. 在「設定」分頁中,按一下「與機群設定同步」
    2. 在「Clusters in the fleet」(機群中的叢集) 清單中,選取要同步的叢集,然後按一下「Sync to fleet settings」(與機群設定同步)。這項作業可能要幾分鐘才能完成。

系統會將您重新導向至 Policy Controller 的「設定」分頁。在叢集上安裝及設定 Policy Controller 後,狀態欄就會顯示「已安裝」圖示 。這可能需要幾分鐘的時間。

gcloud

執行下列指令來啟用 Policy Controller:

gcloud container fleet policycontroller enable \
    --memberships=MEMBERSHIP_NAME

您可以設定其他欄位來設定 Policy Controller。舉例來說,您可能會希望政策控制器免除部分命名空間的強制執行作業。如需可設定的完整欄位清單,請參閱 Policy Controller Google Cloud CLI 說明文件,或執行 gcloud container fleet policycontroller enable --help

如要為 Policy Controller 設定機群層級設定,請完成下列步驟:

  1. 建立名為 fleet-default.yaml 的檔案,其中包含 Policy Controller 的預設設定。如要啟用車隊層級的預設值,請務必填寫「installSpec」欄位。這個範例顯示可設定的選項:

    # cat fleet-default.yaml
    
     policyControllerHubConfig:
      installSpec: INSTALL_SPEC_ENABLED 
      # Uncomment to set default deployment-level configurations.
      # deploymentConfigs:
      #   admission:
      #     containerResources:
      #       limits:
      #        cpu: 1000m
      #         memory: 8Gi
      #       requests:
      #         cpu: 500m
      #         memory: 4Gi
      # Uncomment to set policy bundles that you want to install by default.
      # policyContent:
      #   bundles:
      #     cis-k8s-v1.5.1:
      #       exemptedNamespaces:
      #       - "namespace-name"
      # Uncomment to exempt namespaces from admission.
      # exemptableNamespaces:
      # - "namespace-name"
      # Uncomment to enable support for referential constraints
      # referentialRulesEnabled: true
      # Uncomment to disable audit, adjust value to set audit interval
      # auditIntervalSeconds: 0
      # Uncomment to log all denies and dryrun failures
      # logDeniesEnabled: true
      # Uncomment to enable mutation
      # mutationEnabled: true
      # Uncomment to adjust the value to set the constraint violation limit
      # constraintViolationLimit: 20
      # ... other fields ...
    
    最佳做法

    豁免系統命名空間,避免環境發生錯誤。如要瞭解如何排除命名空間,以及查看 Google Cloud 服務建立的常見命名空間清單,請前往「排除命名空間」頁面。

  2. 將預設設定套用至機群:

    gcloud container fleet policycontroller enable \
      --fleet-default-member-config=fleet-default.yaml
    
  3. 如要確認設定已套用,請執行下列指令:

    gcloud container fleet policycontroller describe
    
  4. 如要移除車隊層級的預設設定,請執行下列指令:

    gcloud container fleet policycontroller enable \
      --no-fleet-default-member-config
    

Terraform

如要透過安裝的預設範本庫,在整個機群中啟用 Policy Controller,請參閱下列範例:

resource "google_gke_hub_feature" "policycontroller" {
  name     = "policycontroller"
  location = "global"
  project  = data.google_project.default.project_id

  fleet_default_member_config {
    policycontroller {
      policy_controller_hub_config {
        install_spec = "INSTALL_SPEC_ENABLED"
        policy_content {
          bundles {
            bundle = "pss-baseline-v2022"
          }
          template_library {
            installation = "ALL"
          }
        }
      }
    }
  }
}

您可以傳遞多個 bundle 區塊,安裝Policy Controller 套件總覽中列出的任何套件。

如要進一步瞭解如何使用 Terraform,請參閱「Policy Controller 的 Terraform 支援」。

驗證 Policy Controller 安裝作業

安裝 Policy Controller 後,您可以確認是否成功完成安裝。

控制台

操作步驟如下:

  1. 在 Google Cloud 控制台中,前往「Posture Management」(狀態管理) 專區下方的 GKE Enterprise「Policy」(政策) 頁面。

    前往「政策」

  2. 在「設定」分頁的叢集表格中,查看「Policy Controller 狀態」欄。安裝成功後,狀態會顯示為「已安裝」

gcloud

執行下列指令:

gcloud container fleet policycontroller describe --memberships=MEMBERSHIP_NAME

安裝成功後,系統會顯示 membershipStates: MEMBERSHIP_NAME: policycontroller: state: ACTIVE

確認限制範本庫安裝作業

安裝 Policy Controller 時,系統預設會安裝限制範本庫。這項安裝作業可能需要幾分鐘才能完成。 您可以確認範本庫是否已順利完成。

控制台

操作步驟如下:

  1. 在 Google Cloud 控制台中,前往「Posture Management」(狀態管理) 專區下方的 GKE Enterprise「Policy」(政策) 頁面。

    前往「政策」

  2. 在「設定」分頁的叢集表格中,選取「已安裝的套件組合」欄中列出的數字。在「政策內容狀態」窗格中,範本庫成功安裝後會顯示「已安裝」狀態

gcloud

執行下列指令:

kubectl get constrainttemplates

您會看到類似以下範例的輸出內容:

NAME                                      AGE
k8sallowedrepos                           84s
k8scontainerlimits                        84s
k8spspallowprivilegeescalationcontainer   84s
...[OUTPUT TRUNCATED]...

如果個別限制範本安裝正確,其 status.created 欄位會是 true

Policy Controller 與 Config Sync 的互動

如果您搭配使用 Policy Controller 和 Config Sync,請注意與儲存在可靠來源 (例如 Git 存放區) 中的資源的下列互動,這些資源會由 Config Sync 同步處理:

  • 除非停用限制範本程式庫,否則您無法同步處理也屬於範本程式庫的限制範本。

  • 如要同步處理儲存在 gatekeeper-system 命名空間中的 Config 資源,您必須只在可靠來源中定義 Config 資源。gatekeeper-system Namespace 不得與其一併定義。

設定指標

根據預設,Policy Controller 會嘗試將指標匯出至 Prometheus 和 Cloud Monitoring。您可能需要完成其他設定步驟 (例如授予權限),才能允許 Policy Controller 匯出指標。詳情請參閱「使用 Policy Controller 指標」。

管理限制範本庫

如要瞭解如何解除安裝或安裝限制範本、相關聯的限制,或是限制範本庫,請參閱「建立限制」。

豁免命名空間,不強制執行政策

您可以設定 Policy Controller,忽略命名空間內的物件。詳情請參閱「將命名空間排除在政策控制器之外」。

修改資源

政策控管器也會做為變更 Webhook。詳情請參閱「修改資源」。

查看 Policy Controller 版本

如要瞭解 Gatekeeper Policy Controller 使用哪個版本,請執行下列指令來查看映像檔標記:

kubectl get deployments -n gatekeeper-system gatekeeper-controller-manager \
  -o="jsonpath={.spec.template.spec.containers[0].image}"

用於建構 Gatekeeper 的 Git 標記 (或雜湊) 和 ConfigManagement Operator 版本號碼會納入映像檔標記,如下所示:

.../gatekeeper:VERSION_NUMBER-GIT_TAG.gBUILD_NUMBER

舉例來說,以下圖片:

gcr.io/config-management-release/gatekeeper:anthos1.3.2-480baac.g0
  • anthos1.3.2 是版本號碼。
  • 480baac 是 Git 標記。
  • 0 是版本號碼。

升級 Policy Controller

升級 Policy Controller 之前,請先查看版本資訊,瞭解各版本之間的異動。

如要升級 Policy Controller,請完成下列步驟:

控制台

  1. 在 Google Cloud 控制台中,前往「Posture Management」(狀態管理) 專區下方的 GKE Enterprise「Policy」(政策) 頁面。

    前往「政策」

  2. 在「設定」分頁標籤下方,找出要升級版本的叢集,然後選取旁邊的「編輯設定」圖示
  3. 展開「編輯 Policy Controller 設定」選單。
  4. 從「版本」下拉式選單中,選取要升級的版本。
  5. 按一下 [儲存變更]。

gcloud

執行下列指令:

gcloud container fleet policycontroller update \
  --version=VERSION \
  --memberships=MEMBERSHIP_NAME

更改下列內容:

  • VERSION:要升級的版本
  • MEMBERSHIP_NAME:註冊叢集時選擇的成員名稱。您可以執行 gcloud container fleet memberships list 找出會員名稱。

解除安裝 Policy Controller

如要從叢集解除安裝 Policy Controller,請按照下列步驟操作。

控制台

如要在叢集上停用 Policy Controller,請完成下列工作:

  1. 在 Google Cloud 控制台中,前往「Posture Management」(狀態管理) 專區下方的 GKE Enterprise「Policy」(政策) 頁面。

    前往「政策」

  2. 在「設定」分頁的叢集表格中,選取「編輯設定」欄中的「編輯」
  3. 在叢集窗格中,展開「About Policy Controller」選單。
  4. 選取「解除安裝 Policy Controller」
  5. 按照確認對話方塊中的指示操作,然後選取「確認」,確認解除安裝。

解除安裝 Policy Controller 後,狀態欄就會顯示「Not installed」(未安裝) 圖示

gcloud

如要解除安裝 Policy Controller,請執行下列指令:

gcloud container fleet policycontroller disable \
  --memberships=MEMBERSHIP_NAME

MEMBERSHIP_NAME 替換為要停用 Policy Controller 的已註冊叢集成員資格名稱。你可以指定多個會員方案,並以半形逗號分隔。

Policy Controller RBAC 和權限

Policy Controller 包含高權限工作負載。這些工作負載的權限涵蓋在 Open Policy Agent Gatekeeper 作業說明文件中。

後續步驟