從工作負載收集指標

Google Distributed Cloud (GDC) 實體隔離方案可讓您持續擷取及收集工作負載的指標,監控應用程式效能並維持資料觀測能力。如要開始收集指標,請在 Management API 伺服器上,將 MonitoringTarget 自訂資源部署至專案命名空間。

MonitoringTarget 自訂資源會指示監控管道,抓取以 Prometheus 曝光格式 (例如 OpenMetrics) 公開 HTTP 端點的指定 Pod。您可以自訂目標 Pod 的抓取頻率、端點設定、標籤和註解。專案的 Grafana 執行個體會顯示收集到的指標,方便您分析應用程式的運作狀態。

事前準備

如要管理 MonitoringRule 自訂資源,請向管理員要求必要權限。

必要的 IAM 角色

請與 IAM 專案管理員聯絡,要求下列角色:

  • MonitoringRule 建立者 (monitoringrule-creator):在專案命名空間中建立 MonitoringRule 自訂資源。
  • MonitoringRule 編輯者 (monitoringrule-editor):編輯或修改專案命名空間中的 MonitoringRule 自訂資源。
  • MonitoringRule 檢視者 (monitoringrule-viewer):查看專案命名空間中的 MonitoringRule 自訂資源。

設定 MonitoringTarget 自訂資源

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

這項資源定義了下列設定:

  • 目標:專案中公開指標的 Pod 和端點。
  • Scrape interval:從所選端點提取指標的頻率。
  • 標籤自訂:選用規則,可對指標進行任何標籤修改。

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

  • 靜態端點:您在 MonitoringTarget 設定中明確宣告端點 (通訊埠、路徑、配置)。
  • 註解:Pod 指標端點資訊是從容器 Deployment 檔案中的註解擷取。如果每個 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 檔案顯示 MonitoringTarget 設定範例,其中每個所選 Pod 都必須在同一個端點 http://your-container-name:2112/metrics 上公開指標:

    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. 將 MonitoringTarget 設定套用至 Management API 伺服器,並與目標 Pod 位於相同命名空間:

    kubectl --kubeconfig KUBECONFIG_PATH apply -f MONITORING_TARGET_NAME.yaml
    

    更改下列內容:

    • KUBECONFIG_PATH:Management API 伺服器的 kubeconfig 檔案路徑。
    • MONITORING_TARGET_NAME:MonitoringTarget 定義檔案的名稱。

監控平台開始收集指標。

註解

如果每個 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. 將 MonitoringTarget 設定套用至 Management API 伺服器,並與目標 Pod 位於相同命名空間:

    kubectl --kubeconfig KUBECONFIG_PATH apply -f MONITORING_TARGET_NAME.yaml
    

    更改下列內容:

    • KUBECONFIG_PATH:Management API 伺服器的 kubeconfig 檔案路徑。
    • MONITORING_TARGET_NAME:MonitoringTarget 定義檔案的名稱。

監控平台開始收集指標。

如需其他欄位和選項,請參閱完整的 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_NAME:MonitoringTarget 定義檔案的名稱。