如何为prometheus-operator创建ServiceMonitor?

and*_*dig 17 coreos kubernetes kubernetes-helm

最近,prometheus-operator已提升为稳定舵图(https://github.com/helm/charts/tree/master/stable/prometheus-operator)。

我想了解如何通过k8s集群中的prometheus-operator将自定义应用程序添加到监视中。例如,默认情况下会在9252上提供指标的gitlabRunner示例将不胜感激(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
Run Code Online (Sandbox Code Playgroud)

这是prometheus配置:

> kubectl get prometheus -o yaml

...
serviceMonitorNamespaceSelector: {}
serviceMonitorSelector:
  matchLabels:
    release: prometheus
...
Run Code Online (Sandbox Code Playgroud)

因此,选择器应该匹配。“不工作”是指端点不会出现在Prometheus UI中。

Fra*_*oso 38

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

在此输入图像描述

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

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


and*_*dig 15

感谢Peter向我展示了这个想法,从原理上讲,这并不是完全不正确的,我发现了缺少的链接。作为servicemonitor监视服务的对象(哈哈),我错过了创建服务的部分,该服务不属于gitlab掌舵图。最后,这个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
Run Code Online (Sandbox Code Playgroud)

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

  • 从 kube-prometheus stack helm 安装操作符时,使用 `release: kube-prometheus-stack` 标签而不是 `release: prometheus` (4认同)

vrs*_*vrs 7

我知道这个问题已经回答。但是当Prometheus部署在具有Helm稳定/ prometheus-operator图表的Kubernetes中时,我找不到类似的问题ServiceMonitor。原来,我的服务暴露了一个我没有明确命名的端口:

  - protocol: TCP
    port: 8080
    targetPort: uwsgi
Run Code Online (Sandbox Code Playgroud)

我可以通过定位uwsgi端口在Ingress中使用它。但是似乎它ServiceMonitor需要一个显式命名的端口,Service即使它具有与其自己的tagetPort相同的名称:

  - name: uwsgi
    protocol: TCP
    port: 8080
    targetPort: uwsgi
Run Code Online (Sandbox Code Playgroud)

我写了一个博客帖子这个问题在这里

  • 感谢您的博客文章,非常有用 (2认同)