【问题标题】:Use Servicemonitor to check services via external hostname使用 Servicemonitor 通过外部主机名检查服务
【发布时间】:2021-12-08 16:47:59
【问题描述】:

我们有一个外部 DNS 设置,可以根据服务注释创建和绑定 dns 条目。

例如,我们有这样的警报管理器服务:

apiVersion: v1
kind: Service
metadata:
  name: prometheus-kube-prometheus-alertmanager
  namespace: prometheus
  labels:
...
    heritage: Helm
    prometheus-monitor-https: 'true'
    release: prometheus
    self-monitor: 'true'
  annotations:
    external-dns.alpha.kubernetes.io/hostname: alertmanager.ourdomain.com
    external-dns.alpha.kubernetes.io/ttl: '60'
spec:
  ports:
    - name: web
      protocol: TCP
      port: 80
      targetPort: 9093
      nodePort: 31126
  selector:
    alertmanager: prometheus-kube-prometheus-alertmanager
    app.kubernetes.io/name: alertmanager
  type: LoadBalancer
  sessionAffinity: None
  externalTrafficPolicy: Cluster

(缩写)

我想使用黑盒导出器和注释中的数据,所以我们不必在这里手动添加监控,而是依靠 kubernetes 提供要监控的信息。

为此,我编写了一个 servicemonitor,但它与服务不匹配并调用 blackbox 导出器。

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: blackbox-exporter-monitor-https-external
  namespace: prometheus
spec:
  namespaceSelector:
    any: true
  selector:
    matchLabels:
      prometheus-monitor-https: any
  targetLabels:
    - environment
    - instance
  endpoints:
    - metricRelabelings:
        - sourceLabels: [__meta_kubernetes_service_annotation_external_dns_alpha_kubernetes_io_hostname]
          targetLabel: __param_target
          replacement: "https://$1"
        - sourceLabels: [__param_target]
          targetLabel: instance
        - targetLabel: __param_scheme
          replacement: https
        - targetLabel: __address__
          replacement: prometheus-blackbox-exporter:9115
      path: /probe
      params:
        debug:
          - "true"
        module:
          - "http_2xx"

我不明白为什么它不应该与服务匹配。你有什么提示吗?

【问题讨论】:

    标签: kubernetes prometheus-operator prometheus-blackbox-exporter


    【解决方案1】:

    服务的标签为prometheus-monitor-https: 'true',而ServiceMonitor 的标签为selector.matchLabelsprometheus-monitor-https: any

    如果您更改此设置,使 ServiceMonitor 的 selector.matchLabels 等于 prometheus-monitor-https: 'true',那么我认为它应该可以工作。 matchLabels 查找标签键值对的预期匹配项。

    我还看到你写的namespaceSelectorany: true。很高兴知道 namespaceSelector 以不同的方式工作。它需要命名空间的标签,它应该在其中找到资源。在您的情况下,它将查找具有标签any: true 的命名空间。但我觉得你其实是想选择所有的命名空间,等于根本不指定namespaceSelector。

    【讨论】:

    • 天哪!命名空间选择器是问题所在。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多