0

我们有一个带有外部 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"

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

4

1 回答 1

1

服务有 label prometheus-monitor-https: 'true',而 ServiceMonitor 有selector.matchLabelsof prometheus-monitor-https: any

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

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

于 2021-12-08T20:47:21.447 回答