ワークロードから指標を収集する

このページでは、モニタリングとデータ オブザーバビリティを容易にするために、Google Distributed Cloud(GDC)エアギャップ環境のワークロードから指標をスクレイピングするプロセスについて説明します。

コンポーネントが生成する指標をスクレイピングして収集できます。モニタリング プラットフォームは、Distributed Cloud プロジェクトの Namespace 内で実行されているワークロードから指標をスクレイピングするためのカスタム API を提供します。指標をスクレイピングするには、Management API サーバーのプロジェクト Namespace に MonitoringTarget カスタム リソースをデプロイします。このリソースをデプロイすると、モニタリング プラットフォームがデータ収集を開始します。

MonitoringTarget カスタム リソースは、モニタリング パイプラインにプロジェクト内の指定された Pod をスクレイピングするように指示します。これらの Pod は、OpenMetrics などの Prometheus 公開形式で指標を配信する HTTP エンドポイントを公開する必要があります。スクレイピングされた指標は、プロジェクトの Grafana インスタンスに表示され、アプリケーションの運用状態に関する分析情報が提供されます。

MonitoringTarget カスタム リソースを構成するには、指標収集のプロジェクト Namespace 内の Pod を指定する必要があります。スクレイピングの頻度、Pod の指標エンドポイント、ラベル、アノテーションなど、さまざまな設定をカスタマイズできます。

始める前に

MonitoringTarget カスタム リソースを管理するために必要な権限を取得するには、関連する MonitoringTarget ロールのいずれかを付与するよう組織 IAM 管理者またはプロジェクト IAM 管理者に依頼してください。

必要なアクセスレベルと権限に応じて、組織またはプロジェクトでこのリソースの作成者、編集者、閲覧者のロールを取得できます。詳細については、IAM 権限を準備するをご覧ください。

MonitoringTarget カスタム リソースを構成する

MonitoringTarget カスタム リソースは、モニタリング プラットフォームに指標の収集元を伝えます。指標を収集する Pod、それらの Pod の指標エンドポイント、スクレイピング頻度、その他の設定を指定できます。

このリソースは、次の構成を定義します。

  • ターゲット: 指標を公開するプロジェクト内の Pod とそのエンドポイント。
  • スクレイピング間隔: 選択したエンドポイントから指標を取得する頻度。
  • ラベルのカスタマイズ: 指標のラベル変更を含む省略可能なルール。

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 ファイルは、選択したすべての 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 と同じ Namespace 内の Management API サーバーに MonitoringTarget 構成を適用します。

    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 構成で、コンテナの Deployment ファイルに追加したアノテーションを podMetricsEndpoints セクションで指定します。この仕様は、選択した 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 と同じ Namespace 内の Management API サーバーに MonitoringTarget 構成を適用します。

    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: プロジェクトの Namespace。
  • MONITORING_TARGET_NAME: MonitoringTarget 定義ファイルの名前。