简体   繁体   中英

Servicemonitor with multiple targets

I have kube-prometheus-stack running on a kubernetes cluster along with prometheus-blackbox-exporter. I want to monitor multiple http targets. I have tried setting this up with an servicemonitor but when I add a target the target does not get the right labels

The servicemonitor yaml:

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  annotations:
    meta.helm.sh/release-name: blackbox
    meta.helm.sh/release-namespace: default
  generation: 1
  labels:
    app.kubernetes.io/instance: blackbox
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/name: prometheus-blackbox-exporter
    app.kubernetes.io/version: 0.20.0
    helm.sh/chart: prometheus-blackbox-exporter-5.7.0
    release: kube-prometheus
  name: blackbox-prometheus-blackbox-exporter
  namespace: default
spec:
  endpoints:
    - interval: 10s
      path: /probe
      port: http
      scheme: http
      params:
        module: 
        - http_2xx
        target:
          - https://google.com
          - https://example.com
      metricRelabelings:
      - sourceLabels: [__address__]
        targetLabel: __param_target
  jobLabel: kube-prometheus
  selector:
    matchLabels:
      app.kubernetes.io/instance: blackbox
      app.kubernetes.io/name: prometheus-blackbox-exporter 

But in grafana I only get one instance label right: grafana instances

spec.endpoints is a list . In yaml each lists element are defined with a dash . So you have to modify your service monitor yaml as:

spec:
  endpoints:
  - interval: 10s
    path: /probe
    port: metrics
    params:
      module:
      - http_2xx
      target:
      - https://www.google.com
    relabelings:
      - sourceLabels: [__param_target]
        targetLabel: target

  - interval: 10s
    path: /probe
    port: metrics
    params:
      module:
      - http_2xx
      target:
      - https://www.example.com
    relabelings:
      - sourceLabels: [__param_target]
        targetLabel: target

It works like it is shown here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM