如何为prometheus-operator创建ServiceMonitor?

时间:2018-10-25 13:52:30

标签: kubernetes coreos kubernetes-helm

最近,普罗米修斯-运算符已升级为稳定的舵图(https://github.com/helm/charts/tree/master/stable/prometheus-operator)。

我想了解如何通过k8s集群中的prometheus-operator将自定义应用程序添加到监视中。最好说一个示例gitlabRunner的示例,该示例默认提供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中。

2 个答案:

答案 0 :(得分:7)

感谢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

要知道的是:metrics targetPort是在gitlab运行器图表中定义的。

答案 1 :(得分:0)

我知道这个问题已经回答。但是当普罗米修斯在Heber的稳定/普罗米修斯-运营商图表上部署到Kubernetes中时,找不到我的ServiceMonitor的任何活动目标时,我遇到了类似的问题。 事实证明,我的服务暴露了一个我没有明确命名的端口:

  - protocol: TCP
    port: 8080
    targetPort: uwsgi

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

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

我写了一篇有关此问题的博客文章here