開始使用代管集合

本文說明如何設定 Google Cloud Managed Service for Prometheus,並啟用代管收集功能。這項設定是運作中擷取作業的最小範例,使用 Prometheus 部署項目監控範例應用程式,並將收集到的指標儲存在 Monarch 中。

本文說明如何執行下列操作:

  • 設定環境和指令列工具。
  • 為叢集設定代管收集功能。
  • 設定資源,以便抓取目標及擷取指標。
  • 遷移現有的 prometheus-operator 自訂資源。

建議您使用代管收集功能,降低部署、擴充、資料分割、設定及維護收集器的複雜度。GKE 和所有其他 Kubernetes 環境都支援代管收集功能。

代管收集功能會以 DaemonSet 的形式執行 Prometheus 收集器,並僅抓取共置節點上的目標,確保可擴充性。您可以使用輕量級自訂資源設定收集器,透過提取收集功能抓取匯出工具,然後收集器會將抓取的資料推送至中央資料儲存庫 Monarch。 Google Cloud 一律不會直接存取叢集來提取或抓取指標資料,而是由收集器將資料推送至Google Cloud。如要進一步瞭解代管和自行部署的資料收集,請參閱「Managed Service for Prometheus 資料收集」和「使用代管和自行部署的資料收集功能擷取及查詢」。

事前準備

本節說明本文所述工作所需的設定。

設定專案和工具

如要使用 Google Cloud Managed Service for Prometheus,您需要下列資源:

  • 已啟用 Cloud Monitoring API 的 Google Cloud 專案。

    • 如果您沒有 Google Cloud 專案,請按照下列步驟操作:

      1. 前往 Google Cloud 控制台的「New Project」(新增專案):

        建立新專案

      2. 在「Project Name」(專案名稱) 欄位中輸入專案名稱,然後按一下「Create」(建立)。

      3. 前往「帳單」:

        前往「帳單」頁面

      4. 如果頁面頂端尚未選取您剛建立的專案,請選取該專案。

      5. 系統會提示您選擇現有的付款資料或是建立新的付款資料。

      新專案預設會啟用 Monitoring API。

    • 如果您已有 Google Cloud 專案,請確認 Monitoring API 已啟用:

      1. 前往Google Cloud 控制台的 Monitoring API 程式庫頁面:

        前往「Library」(程式庫) 頁面

      2. 選取專案。

      3. 如果程式庫頁面顯示「Manage」(管理) 按鈕,表示 Monitoring API 已啟用。如果看到「Enable」(啟用) 按鈕,代表 API 尚未啟用,必須點選「Enable」(啟用) 按鈕才能啟用。

  • Kubernetes 叢集。如果您沒有 Kubernetes 叢集,請按照 GKE 快速入門導覽課程中的操作說明操作。

您還需要下列指令列工具:

  • gcloud
  • kubectl

gcloud 和 kubectl 工具是 Google Cloud CLI 的一部分。如要瞭解如何安裝,請參閱「管理 Google Cloud CLI 元件」。如要查看已安裝的 gcloud CLI 元件,請執行下列指令:

gcloud components list

設定環境

為避免重複輸入專案 ID 或叢集名稱,請執行下列設定:

  • 請按照下列步驟設定指令列工具:

    • 設定 gcloud CLI,參照Google Cloud 專案的 ID:

      gcloud config set project PROJECT_ID
      
    • 如果是在 GKE 上執行,請使用 gcloud CLI 設定叢集:

      gcloud container clusters get-credentials CLUSTER_NAME --location LOCATION --project PROJECT_ID
      
    • 否則,請使用 kubectl CLI 設定叢集:

      kubectl config set-cluster CLUSTER_NAME
      

    如要進一步瞭解這些工具,請參閱下列資源:

設定命名空間

建立 NAMESPACE_NAME Kubernetes 命名空間,用於存放您在範例應用程式中建立的資源。使用本文件設定 Prometheus 範例時,建議使用命名空間名稱 gmp-test。

執行下列指令,建立命名空間:

kubectl create ns NAMESPACE_NAME

設定代管收集功能

您可以在 GKE 和非 GKE Kubernetes 叢集上使用代管收集功能。

啟用代管收集功能後,叢集內元件會開始運作,但尚未產生任何指標。這些元件需要 PodMonitoring 或 ClusterPodMonitoring 資源,才能正確抓取指標 端點。您必須使用有效的指標端點部署這些資源,或啟用其中一個代管指標套件,例如 GKE 內建的 Kube 狀態指標。如需疑難排解資訊,請參閱「擷取端問題」。

啟用代管收集功能後,系統會在叢集中安裝下列元件:

如需 Managed Service for Prometheus operator 的參考文件,請參閱資訊清單頁面。

啟用代管收集功能:GKE

下列項目預設會啟用代管收集功能:

如果您在預設未啟用代管收集功能的 GKE 環境中執行,請參閱「手動啟用代管收集功能」。

當新的叢集內元件版本發布時,GKE 的代管收集功能會自動升級。

GKE 上的代管收集功能會使用授予預設 Compute Engine 服務帳戶的權限。如果您的政策會修改預設節點服務帳戶的標準權限,可能需要新增 Monitoring Metric Writer 角色才能繼續。

手動啟用代管收集功能

如果您在 GKE 環境中執行,但預設未啟用代管收集功能,則可使用下列指令啟用:

  • Cloud Monitoring 中的「Managed Prometheus Bulk Cluster Enablement」(大量啟用 Managed Prometheus 叢集) 資訊主頁。
  • Google Cloud 控制台中的「Kubernetes Engine」頁面。
  • Google Cloud CLI。如要使用 gcloud CLI,您必須執行 GKE 1.21.4-gke.300 以上版本。
  • Terraform for Google Kubernetes Engine。如要使用 Terraform 啟用 Managed Service for Prometheus,您必須執行 GKE 1.21.4-gke.300 以上版本。

Managed Prometheus 大量啟用叢集資訊主頁

您可以在 Cloud Monitoring 中使用「Managed Prometheus Bulk Cluster Enablement」(大量啟用 Managed Prometheus 叢集) 資訊主頁,執行下列操作:

  • 判斷叢集是否已啟用 Managed Service for Prometheus,以及您使用的是代管或自行部署的收集作業。
  • 為專案中的叢集啟用代管收集功能。
  • 查看叢集的其他資訊。

如要查看「Managed Prometheus Bulk Cluster Enablement」資訊主頁,請按照下列步驟操作:

  1. 在 Google Cloud 控制台中,前往「Dashboards」(資訊主頁) 頁面:

    前往「Dashboards」(資訊主頁)

    如果您是使用搜尋列尋找這個頁面,請選取子標題為「Monitoring」的結果。

  2. 使用篩選列搜尋並選取「Managed Prometheus Bulk Cluster Enablement」項目。

Cloud Monitoring 中的「Managed Prometheus Bulk Cluster Enablement」(大量啟用 Managed Prometheus 叢集) 資訊主頁。

如要使用「Managed Prometheus Bulk Cluster Enablement」(大量啟用 Managed Prometheus 叢集) 資訊主頁,在一或多個 GKE 叢集啟用代管收集功能,請按照下列步驟操作:

  1. 找出您要啟用代管集合的所有 GKE 叢集,然後勾選相應的核取方塊。

  2. 選取「啟用所選項目」。

Kubernetes Engine UI

您可以使用 Google Cloud 控制台執行下列操作:

  • 在現有 GKE 叢集啟用代管收集功能。
  • 建立新的 GKE 叢集,並啟用代管收集功能。

如要更新現有叢集,請按照下列步驟操作:

  1. 在 Google Cloud 控制台中,前往「Kubernetes clusters」(Kubernetes 叢集) 頁面:

    前往「Kubernetes clusters」(Kubernetes 叢集)

    如果您是使用搜尋列尋找這個頁面,請選取子標題為「Kubernetes Engine」的結果。

  2. 按一下叢集名稱。

  3. 在「Features」(功能) 清單中,找到「Managed Service for Prometheus」選項。如果顯示為停用,請按一下 「Edit」(編輯),然後選取「Enable Managed Service for Prometheus」(啟用 Managed Service for Prometheus)。

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

如要建立已啟用代管收集功能的叢集,請按照下列步驟操作:

  1. 在 Google Cloud 控制台中,前往「Kubernetes clusters」(Kubernetes 叢集) 頁面:

    前往「Kubernetes clusters」(Kubernetes 叢集)

    如果您是使用搜尋列尋找這個頁面,請選取子標題為「Kubernetes Engine」的結果。

  2. 點選「建立」。

  3. 點選「Standard」選項的「設定」。

  4. 在導覽面板中,按一下「Features」(功能)。

  5. 在「Operations」(作業) 專區中,選取「Enable Managed Service for Prometheus」(啟用 Managed Service for Prometheus)。

  6. 按一下「儲存」。

gcloud CLI

您可以使用 gcloud CLI 執行下列操作:

  • 在現有 GKE 叢集啟用代管收集功能。
  • 建立新的 GKE 叢集,並啟用代管收集功能。

這些指令最多可能需要 5 分鐘才能完成。

首先,設定專案:

gcloud config set project PROJECT_ID

如要更新現有叢集,請根據叢集是可用區或區域叢集,執行下列其中一個 update 指令:

  • gcloud container clusters update CLUSTER_NAME --enable-managed-prometheus --zone ZONE
    
  • gcloud container clusters update CLUSTER_NAME --enable-managed-prometheus --region REGION
    

如要建立已啟用代管收集功能的叢集,請執行下列指令:

gcloud container clusters create CLUSTER_NAME --zone ZONE --enable-managed-prometheus

GKE Autopilot

在執行 GKE 1.25 以上版本的 GKE Autopilot 叢集中,代管收集功能預設為啟用。無法關閉代管收集功能。

如果叢集在升級至 1.25 版時,無法自動啟用代管收集功能,您可以執行 gcloud CLI 專區中的更新指令,手動啟用這項功能。

Terraform

如需使用 Terraform 設定代管收集功能的說明,請參閱 google_container_cluster 的 Terraform 登錄檔。

如需搭配使用 Google Cloud 與 Terraform 的一般資訊,請參閱「搭配使用 Terraform 與 Google Cloud」。

停用代管收集功能

如要停用叢集的代管收集功能,可以使用下列任一方法:

Kubernetes Engine UI

您可以使用 Google Cloud 控制台執行下列操作:

  • 在現有 GKE 叢集停用代管收集功能。
  • 建立執行 GKE 1.27 以上版本的新 GKE Standard 叢集時,覆寫代管收集功能的自動啟用設定。

如要更新現有叢集,請按照下列步驟操作:

  1. 在 Google Cloud 控制台中,前往「Kubernetes clusters」(Kubernetes 叢集) 頁面:

    前往「Kubernetes clusters」(Kubernetes 叢集)

    如果您是使用搜尋列尋找這個頁面,請選取子標題為「Kubernetes Engine」的結果。

  2. 按一下叢集名稱。

  3. 在「Features」(功能) 區段中,找到「Managed Service for Prometheus」選項。按一下「 Edit」(編輯),然後清除「Enable Managed Service for Prometheus」(啟用 Managed Service for Prometheus)。

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

如要在建立新的 GKE Standard 叢集 (1.27 以上版本) 時,覆寫受管理收集功能的自動啟用設定,請按照下列步驟操作:

  1. 在 Google Cloud 控制台中,前往「Kubernetes clusters」(Kubernetes 叢集) 頁面:

    前往「Kubernetes clusters」(Kubernetes 叢集)

    如果您是使用搜尋列尋找這個頁面,請選取子標題為「Kubernetes Engine」的結果。

  2. 點選「建立」。

  3. 點選「Standard」選項的「設定」。

  4. 在導覽面板中,按一下「Features」(功能)。

  5. 在「Operations」部分,清除「Enable Managed Service for Prometheus」(啟用 Managed Service for Prometheus)。

  6. 按一下「儲存」。

gcloud CLI

您可以使用 gcloud CLI 執行下列操作:

  • 在現有 GKE 叢集停用代管收集功能。
  • 建立執行 GKE 1.27 以上版本的新 GKE Standard 叢集時,覆寫代管收集功能的自動啟用設定。

這些指令最多可能需要 5 分鐘才能完成。

首先,設定專案:

gcloud config set project PROJECT_ID

如要停用現有叢集的代管收集功能,請根據叢集是可用區或區域叢集,執行下列其中一個 update 指令:

  • gcloud container clusters update CLUSTER_NAME --disable-managed-prometheus --zone ZONE
    
  • gcloud container clusters update CLUSTER_NAME --disable-managed-prometheus --region REGION
    

如要在建立新的 GKE Standard 叢集 (1.27 以上版本) 時,覆寫代管收集功能的自動啟用設定,請執行下列指令:

gcloud container clusters create CLUSTER_NAME --zone ZONE --no-enable-managed-prometheus

GKE Autopilot

如果 GKE Autopilot 叢集執行 GKE 1.25 以上版本,則無法關閉代管收集功能。

Terraform

如要停用代管收集功能,請將 managed_prometheus 設定區塊中的 enabled 屬性設為 false。如要進一步瞭解這個設定區塊,請參閱 google_container_cluster 的 Terraform 註冊資料庫。

如需搭配使用 Google Cloud 與 Terraform 的一般資訊,請參閱「搭配使用 Terraform 與 Google Cloud」。

啟用代管收集功能:非 GKE Kubernetes

如果您在非 GKE 環境中執行,則可使用下列指令啟用代管收集功能:

  • kubectl CLI。
  • 執行 1.12 以上版本的 VMware 或裸機地端部署。

kubectl CLI

如要在使用非 GKE Kubernetes 叢集時安裝代管收集器,請執行下列指令,安裝設定和運算子資訊清單:

kubectl apply -f https://raw.githubusercontent.com/GoogleCloudPlatform/prometheus-engine/v0.19.4/manifests/setup.yaml

kubectl apply -f https://raw.githubusercontent.com/GoogleCloudPlatform/prometheus-engine/v0.19.4/manifests/operator.yaml

地端部署

如要瞭解如何為地端部署叢集設定代管收集功能,請參閱發行版本的說明文件:

部署範例應用程式

範例應用程式會在 metrics 通訊埠發布 example_requests_total 計數器指標和 example_random_numbers 直方圖指標 (以及其他指標)。應用程式的資訊清單定義了三個副本。

執行下列指令來部署範例應用程式:

kubectl -n NAMESPACE_NAME apply -f https://raw.githubusercontent.com/GoogleCloudPlatform/prometheus-engine/v0.19.4/examples/example-app.yaml

設定 PodMonitoring 資源

Managed Service for Prometheus 會使用目標抓取功能,擷取範例應用程式發出的指標資料。目標抓取和指標擷取作業需透過 Kubernetes 自訂資源設定,而 Prometheus 代管服務使用的是 PodMonitoring 自訂資源。

PodMonitoring 自訂資源只會抓取所部署命名空間內的目標。如要抓取多個命名空間內的目標,必須在每個命名空間部署相同的 PodMonitoring 自訂資源。您可以執行 kubectl get podmonitoring -A,確認 PodMonitoring 資源已安裝在目標命名空間。

如要查看所有 Managed Service for Prometheus 自訂資源的參考文件,請參閱 prometheus-engine/doc/api 參考資料。

下列資訊清單可為 NAMESPACE_NAME 命名空間定義 PodMonitoring 資源 prom-example。這項資源會使用 Kubernetes 標籤選取器,找出命名空間中含有 app.kubernetes.io/name 標籤且值為 prom-example 的 Pod。系統每 30 秒會透過 /metrics HTTP 路徑的 metrics 通訊埠,抓取相符 Pod 的資料。

apiVersion: monitoring.googleapis.com/v1
kind: PodMonitoring
metadata:
  name: prom-example
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: prom-example
  endpoints:
  - port: metrics
    interval: 30s

執行下列指令來套用這項資源:

kubectl -n NAMESPACE_NAME apply -f https://raw.githubusercontent.com/GoogleCloudPlatform/prometheus-engine/v0.19.4/examples/pod-monitoring.yaml

代管收集器已開始抓取相符的 Pod。如要查看抓取目標的狀態,請啟用目標狀態功能。

如要設定會跨所有命名空間橫向收集 Pod 的作業,請使用 ClusterPodMonitoring 資源。ClusterPodMonitoring 資源提供的介面與 PodMonitoring 資源相同,但不會將抓取到的 Pod 限制在特定命名空間。

如果您在 GKE 執行上述作業,還能執行以下操作:

如果您是在 GKE 以外的環境中執行,則必須建立服務帳戶,並授權該帳戶寫入指標資料,如以下各節所述。

明確提供憑證

在 GKE 上執行時,收集 Prometheus 伺服器會根據節點的服務帳戶,自動從環境擷取憑證。在非 GKE Kubernetes 叢集中,必須透過 gmp-public 命名空間中的 OperatorConfig 資源明確提供憑證。

  1. 將背景資訊設為目標專案:

    gcloud config set project PROJECT_ID
    
  2. 建立服務帳戶:

    gcloud iam service-accounts create gmp-test-sa
    

  3. 將必要權限授予服務帳戶:

    gcloud projects add-iam-policy-binding PROJECT_ID\
      --member=serviceAccount:gmp-test-sa@PROJECT_ID.iam.gserviceaccount.com \
      --role=roles/monitoring.metricWriter
    

  4. 為服務帳戶建立並下載金鑰:

    gcloud iam service-accounts keys create gmp-test-sa-key.json \
      --iam-account=gmp-test-sa@PROJECT_ID.iam.gserviceaccount.com
    
  5. 將金鑰檔案新增為非 GKE 叢集的 Secret:

    kubectl -n gmp-public create secret generic gmp-test-sa \
      --from-file=key.json=gmp-test-sa-key.json
    

  6. 開啟 OperatorConfig 資源進行編輯:

    kubectl -n gmp-public edit operatorconfig config
    
    1. 將粗體文字新增至資源:

      apiVersion: monitoring.googleapis.com/v1
      kind: OperatorConfig
      metadata:
        namespace: gmp-public
        name: config
      collection:
        credentials:
          name: gmp-test-sa
          key: key.json
      
      請務必將這些憑證新增至 rules 區段,代管規則評估功能才能正常運作。

    2. 儲存檔案並關閉編輯器。套用變更後,系統會重新建立 Pod,並開始使用指定的服務帳戶向指標後端進行驗證。

    受管理精選項目的其他主題

    本節說明如何執行下列操作:

    • 啟用目標狀態功能,輕鬆偵錯。
    • 使用 Terraform 設定目標抓取。
    • 篩選匯出至代管服務的資料。
    • 抓取 Kubelet 和 cAdvisor 指標。
    • 轉換現有的 prom-operator 資源,以便與代管服務搭配使用。
    • 在 GKE 外部執行代管收集。

    啟用目標狀態功能

    Managed Service for Prometheus 可讓您檢查收集器是否正確探索及抓取目標。這份目標狀態報表是用來偵錯嚴重問題的工具。強烈建議您只在調查緊急問題時啟用這項功能。如果在大型叢集中啟用目標狀態回報功能,可能會導致運算子記憶體不足,並發生當機迴圈。

    如要查看 PodMonitoring 或 ClusterPodMonitoring 資源中的目標狀態,請將 OperatorConfig 資源中的 features.targetStatus.enabled 值設為 true,如下所示:

    apiVersion: monitoring.googleapis.com/v1
    kind: OperatorConfig
    metadata:
      namespace: gmp-public
      name: config
    features:
      targetStatus:
        enabled: true
    

    幾秒後,每個有效的 PodMonitoring 或 ClusterPodMonitoring 資源 (如有設定) 都會顯示 Status.Endpoint Statuses 欄位。

    如果 NAMESPACE_NAME 命名空間中有一個名為 prom-example 的 PodMonitoring 資源,您可以執行下列指令來檢查狀態:

    kubectl -n NAMESPACE_NAME describe podmonitorings/prom-example
    

    輸出內容如下:

    API Version:  monitoring.googleapis.com/v1
    Kind:         PodMonitoring
    ...
    Status:
      Conditions:
        ...
        Status:                True
        Type:                  ConfigurationCreateSuccess
      Endpoint Statuses:
        Active Targets:       3
        Collectors Fraction:  1
        Last Update Time:     2023-08-02T12:24:26Z
        Name:                 PodMonitoring/custom/prom-example/metrics
        Sample Groups:
          Count:  3
          Sample Targets:
            Health:  up
            Labels:
              Cluster:                     CLUSTER_NAME
              Container:                   prom-example
              Instance:                    prom-example-589ddf7f7f-hcnpt:metrics
              Job:                         prom-example
              Location:                    REGION
              Namespace:                   NAMESPACE_NAME
              Pod:                         prom-example-589ddf7f7f-hcnpt
              project_id:                  PROJECT_ID
            Last Scrape Duration Seconds:  0.020206416
            Health:                        up
            Labels:
              ...
            Last Scrape Duration Seconds:  0.054189485
            Health:                        up
            Labels:
              ...
            Last Scrape Duration Seconds:  0.006224887
    

    輸出內容包含下列狀態欄位:

    • 當 Managed Service for Prometheus 確認並處理 PodMonitoring 或 ClusterPodMonitoring 時,Status.Conditions.Status 為 true。
    • Status.Endpoint Statuses.Active Targets 顯示 Managed Service for Prometheus 在這個 PodMonitoring 資源的所有收集器上,計算的抓取目標數量。在範例應用程式中,prom-example 部署作業有三個副本,且只有一個指標目標,因此值為 3。如果目標健康狀態不良,系統會顯示 Status.Endpoint Statuses.Unhealthy Targets 欄位。
    • 如果 Managed Service for Prometheus 可連上所有代管收集器,Status.Endpoint Statuses.Collectors Fraction 會顯示 1 (代表 100%)。
    • Status.Endpoint Statuses.Last Update Time 顯示上次更新時間。如果上次更新時間明顯長於您想要的抓取間隔時間,則差異可能表示目標或叢集有問題。
    • Status.Endpoint Statuses.Sample Groups 欄位會顯示樣本目標,並依收集器插入的常見目標標籤分組。如果找不到目標,這個值有助於偵錯。如果所有目標都健康良好且正在收集,則 Health 欄位的預期值為 up,而 Last Scrape Duration Seconds 欄位的值則是一般目標的正常持續時間。

    如要進一步瞭解這些欄位,請參閱 Managed Service for Prometheus API 文件。

    下列任一情況可能代表設定有問題:

    • PodMonitoring 資源中沒有 Status.Endpoint Statuses 欄位。
    • Last Scrape Duration Seconds 欄位的值過舊。
    • 目標數量太少。
    • Health 欄位的值表示目標為 down。

    如要進一步瞭解如何偵錯目標探索問題,請參閱疑難排解說明文件中的「擷取端問題」。

    設定授權的抓取端點

    如果抓取目標需要授權,您可以設定收集器,使用正確的授權類型並提供任何相關 Secret。

    Google Cloud Managed Service for Prometheus 支援下列授權類型:

    mTLS

    mTLS 通常是在零信任環境中設定,例如 Istio 服務網格或 Cloud Service Mesh。

    在 PodMonitoring 或 ClusterPodMonitoring 資源中設定 mTLS,只會為 Managed Service for Prometheus 收集器設定用戶端 TLS 行為,不會在工作負載上啟用 TLS。設定 mTLS 抓取作業前,請確認目標工作負載使用 TLS 伺服器憑證,透過 HTTPS 提供指標端點,並驗證收集器的用戶端憑證。如果工作負載透過未加密的 HTTP 提供指標,HTTPS 抓取作業會失敗,並顯示 http: server gave HTTP response to HTTPS client 錯誤。如需疑難排解步驟,請參閱「透過 HTTPS 抓取時通訊協定不相符」。

    如要啟用抓取端點,並使用 mTLS 保護,請將 PodMonitoring 資源中的 Spec.Endpoints[].Scheme 欄位設為 https。雖然不建議這麼做,但您可以將 PodMonitoring 資源中的 Spec.Endpoints[].tls.insecureSkipVerify 欄位設為 true,略過驗證憑證授權單位。或者,您也可以設定 Managed Service for Prometheus,從 Secret 資源載入憑證和金鑰。

    舉例來說,下列 Secret 資源包含用戶端 (cert)、私密金鑰 (key) 和憑證授權單位 (ca) 憑證的金鑰:

    kind: Secret
    metadata:
      name: secret-example
    stringData:
      cert: ********
      key: ********
      ca: ********
    

    授予 Managed Service for Prometheus 收集器存取該 Secret 資源的權限:

    apiVersion: rbac.authorization.k8s.io/v1
    kind: Role
    metadata:
      name: secret-example-read
    rules:
    - resources:
      - secrets
      apiGroups: [""]
      verbs: ["get", "list", "watch"]
      resourceNames: ["secret-example"]
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: RoleBinding
    metadata:
      name: gmp-system:collector:secret-example-read
      namespace: default
    roleRef:
      name: secret-example-read
      kind: Role
      apiGroup: rbac.authorization.k8s.io
    subjects:
    - name: collector
      namespace: gmp-system
      kind: ServiceAccount
    

    在 GKE Autopilot 叢集上,這看起來像:

    apiVersion: rbac.authorization.k8s.io/v1
    kind: Role
    metadata:
      name: secret-example-read
    rules:
    - resources:
      - secrets
      apiGroups: [""]
      verbs: ["get", "list", "watch"]
      resourceNames: ["secret-example"]
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: RoleBinding
    metadata:
      name: gmp-system:collector:secret-example-read
      namespace: default
    roleRef:
      name: secret-example-read
      kind: Role
      apiGroup: rbac.authorization.k8s.io
    subjects:
    - name: collector
      namespace: gke-gmp-system
      kind: ServiceAccount
    

    如要設定使用先前 Secret 資源的 PodMonitoring 資源,請修改資源,新增 scheme 和 tls 區段:

    apiVersion: monitoring.googleapis.com/v1
    kind: PodMonitoring
    metadata:
      name: prom-example
    spec:
      selector:
        matchLabels:
          app.kubernetes.io/name: prom-example
      endpoints:
      - port: metrics
        interval: 30s
        scheme: https
        tls:
          ca:
            secret:
              name: secret-example
              key: ca
          cert:
            secret:
              name: secret-example
              key: cert
          key:
            secret:
              name: secret-example
              key: key
    

    如要查看所有 Managed Service for Prometheus mTLS 選項的參考說明文件,請參閱 API 參考文件。

    BasicAuth

    如要啟用抓取端點,並使用 BasicAuth 保護,請在 PodMonitoring 資源中設定 Spec.Endpoints[].BasicAuth 欄位,並填入使用者名稱和密碼。如需其他 HTTP 授權標頭類型,請參閱「HTTP 授權標頭」。

    舉例來說,下列 Secret 資源包含用於儲存密碼的金鑰:

    kind: Secret
    metadata:
      name: secret-example
    stringData:
      password: ********
    

    授予 Managed Service for Prometheus 收集器存取該 Secret 資源的權限:

    apiVersion: rbac.authorization.k8s.io/v1
    kind: Role
    metadata:
      name: secret-example-read
    rules:
    - resources:
      - secrets
      apiGroups: [""]
      verbs: ["get", "list", "watch"]
      resourceNames: ["secret-example"]
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: RoleBinding
    metadata:
      name: gmp-system:collector:secret-example-read
      namespace: default
    roleRef:
      name: secret-example-read
      kind: Role
      apiGroup: rbac.authorization.k8s.io
    subjects:
    - name: collector
      namespace: gmp-system
      kind: ServiceAccount
    

    在 GKE Autopilot 叢集上,這看起來像:

    apiVersion: rbac.authorization.k8s.io/v1
    kind: Role
    metadata:
      name: secret-example-read
    rules:
    - resources:
      - secrets
      apiGroups: [""]
      verbs: ["get", "list", "watch"]
      resourceNames: ["secret-example"]
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: RoleBinding
    metadata:
      name: gmp-system:collector:secret-example-read
      namespace: default
    roleRef:
      name: secret-example-read
      kind: Role
      apiGroup: rbac.authorization.k8s.io
    subjects:
    - name: collector
      namespace: gke-gmp-system
      kind: ServiceAccount
    

    如要設定使用先前 Secret 資源和使用者名稱 foo 的 PodMonitoring 資源,請修改資源,新增 basicAuth 區段:

    apiVersion: monitoring.googleapis.com/v1
    kind: PodMonitoring
    metadata:
      name: prom-example
    spec:
      selector:
        matchLabels:
          app.kubernetes.io/name: prom-example
      endpoints:
      - port: metrics
        interval: 30s
        basicAuth:
          username: foo
          password:
            secret:
              name: secret-example
              key: password
    

    如要查看所有 Managed Service for Prometheus BasicAuth 選項的參考文件,請參閱 API 參考文件。

    HTTP 授權標頭

    如要啟用使用 HTTP 授權標頭保護的抓取端點,請在 PodMonitoring 資源中設定 Spec.Endpoints[].Authorization 欄位,並指定類型和憑證。如果是 BasicAuth 端點,請改用 BasicAuth 設定。

    舉例來說,下列 Secret 資源包含用於儲存憑證的金鑰:

    kind: Secret
    metadata:
      name: secret-example
    stringData:
      credentials: ********
    

    授予 Managed Service for Prometheus 收集器存取該 Secret 資源的權限:

    apiVersion: rbac.authorization.k8s.io/v1
    kind: Role
    metadata:
      name: secret-example-read
    rules:
    - resources:
      - secrets
      apiGroups: [""]
      verbs: ["get", "list", "watch"]
      resourceNames: ["secret-example"]
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: RoleBinding
    metadata:
      name: gmp-system:collector:secret-example-read
      namespace: default
    roleRef:
      name: secret-example-read
      kind: Role
      apiGroup: rbac.authorization.k8s.io
    subjects:
    - name: collector
      namespace: gmp-system
      kind: ServiceAccount
    

    在 GKE Autopilot 叢集上,這看起來像:

    apiVersion: rbac.authorization.k8s.io/v1
    kind: Role
    metadata:
      name: secret-example-read
    rules:
    - resources:
      - secrets
      apiGroups: [""]
      verbs: ["get", "list", "watch"]
      resourceNames: ["secret-example"]
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: RoleBinding
    metadata:
      name: gmp-system:collector:secret-example-read
      namespace: default
    roleRef:
      name: secret-example-read
      kind: Role
      apiGroup: rbac.authorization.k8s.io
    subjects:
    - name: collector
      namespace: gke-gmp-system
      kind: ServiceAccount
    

    如要設定使用先前 Secret 資源和 Bearer 類型的 PodMonitoring 資源,請修改資源,新增 authorization 區段:

    apiVersion: monitoring.googleapis.com/v1
    kind: PodMonitoring
    metadata:
      name: prom-example
    spec:
      selector:
        matchLabels:
          app.kubernetes.io/name: prom-example
      endpoints:
      - port: metrics
        interval: 30s
        authorization:
          type: Bearer
          credentials:
            secret:
              name: secret-example
              key: credentials
    

    如要查看所有 Managed Service for Prometheus HTTP 授權標頭選項的參考文件,請參閱 API 參考文件。

    OAuth 2

    如要啟用使用 OAuth 2 保護的抓取端點,您必須在 PodMonitoring 資源中設定 Spec.Endpoints[].OAuth2 欄位。

    舉例來說,下列 Secret 資源包含儲存用戶端密鑰的鍵:

    kind: Secret
    metadata:
      name: secret-example
    stringData:
      clientSecret: ********
    

    授予 Managed Service for Prometheus 收集器存取該 Secret 資源的權限:

    apiVersion: rbac.authorization.k8s.io/v1
    kind: Role
    metadata:
      name: secret-example-read
    rules:
    - resources:
      - secrets
      apiGroups: [""]
      verbs: ["get", "list", "watch"]
      resourceNames: ["secret-example"]
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: RoleBinding
    metadata:
      name: gmp-system:collector:secret-example-read
      namespace: default
    roleRef:
      name: secret-example-read
      kind: Role
      apiGroup: rbac.authorization.k8s.io
    subjects:
    - name: collector
      namespace: gmp-system
      kind: ServiceAccount
    

    在 GKE Autopilot 叢集上,這看起來像:

    apiVersion: rbac.authorization.k8s.io/v1
    kind: Role
    metadata:
      name: secret-example-read
    rules:
    - resources:
      - secrets
      apiGroups: [""]
      verbs: ["get", "list", "watch"]
      resourceNames: ["secret-example"]
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: RoleBinding
    metadata:
      name: gmp-system:collector:secret-example-read
      namespace: default
    roleRef:
      name: secret-example-read
      kind: Role
      apiGroup: rbac.authorization.k8s.io
    subjects:
    - name: collector
      namespace: gke-gmp-system
      kind: ServiceAccount
    

    如要設定 PodMonitoring 資源,使用用戶端 ID 為 foo、權杖網址為 example.com/token 的先前 Secret 資源,請修改資源,新增 oauth2 區段:

    apiVersion: monitoring.googleapis.com/v1
    kind: PodMonitoring
    metadata:
      name: prom-example
    spec:
      selector:
        matchLabels:
          app.kubernetes.io/name: prom-example
      endpoints:
      - port: metrics
        interval: 30s
        oauth2:
          clientID: foo
          clientSecret:
            secret:
              name: secret-example
              key: password
          tokenURL: example.com/token
    

    如要查看所有 Managed Service for Prometheus OAuth 2 選項的參考文件,請參閱 API 參考文件。

    使用 Terraform 設定目標抓取

    您可以使用 kubernetes_manifest Terraform 資源類型或 kubectl_manifest Terraform 資源類型,自動建立及管理 PodMonitoring 和 ClusterPodMonitoring 資源,這兩種資源類型都能讓您指定任意自訂資源。

    如要瞭解如何搭配使用 Google Cloud 與 Terraform,請參閱「Terraform 搭配 Google Cloud」。

    篩選匯出的指標

    如果收集的資料量很多,您可能需要避免部分時間序列傳送至 Managed Service for Prometheus,藉此降低成本。方法是使用 Prometheus 重新標籤規則,並對許可清單執行 keep 動作,或對拒絕清單執行 drop 動作。如果是代管收集,這項規則會位於 PodMonitoring 或 ClusterPodMonitoring 資源的 metricRelabeling 區段。

    舉例來說,下列指標重新標籤規則會篩除開頭為 foo_bar_、foo_baz_ 或 foo_qux_ 的任何指標:

      metricRelabeling:
      - action: drop
        regex: foo_(bar|baz|qux)_.+
        sourceLabels: [__name__]
    

    Cloud Monitoring 的「Metrics Management」(指標管理) 頁面提供相關資訊,可協助您控管可計費指標的支出,同時不影響觀測能力。「Metrics Management」(指標管理) 頁面會回報下列資訊:

    • 依位元組和樣本計費的擷取量,涵蓋指標網域和個別指標。
    • 指標標籤和基數的資料。
    • 每項指標的讀取次數。
    • 警告政策和自訂資訊主頁中的指標使用情形。
    • 指標寫入錯誤率。

    您也可以使用「指標管理」頁面排除不必要的指標,省去擷取這些指標的費用。如要進一步瞭解「Metrics Management」(指標管理) 頁面,請參閱「查看及管理指標用量」。

    如需更多降低費用的建議,請參閱「費用控管和歸因」。

    抓取 Kubelet 和 cAdvisor 指標

    Kubelet 會公開自身指標,以及節點上執行容器的 cAdvisor 指標。您可以編輯 OperatorConfig 資源,設定代管收集功能,抓取 Kubelet 和 cAdvisor 指標。如需操作說明,請參閱 Kubelet 和 cAdvisor 的匯出工具說明文件。

    轉換現有的 prometheus-operator 資源

    您通常可以將現有的 prometheus-operator 資源,轉換為 Managed Service for Prometheus 代管收集 PodMonitoring 和 ClusterPodMonitoring 資源。

    例如,ServiceMonitor 資源會定義一組服務的監控作業。PodMonitoring 資源提供 ServiceMonitor 資源提供的部分欄位。您可以對應欄位,將 ServiceMonitor CR 轉換為 PodMonitoring CR,如下表所示:

    monitoring.coreos.com/v1
    ServiceMonitor
    相容性
     
    monitoring.googleapis.com/v1
    PodMonitoring
    .ServiceMonitorSpec.Selector Identical .PodMonitoringSpec.Selector
    .ServiceMonitorSpec.Endpoints[] .TargetPort 對應至 .Port
    .Path:相容
    .Interval:相容
    .Timeout:相容
    .PodMonitoringSpec.Endpoints[]
    .ServiceMonitorSpec.TargetLabels PodMonitor 必須指定:
    .FromPod[].From Pod 標籤
    .FromPod[].To 目標標籤
    .PodMonitoringSpec.TargetLabels

    以下是 ServiceMonitor CR 範例,粗體內容會在轉換時替換,斜體內容則會直接對應:

    apiVersion: monitoring.coreos.com/v1
    kind: ServiceMonitor
    metadata:
      name: example-app
    spec:
      selector:
        matchLabels:
          app: example-app
      endpoints:
      - targetPort: web
        path: /stats
        interval: 30s
      targetLabels:
      - foo
    

    以下是類似的 PodMonitoring CR,假設您的服務和 Pod 標有 app=example-app。如果這項假設不適用,則必須使用基礎 Service 資源的標籤選取器。

    轉換時,粗體內容會替換為:

    apiVersion: monitoring.googleapis.com/v1
    kind: PodMonitoring
    metadata:
      name: example-app
    spec:
      selector:
        matchLabels:
          app: example-app
      endpoints:
      - port: web
        path: /stats
        interval: 30s
      targetLabels:
        fromPod:
        - from: foo # pod label from example-app Service pods.
          to: foo
    

    您隨時可以繼續使用現有的 prometheus-operator 資源和部署設定,只要使用自行部署的收集器,而非代管收集器即可。您可以查詢這兩種收集器類型傳送的指標,因此建議您在現有的 Prometheus 部署作業中使用自行部署的收集器,並在新的 Prometheus 部署作業中使用代管收集器。

    保留標籤

    Managed Service for Prometheus 會自動為所有收集到的指標新增下列標籤。這些標籤用於在 Monarch 中明確識別資源:

    • project_id:與指標相關聯的 Google Cloud 專案 ID。
    • location:資料儲存的實體位置 (Google Cloud 區域)。這個值通常是 GKE 叢集的區域。如果資料是從 AWS 或地端部署收集,則值可能是最接近的 Google Cloud 區域。
    • cluster:與指標相關聯的 Kubernetes 叢集名稱。
    • namespace:與指標相關聯的 Kubernetes 命名空間名稱。
    • job:Prometheus 目標的工作標籤 (如已知);規則評估結果可能為空白。
    • instance:Prometheus 目標的執行個體標籤 (如已知);規則評估結果可能為空白。

    雖然不建議在 Google Kubernetes Engine 上執行時覆寫 project_id、location 和 cluster 標籤,但您可以將這些標籤新增為 args,覆寫 operator.yaml 中的 Deployment 資源。如果您使用任何保留標籤做為指標標籤,Managed Service for Prometheus 會自動加上 exported_ 前置字串,重新加上標籤。這項行為與上游 Prometheus 處理與保留標籤衝突的方式一致。

    壓縮設定

    如果 PodMonitoring 資源過多,ConfigMap 空間可能會用盡。如要修正這個問題,請在OperatorConfig 資源中啟用 gzip 壓縮:

      apiVersion: monitoring.googleapis.com/v1
      kind: OperatorConfig
      metadata:
        namespace: gmp-public
        name: config
      features:
        config:
          compression: gzip
    

    為代管收集啟用垂直自動調度 Pod 資源 (VPA)

    您可以使用垂直自動調度 Pod 資源 (VPA) 功能,為 Managed Service for Prometheus 工作負載動態分配資源。動態分配可解決下列問題:

    • 叢集中的 collector、rule-evaluator、alertmanager 或 gmp-operator Pod 發生記憶體不足 (OOM) 錯誤。
    • 這些工作負載的預設資源要求和限制不符合您的需求。

    在 OperatorConfig 資源上設定 scaling.vpa.enabled: true 欄位時,運算子會為 collector、rule-evaluator、alertmanager 和 gmp-operator Pod 部署 VerticalPodAutoscaler 資訊清單。這些資訊清單會部署在標準叢集的 gmp-system 命名空間和 Autopilot 叢集的 gke-gmp-system 命名空間中,並根據用量自動設定資源要求和限制。

    如要為 Managed Service for Prometheus 工作負載啟用 VPA,請執行下列指令:

    kubectl -n gmp-public patch operatorconfig/config -p '{"scaling":{"vpa":{"enabled":true}}}' --type=merge
    

    如果指令成功執行,運算子會為 Managed Service for Prometheus 工作負載設定垂直自動調度 Pod 資源。OOM 錯誤會導致資源限制立即增加。如果沒有 OOM 錯誤,系統通常會在 24 小時內首次調整資源要求和限制。

    嘗試啟用 VPA 時,可能會收到以下錯誤訊息:

    vertical pod autoscaling is not available - install vpa support and restart the operator

    如要解決這項錯誤,請先在叢集層級啟用垂直自動調度 Pod 資源:

    1. 前往Google Cloud 控制台的「Kubernetes Engine - Clusters」(Kubernetes Engine - 叢集) 頁面。

      在 Google Cloud 控制台中,前往「Kubernetes clusters」(Kubernetes 叢集) 頁面:

      前往「Kubernetes clusters」(Kubernetes 叢集)

      如果您是使用搜尋列尋找這個頁面,請選取子標題為「Kubernetes Engine」的結果。

    2. 選取要修改的叢集。

    3. 在「Automation」(自動化) 區段中,編輯「Vertical Pod Autoscaling」(垂直自動調度 Pod 資源) 選項的值。

    4. 勾選「Enable Vertical Pod Autoscaling」(啟用垂直自動調度 Pod 資源) 核取方塊,然後按一下「Save changes」(儲存變更)。變更之後,叢集會重新啟動。在這個過程中,運算子會重新啟動。

    5. 請重試下列指令:kubectl -n gmp-public patch operatorconfig/config -p '{"scaling":{"vpa":{"enabled":true}}}' --type=merge,為 Managed Service for Prometheus 啟用 VPA。

    如要確認 OperatorConfig 資源已成功編輯,請使用 kubectl -n gmp-public edit operatorconfig config 指令開啟資源。如果成功,OperatorConfig 會包含下列粗體部分:

    apiVersion: monitoring.googleapis.com/v1
    kind: OperatorConfig
    metadata:
      namespace: gmp-public
      name: config
    scaling:
      vpa:
        enabled: true
    

    如果您已在叢集層級啟用垂直自動調度 Pod 資源功能,但仍看到 vertical pod autoscaling is not available - install vpa support and restart the operator 錯誤,則 gmp-operator Pod 可能需要重新評估叢集設定。執行下列其中一個步驟:

    • 如果您執行的是標準叢集,請執行下列指令,重新建立 Pod:

      kubectl -n gmp-system rollout restart deployment/gmp-operator
      

      gmp-operator Pod 重新啟動後,請按照上述步驟再次修補 OperatorConfig。

    • 如果您執行的是 Autopilot 叢集,則無法手動重新啟動 gmp-operator Pod。為 Autopilot 叢集中的代管收集器啟用 VPA 後,VPA 會自動淘汰並重新建立收集器 Pod,以套用新的資源要求,您不需要重新啟動叢集。如果啟用 VPA 後看到 vertical pod autoscaling is not available 錯誤,或在啟用 Managed Service for Prometheus 的 VPA 時遇到其他問題,請聯絡支援團隊。

    垂直自動調度 Pod 資源功能最適合用於擷取穩定數量的樣本,並平均分配到各個節點。如果指標負載不規則或不穩定,或節點之間的指標負載差異很大,VPA 可能不是有效的解決方案。

    詳情請參閱「GKE 的垂直自動調度 Pod 資源功能」。

    設定 statsd_exporter 和其他集中回報報表指標的匯出工具

    如果您使用 Prometheus 的 statsd_exporter、Istio 的 Envoy、SNMP 匯出工具、Prometheus Pushgateway、kube-state-metrics,或有類似的匯出工具,可代表環境中執行的其他資源中介及回報指標,則需要對匯出工具進行一些小幅變更,才能與 Managed Service for Prometheus 搭配使用。

    如需設定這些匯出工具的操作說明,請參閱疑難排解一節中的附註。

    清理作業

    如要停用透過 gcloud 或 GKE UI 部署的代管收集功能,請採取下列任一做法:

    • 執行下列指令:

      gcloud container clusters update CLUSTER_NAME --disable-managed-prometheus
      
    • 使用 GKE UI:

      1. 在 Google Cloud 控制台中,依序選取「Kubernetes Engine」和「Clusters」(叢集)。

      2. 找出要停用代管收集功能的叢集,然後按一下叢集名稱。

      3. 在「Details」(詳細資料) 分頁中,向下捲動至「Features」(功能),然後使用編輯按鈕將狀態變更為「Disabled」(已停用)。

    如要停用使用 Terraform 部署的代管收集功能,請在 google_container_cluster 資源的 managed_prometheus 區段中指定 enabled = false。

    如要停用使用 kubectl 部署的代管收集功能,請執行下列指令:

    kubectl delete -f https://raw.githubusercontent.com/GoogleCloudPlatform/prometheus-engine/v0.19.4/manifests/operator.yaml
    

    停用代管收集功能後,叢集就會停止將新資料傳送至 Managed Service for Prometheus。這項操作不會刪除系統中已儲存的任何現有指標資料。

    停用代管收集功能也會刪除 gmp-public 命名空間和其中的任何資源,包括在該命名空間中安裝的任何匯出工具。

    在 GKE 以外執行代管收集

    在 GKE 環境中,您無須進行其他設定,即可執行代管收集作業。在其他 Kubernetes 環境中,您必須明確提供憑證、包含指標的 project-id 值、儲存指標的 location 值 (Google Cloud 區域),以及儲存收集器執行所在叢集名稱的 cluster 值。

    由於 gcloud 無法在 Google Cloud 環境外運作,因此您必須改為使用 kubectl 部署。與 gcloud 不同,使用 kubectl 部署代管收集功能時,叢集不會在新版本推出時自動升級。請記得查看版本頁面,瞭解是否有新版本,並使用新版本重新執行 kubectl 指令,手動升級。

    如要提供服務帳戶金鑰,請按照「明確提供憑證」一節的說明,修改 operator.yaml 中的 OperatorConfig 資源。如要提供 project-id、location 和 cluster 值,請將這些值新增為 operator.yaml 內 Deployment 資源的 args。

    建議您根據預計的讀取租用模式選擇 project-id。根據您日後打算如何使用指標範圍整理讀取作業,選擇要儲存指標的專案。如果不在意,可以將所有內容放入一個專案。

    如果是location,建議選擇最靠近部署作業的 Google Cloud 區域。所選 Google Cloud 區域與部署作業的距離越遠,寫入延遲時間就越長,也越容易受到潛在網路問題的影響。建議您參閱這份多雲端區域清單。如果不在意,可以將所有內容放入一個 Google Cloud 區域。無法將「global」設為位置。

    針對 cluster,建議您選擇部署運算子的叢集名稱。

    正確設定後,OperatorConfig 應如下所示:

        apiVersion: monitoring.googleapis.com/v1
        kind: OperatorConfig
        metadata:
          namespace: gmp-public
          name: config
        collection:
          credentials:
            name: gmp-test-sa
            key: key.json
        rules:
          credentials:
            name: gmp-test-sa
            key: key.json
    

    Deployment 資源應如下所示:

    apiVersion: apps/v1
    kind: Deployment
    ...
    spec:
      ...
      template:
        ...
        spec:
          ...
          containers:
          - name: operator
            ...
            args:
            - ...
            - "--project-id=PROJECT_ID"
            - "--cluster=CLUSTER_NAME"
            - "--location=REGION"
    

    這個範例假設您已將 REGION 變數設為 us-central1 等值。

    在 Google Cloud 以外執行 Managed Service for Prometheus 會產生資料移轉費用。將資料移轉至 Google Cloud會產生費用,從其他雲端移轉資料也可能產生費用。您可以透過 OperatorConfig 啟用 gzip 壓縮功能,盡可能降低這些費用。將粗體文字新增至資源:

        apiVersion: monitoring.googleapis.com/v1
        kind: OperatorConfig
        metadata:
          namespace: gmp-public
          name: config
        collection:
          compression: gzip
          ...
    

    進一步瞭解代管收集自訂資源

    如要查看所有 Managed Service for Prometheus 自訂資源的參考文件,請參閱 prometheus-engine/doc/api 參考資料。

    後續步驟