收集工作負載的指標

本頁說明如何從 Google Distributed Cloud (GDC) 實體隔離環境中的工作負載擷取指標,以利監控及資料可觀測性。

您可以擷取並收集元件隨時間產生的指標。監控平台提供自訂 API,可從 Distributed Cloud 專案命名空間中執行的工作負載擷取指標。如要擷取指標,請將 MonitoringTarget 自訂資源部署至 Management API 伺服器中的專案命名空間。部署這項資源後,監控平台就會開始收集資料。

MonitoringTarget 自訂資源會引導監控管道,擷取專案中指定的 Pod。這些 Pod 必須公開 HTTP 端點,以 Prometheus 說明格式 (例如 OpenMetrics) 提供指標。然後,系統會在專案的 Grafana 執行個體中顯示擷取的指標, 讓您深入瞭解應用程式的運作狀態。

如要設定 MonitoringTarget 自訂資源,請在專案命名空間中指定要收集指標的 Pod。您可以自訂各種設定,包括擷取頻率、Pod 的指標端點、標籤和註解。

事前準備

如要取得管理 MonitoringTarget 自訂資源所需的權限,請要求機構 IAM 管理員或專案 IAM 管理員授予您相關的 MonitoringTarget 角色。

視存取層級和所需權限而定,您可能會在機構或專案中取得這項資源的建立者、編輯者或檢視者角色。詳情請參閱「準備 IAM 權限」。

設定 MonitoringTarget 自訂資源

MonitoringTarget 自訂資源會告知監控平台要從何處收集指標。您可以指定要收集指標的 Pod、這些 Pod 的指標端點、擷取頻率,以及任何其他設定。

這項資源定義下列設定:

  • 目標:專案中的 Pod 和端點,會公開指標。
  • 擷取間隔:從所選端點擷取指標的頻率。
  • 標籤自訂:可選規則,可修改指標的任何標籤。

在自訂資源中指定指標端點時,請選擇下列其中一種方法:MonitoringTarget

  • 靜態端點:您在 MonitoringTarget 設定中明確宣告端點 (連接埠、路徑、架構)。
  • 註解:系統會從容器 Deployment 檔案中的註解擷取 Pod 指標端點資訊。如果每個 Pod 都有不同的端點,這個方法會更具彈性。

靜態端點

請按照下列步驟,在靜態定義的端點上公開所選 Pod 的指標:

  1. 找出要收集指標以進行監控的 Distributed Cloud 專案。

  2. 在 Pod 的規格中,於 containerPort 欄位宣告提供指標的通訊埠。以下範例說明如何在 Pod 的規格中宣告連接埠 2112

    # ...
    spec:
      template:
        spec:
          containers:
          - name: your-container-name
            ports:
            - containerPort: 2112
    # ...
    
  3. MonitoringTarget 設定中,於 podMetricsEndpoints 區段指定端點詳細資料 (連接埠、路徑、架構),與您在 Pod 規格中公開的連接埠相符。

    下列 YAML 檔案範例顯示設定,其中每個選取的 Pod 都必須在相同端點 http://your-container-name:2112/metrics 上公開指標:MonitoringTarget

    apiVersion: monitoring.gdc.goog/v1
    kind: MonitoringTarget
    metadata:
      # Choose the same namespace as the workload pods.
      namespace: your-project-namespace
      name: your-container-name
    spec:
      selector:
          # Choose pod labels to consider for this job.
          # Optional: Map of key-value pairs.
          # Default: No filtering by label.
          # To consider every pod in the project namespace, remove selector fields.
        matchLabels:
          app: your-app-label
      podMetricsEndpoints:
        port:
          value: 2112
        path:
          # Choose any value for your endpoint.
          # The /metrics value is an example.
          value: /metrics
        scheme:
          value: http
    
  4. 在與目標 Pod 相同的命名空間中,將 MonitoringTarget 設定套用至 Management API 伺服器:

    kubectl --kubeconfig KUBECONFIG_PATH apply -f MONITORING_TARGET_NAME.yaml
    

    更改下列內容:

    • KUBECONFIG_PATH:管理 API 伺服器的 kubeconfig 檔案路徑。
    • MONITORING_TARGET_NAMEMonitoringTarget 定義檔案的名稱。

監控平台會開始收集指標。

註解

如果每個 Pod 都有不同的端點,請按照下列步驟使用註解公開指標:

  1. 找出要收集指標以進行監控的 Distributed Cloud 專案。

  2. 在容器的 Deployment 檔案的 annotations 區段中新增下列註解:

    • prometheus.io/path
    • prometheus.io/port
    • prometheus.io/scheme

    以下範例顯示連接埠 2112 上指標的註解:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: your-container-name
      namespace: your-project-namespace
      labels:
        app: your-app-label
      annotations:
        # These annotations are not required. They demonstrate selecting
        # pod metric endpoints through annotations.
        prometheus.io/path: /metrics
        prometheus.io/port: \"2112\"
        prometheus.io/scheme: http
    
  3. MonitoringTarget 設定中,於 podMetricsEndpoints 區段指定您新增至容器 Deployment 檔案的註解。這項規格會指示自訂資源從所選 Pod 的註解中收集指標端點資訊。

    下列 YAML 檔案範例顯示使用註解的 MonitoringTarget 設定:

    apiVersion: monitoring.gdc.goog/v1
    kind: MonitoringTarget
    metadata:
    metadata:
      # Choose the same namespace as the workload pods.
      namespace: your-project-namespace
      name: your-container-name
    spec:
      selector:
        matchLabels:
          app: your-app-label
      podMetricsEndpoints:
        port:
          annotation: prometheus.io/port
        path:
          annotation: prometheus.io/path
        scheme:
          annotation: prometheus.io/scheme
    
  4. 在與目標 Pod 相同的命名空間中,將 MonitoringTarget 設定套用至 Management API 伺服器:

    kubectl --kubeconfig KUBECONFIG_PATH apply -f MONITORING_TARGET_NAME.yaml
    

    更改下列內容:

    • KUBECONFIG_PATH:管理 API 伺服器的 kubeconfig 檔案路徑。
    • MONITORING_TARGET_NAMEMonitoringTarget 定義檔案的名稱。

監控平台會開始收集指標。

如需其他欄位和選項,請參閱完整MonitoringTarget規格API 參考說明文件

完成MonitoringTarget規格

下列 YAML 檔案範例顯示 MonitoringTarget 自訂資源的完整規格。如需更多資訊和欄位完整說明,請參閱 API 參考說明文件

apiVersion: monitoring.gdc.goog/v1
kind: MonitoringTarget
metadata:
  # Choose the same namespace as the workload pods.
  namespace: PROJECT_NAMESPACE
  name: MONITORING_TARGET_NAME
spec:
  # Choose matching pattern that identifies pods for this job.
  # Optional
  # Relationship between different selectors: AND
  selector:
    # Choose clusters to consider for this job.
    # Optional: List
    # Default: All clusters applicable to this project.
    # Relationship between different list elements: OR
    matchClusters:
    - string

    # Choose pod labels to consider for this job.
    # Optional: Map of key-value pairs.
    # Default: No filtering by label.
    # Relationship between different pairs: AND
    matchLabels:
      key1: value1

    # Choose annotations to consider for this job.
    # Optional: Map of key-value pairs
    # Default: No filtering by annotation
    # Relationship between different pairs: AND
    matchAnnotations:
      key1: value1

  # Configure the endpoint exposed for this job.
  podMetricsEndpoints:
    # Choose a port either through static value or annotation.
    # Optional
    # Annotation takes priority.
    # Default: static port 80
    port:
      value: integer
      annotation: string

    # Choose a path either through static value or annotation.
    # Optional
    # Annotation takes priority
    # Default: static path /metrics
    path:
      value: string
      annotation: string

    # Choose a scheme either through a static value (http or https) or annotation.
    # Optional
    # Annotation takes priority
    # Default: static scheme http
    scheme:
      value: string
      annotation: string

    # Choose the frequency to scrape the metrics endpoint defined in podMetricsEndpoints
    # Optional
    # Default: 60s
    scrapeInterval: string

    # Dynamically rewrite the label set of a target before it gets scraped.
    # https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config
    # Optional
    # Default: No filtering by label
    metricsRelabelings:
    - sourceLabels:
      - string
      separator: string
      regex: string
      action: string
      targetLabel: string
      replacement: string

更改下列內容:

  • PROJECT_NAMESPACE:您的專案命名空間。
  • MONITORING_TARGET_NAMEMonitoringTarget 定義檔案的名稱。