kubernetes 普罗米修斯 Helm 图:未检测到带有www.example.com注解的Scrape目标prometheus.io/scrape

unftdfkk  于 2023-06-28  发布在  Kubernetes
关注(0)|答案(3)|浏览(114)

我用的是this prometheus chart。文件上说
为了让prometheus抓取pod,你必须在pod中添加注解,如下所示:

metadata:   
  annotations:
    prometheus.io/scrape: "true"
    prometheus.io/path: /metrics
    prometheus.io/port: "8080"

所以我创建了这样一个服务

apiVersion: v1
kind: Service
metadata:
  name: nodejs-client-service
  labels:
    app: nodejs-client-app
  annotations:
    prometheus.io/scrape: "true"
    prometheus.io/path: /metrics
    prometheus.io/port: "5000"
spec:
  type: LoadBalancer
  selector:
    app: nodejs-client-app
  ports:
    - protocol: TCP
      name: http
      port: 80
      targetPort: 5000

但我的服务不会出现在普罗米修斯的目标上。我错过了什么?

vktxenjb

vktxenjb1#

我在stable/prometheus-operator图表中遇到了同样的问题。我尝试将上面的注解添加到pod和服务中,但都不起作用。
对我来说,解决方案是添加一个ServiceMonitor对象。添加后,Prometheus动态发现了我的服务:
Fig 1: target list
Fig 2: dynamically added scrape_config

解决方案示例

这个命令解决了这个问题:kubectl apply -f service-monitor.yml

# service-monitor.yml
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  labels:
    release: prom
  name: eztype
  namespace: default
spec:
  endpoints:
    - path: /actuator/prometheus
      port: management
  namespaceSelector:
    matchNames:
      - default
  selector:
    matchLabels:
      app.kubernetes.io/name: eztype

在这里,我的pod和服务被标注为名称eztype,并在指定路径下的端口8282上公开度量。为了完整起见,这里是我的服务定义的相关部分:

# service definition (partial)
spec:
  clusterIP: 10.128.156.246
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: http
  - name: management
    port: 8282
    protocol: TCP
    targetPort: 8282

值得注意的是,ServiceMonitor对象用于Prometheus图表本身:

$ kubectl get servicemonitors -n monitor
NAME                                               AGE
prom-prometheus-operator-alertmanager              14d
prom-prometheus-operator-apiserver                 14d
prom-prometheus-operator-coredns                   14d
prom-prometheus-operator-grafana                   14d
prom-prometheus-operator-kube-controller-manager   14d
prom-prometheus-operator-kube-etcd                 14d
prom-prometheus-operator-kube-proxy                14d
prom-prometheus-operator-kube-scheduler            14d
prom-prometheus-operator-kube-state-metrics        14d
prom-prometheus-operator-kubelet                   14d
prom-prometheus-operator-node-exporter             14d
prom-prometheus-operator-operator                  14d
prom-prometheus-operator-prometheus                14d
cngwdvgl

cngwdvgl2#

你必须在pod中添加注解。非服务

moiiocjp

moiiocjp3#

要使用pod的注解,您需要将作业'kubernetes-pods'添加到prometheus scrape config https://github.com/prometheus/prometheus/blob/main/documentation/examples/prometheus-kubernetes.yml#L257
示例:

- job_name: 'kubernetes-pods'
  honor_labels: true

  kubernetes_sd_configs:
    - role: pod

  relabel_configs:
    - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
      action: keep
      regex: true
    - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape_slow]
      action: drop
      regex: true
    - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scheme]
      action: replace
      regex: (https?)
      target_label: __scheme__
    - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
      action: replace
      target_label: __metrics_path__
      regex: (.+)
    - source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
      action: replace
      regex: (.+?)(?::\d+)?;(\d+)
      replacement: $1:$2
      target_label: __address__
    - action: labelmap
      regex: __meta_kubernetes_pod_annotation_prometheus_io_param_(.+)
      replacement: __param_$1
    - action: labelmap
      regex: __meta_kubernetes_pod_label_(.+)
    - source_labels: [__meta_kubernetes_namespace]
      action: replace
      target_label: namespace
    - source_labels: [__meta_kubernetes_pod_name]
      action: replace
      target_label: pod
    - source_labels: [__meta_kubernetes_pod_phase]
      regex: Pending|Succeeded|Failed|Completed
      action: drop

相关问题