使用 Policy Controller 指標

本頁說明如何使用指標監控 Policy Controller。

Policy Controller 包含多項與政策使用情況相關的指標。舉例來說,有些指標會記錄限制和限制範本的數量,以及偵測到的稽核違規次數。如要建立及記錄這些指標,Policy Controller 會使用 OpenTelemetry。您可以設定 Policy Controller,將這些指標匯出至 PrometheusCloud Monitoring。匯出指標的預設設定會將指標匯出至 Prometheus 和 Cloud Monitoring。

設定指標匯出作業

您可以設定 Policy Controller 匯出指標的方式。安裝 Policy Controller 時,您可以選擇 Prometheus、Cloud Monitoring、兩者皆是或兩者皆非。根據預設,Policy Controller 會嘗試將指標匯出至 Prometheus 和 Cloud Monitoring。

將指標匯出至 Cloud Monitoring

如要允許 Policy Controller 將指標匯出至 Cloud Monitoring,Policy Controller 使用的服務帳戶必須具備專案的 roles/monitoring.metricsWriter IAM 角色。如要授予這個角色,請採取下列任一做法:

  • 如果 Policy Controller 在具有 Google Cloud 預設服務帳戶的環境中執行,請將專案的 roles/monitoring.metricsWriter 角色授予該服務帳戶:

    gcloud projects add-iam-policy-binding PROJECT_ID \
        --member="serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com" \
        --role=roles/monitoring.metricsWriter
    

    更改下列內容:

    • PROJECT_ID:您的 Google Cloud 專案 ID。
    • PROJECT_NUMBER:您的數字 Google Cloud專案編號。
  • 如果已啟用 Workload Identity Federation for GKE機群 Workload Identity Federation,請將專案的 roles/monitoring.metricsWriter 角色授予 Policy Controller 使用的 Kubernetes ServiceAccount:

    gcloud projects add-iam-policy-binding PROJECT_ID \
        --member="serviceAccount:PROJECT_ID.svc.id.goog[gatekeeper-system/gatekeeper-admin]" \
        --role=roles/monitoring.metricWriter
    

    PROJECT_ID 替換為叢集的 Google Cloud 專案 ID。

您可以使用 Metrics Explorer透過 Cloud Monitoring API 查看這些指標。

將指標匯出至 Prometheus

Policy Controller 會在 gatekeeper-system 命名空間下,將 gatekeeper-controller-manager-* Pod 的通訊埠 8888 指標匯出至 Prometheus。

如果 Policy Controller 在已設定 Google Cloud Managed Service for Prometheus 的叢集上執行,這些指標會自動收集並儲存在 Cloud Monitoring 中。如果叢集在安裝 Policy Controller 後啟用 Google Cloud Managed Service for Prometheus,這項功能也適用。視您的政策而定,您可能也需要授予 Google Cloud Managed Service for Prometheus 使用的預設 Compute Engine 服務帳戶其他權限。如要瞭解如何授予 Google Cloud Managed Service for Prometheus 權限,請參閱「啟用 Managed Collection:GKE」。如要進一步瞭解如何設定 Google Cloud Managed Service for Prometheus,請參閱「開始使用代管集合」一文。

由於 Policy Controller 隨附於 GKE Enterprise,因此使用 Google Cloud Managed Service for Prometheus 等 Google Cloud Observability 服務時,無須支付額外費用。如果您在需要 GKE Enterprise 授權前安裝了 Policy Controller,並啟用了 Google Cloud Managed Service for Prometheus,系統可能會針對收集的指標向您收費。如要進一步瞭解 Cloud Monitoring 定價,請參閱「Google Cloud Managed Service for Prometheus 定價」。

如需如何使用 Google Cloud Managed Service for Prometheus 解決方案查看指標的範例,請參閱「使用 Cloud Monitoring 查詢」。

查看指標

Policy Controller 指標會以 Prometheus 格式匯出至 Cloud Monitoring 專案。因此,您可以使用 Cloud Monitoring API 和 Google Cloud 控制台中的資訊主頁查詢指標。您可以編輯這個資訊主頁,以符合業務和營運需求。

如要查詢 Cloud Monitoring API,請使用 Prometheus 查詢語言 (PromQL) (Kubernetes 指標的實際查詢語言),或 Monitoring 查詢語言 (MQL) (Google 的專有指標查詢語言)。

如要建立 Policy Controller 資訊主頁,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「資訊主頁」頁面。

    前往資訊主頁

  2. 在「資訊主頁總覽」頁面中,按一下「範本庫」分頁標籤。 這個分頁會顯示所有可匯入的資訊主頁。

  3. 在「類別」欄中,選取「Anthos Config Management」

  4. 在「Anthos Config Management samples」(Anthos Config Management 範例) 表格中,選取「Policy Controller」(政策控制器) 核取方塊。

  5. 按一下 「匯入」

  6. 如要建立資訊主頁,請按一下確認視窗中的「確認」

如要查看及編輯 Policy Controller 資訊主頁,請按照下列步驟操作:

  1. 在「資訊主頁總覽」頁面中,選取「資訊主頁清單」分頁標籤。
  2. 選取「Policy Controller」資訊主頁。
  3. 如要自訂資訊主頁,請按一下「編輯資訊主頁」
  4. 進行所需變更,然後按一下「儲存」。如要進一步瞭解如何自訂資訊主頁,請參閱 Cloud Monitoring 說明文件中的「篩選資訊主頁」。

建立快訊

如要在指標達到特定門檻時收到通知,請在 Cloud Monitoring 中建立快訊政策

第三方整合

使用 Cloud Monitoring API 時,任何第三方可觀測性工具都能擷取 Policy Controller 指標。

舉例來說,如果您使用 Grafana 資訊主頁,請在 Grafana 中將 Cloud Monitoring API 新增為資料來源。詳情請參閱 Grafana 說明文件中的「Google Cloud Monitoring」。

可用的指標

如果叢集已啟用 Policy Controller,且設定為匯出至 Cloud Monitoring,您可以查詢下列指標 (全部都以 OpenCensus/ 為前置字元):

名稱 類型 標籤 說明
OpenCensus/audit_duration_seconds 累計 稽核週期時間長度分布
OpenCensus/audit_last_run_time 度量圖 上次稽核執行時間以來的 Epoch 時間戳記,以浮點數表示 (以秒為單位)
OpenCensus/constraint_template_ingestion_count 累計 狀態 限制範本擷取動作總數
OpenCensus/constraint_template_ingestion_duration_seconds 累計 狀態 限制範本攝入時間長度的分布情形
OpenCensus/constraint_templates 度量圖 狀態 目前限制範本數量
OpenCensus/validation_request_count 計數器 admission_status API 伺服器發出的許可要求數
OpenCensus/validation_request_duration_seconds 累計 admission_status 許可要求時間長度分布
OpenCensus/violations 度量圖 enforcement_action 上次稽核週期偵測到的稽核違規次數
OpenCensus/watch_manager_intended_watch_gvk 度量圖 Policy Controller 應監控多少不重複的 GroupVersionKinds。這項指標是同步資源和限制的組合。
OpenCensus/watch_manager_watched_gvk 度量圖 Policy Controller 實際監控的 GroupVersionKinds 數量。這項指標的目標是與 OpenCensus/watch_manager_intended_watch_gvk 趨於一致。

如果 Policy Controller 設定為匯出至 Prometheus,您可以查詢下列指標 (所有指標都以 Prometheus/ 為前置字元):

名稱 類型 標籤 說明
Prometheus/gatekeeper_audit_duration_seconds/histogram 累計 稽核週期時間長度分布
Prometheus/gatekeeper_audit_last_run_end_time/gauge 度量圖 上次稽核作業結束時的紀元時間戳記,以浮點數表示 (秒)
Prometheus/gatekeeper_audit_last_run_time/gauge 度量圖 上次稽核執行的開始時間的紀元時間戳記,以浮點數表示 (以秒為單位)
Prometheus/gatekeeper_constraint_template_ingestion_count/counter 累計 狀態 限制範本擷取動作總數
Prometheus/gatekeeper_constraint_template_ingestion_duration_seconds/histogram 累計 狀態 限制範本攝入時間長度的分布情形
Prometheus/gatekeeper_constraint_templates/gauge 度量圖 狀態 目前限制範本數量
Prometheus/gatekeeper_validation_request_count/counter 累計 admission_status、admission_dryrun API 伺服器發出的許可要求數
Prometheus/gatekeeper_validation_request_duration_seconds/histogram 累計 admission_status 許可要求時間長度分布
Prometheus/gatekeeper_violations/gauge 度量圖 enforcement_action 上次稽核週期偵測到的稽核違規次數
Prometheus/gatekeeper_watch_manager_intended_watch_gvk/gauge 度量圖 Policy Controller 應監控多少不重複的 GroupVersionKinds。這項指標是同步資源和限制的組合。
Prometheus/gatekeeper_watch_manager_watched_gvk/gauge 度量圖 Policy Controller 實際監控的 GroupVersionKinds 數量。這項指標的目標是與 Prometheus/gatekeeper_watch_manager_intended_watch_gvk/gauge 趨於一致。

後續步驟