我正在使用 this prometheus helm chart
我想知道是否可以设置 prometheus 操作符来自动监控集群或命名空间中的每个服务,而不必为每个服务创建 ServiceMonitor
使用当前设置,当我想监视服务时,我必须创建一个带有 ServiceMonitor 标签的 release: prometheus
编辑:
带有 monitoring: "true" 标签的服务

apiVersion: v1
kind: Service
metadata:
  name: issue-manager-service
  labels:
    app: issue-manager-app
    monitoring: "true"
spec:
  selector:
    app: issue-manager-app
  ports:
    - protocol: TCP
      name: http
      port: 80
      targetPort: 7200
“包罗万象”的服务监视器:
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: service-monitor-scraper
  labels:
    release: prometheus
spec:
  endpoints:
  - port: metrics
    interval: 30s
    path: /metrics
  jobLabel: monitoring
  namespaceSelector:
    any: true
  selector:
    matchLabels:
      monitoring: "true"

最佳答案

仅当您在所有服务上都有一个共同的标签时

# for example:
org: "my-company"
# or
monitoring: "true"
# or
app.kubernetes.io/managed-by: "Helm"  # <- in most cases this represents all
然后定义一个单一的、跨命名空间的 ServiceMonitor ,它涵盖所有标记的服务:
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: common-monitor
  namespace: monitoring
spec:
  endpoints:
  - port: metrics
    interval: 30s
    path: /metrics
  jobLabel: monitoring
  namespaceSelector:
    any: true  # <- important
  selector:
    matchLabels:
      monitoring: "true"  # <- should match what you've chosen as common
然后,为了确保 Prometheus Operator 发现了这个 ServiceMonitor,您可以:
  • 通过内置操作符模板声明这个 ServiceMonitor:https://github.com/prometheus-community/helm-charts/blob/4164ad5fdb6a977f1aba7b65f4e65582d3081528/charts/kube-prometheus-stack/values.yaml#L2008
  • 或者,声明一个指向你的 serviceMonitorSelector https://github.com/prometheus-community/helm-charts/blob/4164ad5fdb6a977f1aba7b65f4e65582d3081528/charts/kube-prometheus-stack/values.yaml#L1760
  • ServiceMonitor
    Prometheus Operator 和 ServiceMonitor 之间的这种额外显式链接是有意完成的 - 这样,如果您的集群上有 2 个 Prometheus 实例(例如 Infra 和 Product),您可以将哪个 Prometheus 将哪些 Pod 分配到其抓取配置中。
    从您的问题来看,听起来您已经有一个基于 serviceMonitorSelector 标签的 release: prometheus - 尝试将其添加到您的全能 ServiceMonitor 中。

    关于kubernetes - Prometheus:监控所有服务而不为每个服务创建ServiceMonitor?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/64445937/

    10-15 22:53