【问题标题】:How to create a ServiceMonitor for prometheus-operator?如何为 prometheus-operator 创建 ServiceMonitor?
【发布时间】:2019-03-30 04:05:02
【问题描述】:

最近,prometheus-operator 被提升为 stable helm chart (https://github.com/helm/charts/tree/master/stable/prometheus-operator)。

我想了解如何在 k8s 集群中添加自定义应用程序以通过 prometheus-operator 进行监控。举个例子,比如 gitlab runner,它默认提供 9252 的指标,将不胜感激 (https://docs.gitlab.com/runner/monitoring/#configuration-of-the-metrics-http-server)。

我有一个基本的 yaml,它显然不起作用,但也没有提供任何关于 什么 不起作用的反馈:

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: gitlab-monitor
  # Change this to the namespace the Prometheus instance is running in
  namespace: default
  labels:
    app: gitlab-runner-gitlab-runner
    release: prometheus
spec:
  selector:
    matchLabels:
      app: gitlab-runner-gitlab-runner
  namespaceSelector:
    # matchNames:
    # - default
    any: true
  endpoints:
  - port: http-metrics
    interval: 15s

这是prometheus的配置:

> kubectl get prometheus -o yaml

...
serviceMonitorNamespaceSelector: {}
serviceMonitorSelector:
  matchLabels:
    release: prometheus
...

所以选择器应该匹配。 “不工作”是指端点没有出现在 prometheus UI 中。

【问题讨论】:

  • 能否请您发布kubectl get prometheus -o yaml 的输出。您必须在此处指定 serviceMonitorNamespaceSelector: {}serviceMonitorSelector: {}(示例将选择所有命名空间中的所有 serviceMonitors)。您的 ServiceMonitor 定义对我来说很好。
  • 改进了 yaml 并在上面添加了 prometheus 配置。
  • 能否请您定义 serviceMonitorSelector: {} 以排除此选择器无法正常工作。您还可以检查服务的注释。我有一个selector matchLabels: <key>: <value> ,它确实有效。我不确定namespaceSelector: # matchNames: # - default any: true 是否也有效。
  • 基于 prometheus 运算符附带的监视器,我什至可以完全删除 namespaceSelector(尽管我已经尝试了这两种方法)。没有出现在普罗米修斯目标中。是否有任何日志文件可以检查服务监视器在何处被拾取?
  • 头撞墙。看着github.com/coreos/prometheus-operator/blob/…我也需要服务,而不仅仅是服务监视器?

标签: kubernetes coreos kubernetes-helm


【解决方案1】:

感谢彼得向我展示了原则上的想法并不完全不正确,我找到了缺失的链接。作为servicemonitor 监控服务(哈哈),我错过了创建不属于 gitlab helm chart 的服务的部分。最后,这个 yaml 为我解决了问题,指标出现在 Prometheus 中:

# Service targeting gitlab instances
apiVersion: v1
kind: Service
metadata:
  name: gitlab-metrics
  labels:
    app: gitlab-runner-gitlab-runner
spec:
  ports:
  - name: metrics # expose metrics port
    port: 9252 # defined in gitlab chart
    targetPort: metrics
    protocol: TCP
  selector:
    app: gitlab-runner-gitlab-runner # target gitlab pods
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: gitlab-metrics-servicemonitor
  # Change this to the namespace the Prometheus instance is running in
  # namespace: default
  labels:
    app: gitlab-runner-gitlab-runner
    release: prometheus
spec:
  selector:
    matchLabels:
      app: gitlab-runner-gitlab-runner # target gitlab service
  endpoints:
  - port: metrics
    interval: 15s

很高兴知道:metrics targetPort 在 gitlab runner 图表中定义。

【讨论】:

  • 从 kube-prometheus stack helm 安装 operator 时使用 release: kube-prometheus-stack 标签而不是 release: prometheus
【解决方案2】:

我知道这个问题已经得到解答。但是当使用 Helm 的 stable/prometheus-operator 图表部署在 Kubernetes 中的 Prometheus 找不到我的ServiceMonitor 的任何活动目标时,我遇到了类似的问题。 原来我的服务暴露了一个我没有明确命名的端口:

  - protocol: TCP
    port: 8080
    targetPort: uwsgi

我可以通过定位uwsgi 端口在 Ingress 中使用它。但似乎ServiceMonitor 需要在Service 中明确命名端口,即使它与自己的tgetPort 同名:

  - name: uwsgi
    protocol: TCP
    port: 8080
    targetPort: uwsgi

我已经写了一篇关于这个问题的博文here

【讨论】:

  • 感谢您的博文,非常有用
  • 经过一整天的调试,希望我能早点找到这个。很棒的发现。
  • 如果我直接使用一个端口,比如 7896 会怎样?我也觉得还可以
  • @vrs,我已经尝试过您的修复,但没有奏效。你能帮忙吗? stackoverflow.com/questions/60780448/…
  • 感谢您的博客。虽然,现在我对Deployments 上的注释prometheus.io/scrapeprometheus.io/port 的用途感到困惑?
【解决方案3】:

到目前为止,上述解决方案都运行良好。

发布标签很重要。没有这个,Prom 就无法将应用指标添加到其目标列表中。

确保通过检查 Prometheus 本身的 ServiceMonitor 来添加正确的发布标签。还要确保在元数据和规范部分中也将发布标签添加到服务和部署文件中。

如果您遇到 Prometheus 显示目标但不显示端点的情况,请查看:https://github.com/prometheus-operator/prometheus-operator/issues/3053

【讨论】:

  • 这是您从 Prometheus 查找和检查 serviceMonitor 选择器的方法:kubectl -n <your-prometheus-namespace> get prometheus,然后是 kubectl -n <your-prometheus-namespace> get prometheus <resource-name-you-just-found-out> -oyaml 。查找属性serviceMonitorSelector:
【解决方案4】:

这张图完美展示了Prometheus、ServiceMonitors和Services之间的联系

如果任何匹配项不正确,目标将不会显示。

阅读更多:https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/troubleshooting.md#troubleshooting-servicemonitor-changes

【讨论】:

    猜你喜欢
    • 2021-02-12
    • 2020-12-09
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多