简体   繁体   English

如何使用 Prometheus Operator 抓取 Jenkins 指标

[英]How to scrape Jenkins metrics using Prometheus Operator

I'm using Kube-prometheus with Prometheus-Operator to monitor my K8s cluster.我正在使用 Kube-prometheus 和 Prometheus-Operator 来监控我的 K8s 集群。 I've deployed Jenkins on my cluster and want to start to get metrics here using ServiceMonitor.我已经在我的集群上部署了 Jenkins,并想开始使用 ServiceMonitor 在这里获取指标。

I've installed the Prometheus plugin which exposes the metrics using /prometheus or by /metrics/API_KEY/metrics , this works fine if I create a new static job.我已经安装了 Prometheus 插件,它使用/prometheus/metrics/API_KEY/metrics公开/metrics/API_KEY/metrics ,如果我创建一个新的静态作业,这可以正常工作。 However, if I want to use ServiceMonitor, it does not work.但是,如果我想使用 ServiceMonitor,它不起作用。

apiVersion: monitoring.coreos.com/v1                                                                                                                                                                        
kind: ServiceMonitor
metadata:
  labels:
    k8s-app: jenkins
  name: jenkins
  namespace: monitoring
spec:
  endpoints:
  - interval: 30s 
    port: http
    path: /metrics/y1H6G16T-DhqpHdW9XwHWnP9FWAXMMfy4XnXVnyoIOEV3-gPJZKN284OFUcVkPxL/metrics
  selector:
    matchLabels:
      jenkins: main

I don't know about ServiceMonitor, but I monitor my Jenkins instance without any problem, using annotations on Jenkins' service :我不知道 ServiceMonitor,但我使用 Jenkins 服务上的注释监控我的 Jenkins 实例没有任何问题:

prometheus.io/scrape: "true"
prometheus.io/port: "8080"
prometheus.io/path: "/prometheus"

I'm using kube-prometheus-stack v12.8.0 (formerly known as the prometheus-operator helm chart).我正在使用 kube-prometheus-stack v12.8.0(以前称为 prometheus-operator helm chart)。

To make prometheus-operator detect external serviceMonitors (like the one provided by Jenkins), you'll have to configure two things:要使 prometheus-operator 检测外部 serviceMonitors(如 Jenkins 提供的那个),您必须配置两件事:

  1. configure it to scan other namespaces:将其配置为扫描其他命名空间:

    serviceMonitorNamespaceSelector matchLabels: prometheus: please-scan-this-namespace-too serviceMonitorNamespaceSelector matchLabels: prometheus: please-scan-this-namespace-too

note: alternatively, you can leave it to {} so that all namespaces are scanned注意:或者,您可以将其保留为 {},以便扫描所有命名空间

  1. configure it to also select the serviceMonitors detected in these other namespaces:将其配置为还选择在这些其他命名空间中检测到的 serviceMonitors:

    serviceMonitorSelector: matchLabels: release: prometheus-operator serviceMonitorSelector: matchLabels: release: prometheus-operator

note: even though the documentation states that if you leave serviceMonitorSelector to {}, it will select all serviceMonitors, it does not seem to work.注意:尽管文档指出如果您将 serviceMonitorSelector 保留为 {},它将选择所有 serviceMonitor,但它似乎不起作用。

And finally, you'd still need to add these labels to 1) the namespaces and 2) serviceMonitors that you want prometheus to adopt.最后,您仍然需要将这些标签添加到您希望普罗米修斯采用的 1) 命名空间和 2) serviceMonitor。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM