CRD を使用してコレクションをカスタマイズする (サービスおよびポッド モニター)

Note

Managed Prometheus での CRD サポートは現在プレビュー段階です。

Managed Prometheus を有効にすると、ポッド モニターサービス モニターのカスタム リソース定義 (CRD) が自動的にデプロイされます。 これらのカスタム リソース定義は、グループ名の変更を除き、Prometheus の OSS ポッド モニターOSS サービス モニターと同じカスタム リソース定義 (CRD) です。 クラスターに既存の Prometheus CRD とカスタム リソースがある場合、これらの CRD はアドオンによって作成された CRD と競合しません。 同時に、マネージド Prometheus アドオンでは、OSS Prometheus 用に作成された CRD は取得されません。 この分離は、スクレーピング ジョブの分離を目的として意図的に行われます。

ポッドまたはサービス モニターを作成する

ポッド モニターとサービス モニターのテンプレートを使用し、API 仕様に従ってカスタム リソース (PodMonitorService Monitor) を作成します。 Managed Prometheus によって取得される既存の OSS CR (カスタム リソース) に必要な変更は、API グループ - azmonitoring.coreos.com/v1 のみであることに注意してください。

注 - 処理中に削除されないように、テンプレートで指定されている labelLimit、labelNameLengthLimit、labelValueLengthLimit を必ず使用してください。

ポッドおよびサービス モニターは、次の例のようになるはずです。

ポッド モニターの例

# Note the API version is azmonitoring.coreos.com/v1 instead of monitoring.coreos.com/v1
apiVersion: azmonitoring.coreos.com/v1
kind: PodMonitor

# Can be deployed in any namespace
metadata:
  name: reference-app
  namespace: app-namespace
spec:
  labelLimit: 63
  labelNameLengthLimit: 511
  labelValueLengthLimit: 1023

  # The selector specifies which pods to filter for
  selector:

    # Filter by pod labels
    matchLabels:
      environment: test
    matchExpressions:
      - key: app
        operator: In
        values: [app-frontend, app-backend]

    # [Optional] Filter by pod namespace
    namespaceSelector:
      matchNames: [app-frontend, app-backend]

  # [Optional] Labels on the pod with these keys will be added as labels to each metric scraped
  podTargetLabels: [app, region, environment]

  # Multiple pod endpoints can be specified. Port requires a named port.
  podMetricsEndpoints:
    - port: metrics

サービス モニターの例

# Note the API version is azmonitoring.coreos.com/v1 instead of monitoring.coreos.com/v1
apiVersion: azmonitoring.coreos.com/v1
kind: ServiceMonitor

# Can be deployed in any namespace
metadata:
  name: reference-app
  namespace: app-namespace
spec:
  labelLimit: 63
  labelNameLengthLimit: 511
  labelValueLengthLimit: 1023

  # The selector filters endpoints by service labels.
  selector:
    matchLabels:
      app: reference-app

  # Multiple endpoints can be specified. Port requires a named port.
  endpoints:
  - port: metrics

ポッドまたはサービス モニターをデプロイする

その後、kubectl apply を使用してポッドまたはサービス モニターをデプロイできます。

適用すると、カスタム リソースのエラーが表示され、ポッドやサービス モニターの適用は失敗するはずです。
ポッド モニターの作成が成功すると、次のようになります。

podmonitor.azmonitoring.coreos.com/my-pod-monitor created

サンプル アプリケーションを作成する

ポッド/サービス モニターによって構成される prometheus メトリックを公開するサンプル アプリケーションをデプロイします。

kubectl apply -f https://github.com/Azure/prometheus-collector/blob/main/internal/referenceapp/prometheus-reference-app.yaml

メトリックをスクレイピングするポッド モニターやサービス モニターを作成する

前の手順のサンプル アプリケーションからメトリックをスクレイピングするように構成されたポッド モニターをデプロイします。

ポッド モニター
kubectl apply -f https://github.com/Azure/prometheus-collector/blob/main/otelcollector/deploy/example-custom-resources/pod-monitor/pod-monitor-reference-app.yaml
サービス モニター
kubectl apply -f https://github.com/Azure/prometheus-collector/blob/main/otelcollector/deploy/example-custom-resources/service-monitor/service-monitor-reference-app.yaml

トラブルシューティング

ポッドまたはサービス モニターが正常に適用されると、アドオンでターゲットからのメトリックの収集が自動的に開始されるはずです。 これを確認するには、カスタム リソースの一般的なトラブルシューティングと、ターゲットが 127.0.0.1/targets に確実に表示されるようにするためのこちらの手順に従ってください。

ポッド/サービス モニターのターゲットを示すスクリーンショット

次のステップ