kubernetes 如何为prometheus-operator创建一个ServiceMonitor?

9ceoxa92  于 12个月前  发布在  Kubernetes
关注(0)|答案(4)|浏览(103)

最近,prometheus-算子被提升为稳定舵图(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中。

xytpbqjk

xytpbqjk1#

感谢彼得,他向我展示了原则上的想法并不完全错误,我找到了缺失的一环。由于servicemonitor确实监控服务(哈哈),我错过了创建服务的部分,这不是gitlab Helm Chart的一部分。最后,这个yaml为我做了这个把戏,指标出现在普罗米修斯中:

# 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

很高兴知道:metricstargetPort在gitlab运行图中定义。

14ifxucb

14ifxucb2#

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

如果任何一个匹配不正确,目标就不会出现。
阅读更多信息:https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/troubleshooting.md#troubleshooting-servicemonitor-changes

ou6hu8tu

ou6hu8tu3#

我知道这个问题已经有答案了。但是当Prometheus部署在Kubernetes中时,我遇到了类似的问题,Helm的stable/prometheus-operator chart无法为我的ServiceMonitor找到任何活动目标。原来我的服务暴露了一个我没有显式命名的端口:

- protocol: TCP
    port: 8080
    targetPort: uwsgi

我可以在Ingress中使用它,目标是uwsgi端口。但似乎ServiceMonitor需要在Service中显式命名的端口,即使它与自己的tagetPort同名:

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

我写了一篇关于这个问题的博客here

pwuypxnk

pwuypxnk4#

上述解决方案到目前为止运行良好。
**发布标签很重要。**没有此标签,Prom无法将应用指标添加到其目标列表。

通过检查Prometheus本身的ServiceMonitor**,确保添加了**正确的版本标签。同时确保在元数据和规范部分中也将发布标签添加到服务和部署文件中。
如果您遇到Prometheus显示目标但不显示端点的情况,请查看以下内容:https://github.com/prometheus-operator/prometheus-operator/issues/3053

相关问题