简体   繁体   English

避免 Prometheus 调用 k8s 服务的所有实例(只有一个,应用程序范围的指标收集)

[英]Avoiding Prometheus call all instances of k8s service (only one, app-wide metrics collection)

I need to expose application-wide metrics for Prometheus collection from a Kubernetes application that is deployed with multiple instances, eg scaled by Horizontal Pod Autoscaler.我需要从部署了多个实例的 Kubernetes 应用程序中公开 Prometheus 集合的应用程序范围的指标,例如由 Horizo​​ntal Pod Autoscaler 缩放。 The scrape point is exposed by every instance of the pod for fail-over purposes, however I do not want Prometheus to actually call the scrape endpoint on every pod's instance, only one instance at a time and failover to another instance only if necessary.抓取点由 pod 的每个实例公开以用于故障转移目的,但是我不希望 Prometheus 实际调用每个 pod 实例上的抓取端点,一次只调用一个实例,仅在必要时才故障转移到另一个实例。

The statistics is application-wide, not per-pod instance, all instance endpoints report the same data, and calling them in parallel would serve no useful purpose and only increase a workload on the backend system that has to be queried for statistics.统计信息是应用程序范围的,而不是每个 Pod 实例,所有实例端点报告相同的数据,并行调用它们没有任何用处,只会增加后端系统上必须查询统计信息的工作负载。 I do not want 30 calls to the backend (assuming the app is scaled up to 30 pods) where just one call would suffice.我不希望对后端进行 30 次调用(假设应用程序可扩展到 30 个 pod),其中一次调用就足够了。

I hoped that exposing the scrape endpoint as a k8s service (and annotating the service for scraping) should do the trick.我希望将抓取端点公开为 k8s 服务(并注释该服务以进行抓取)应该可以解决问题。 However instead of going through the service proxy and let it route the request to one of the pods, Prometheus seems to be going directly to the instances behind the service, and to all of them, rather than only one at a time.然而,与通过服务代理并让它将请求路由到其中一个 pod 不同,Prometheus 似乎直接进入服务背后的实例,并且是所有这些实例,而不是一次只有一个。

在此处输入图片说明

Is there a way to avoid Prometheus calling all the instances, and have it call only one?有没有办法避免 Prometheus 调用所有实例,而让它只调用一个?

The service is defined as:服务定义为:

apiVersion: v1
kind: Service
metadata:
  name: k8worker-msvc
  labels:
    app: k8worker-msvc
  annotations:
    prometheus.io/scrape: 'true'
    prometheus.io/path: '/metrics'
    prometheus.io/port: '3110'
spec:
  selector:
    app: k8worker
  type: LoadBalancer
  ports:
    - protocol: TCP
      port: 3110
      targetPort: 3110

In case this is not possible, what are my options other than running leader election inside the app and reporting empty metrics data from non-leader instances?如果这是不可能的,除了在应用程序内运行领导者选举和报告来自非领导者实例的空指标数据之外,我还有什么选择?

Thanks for advice.谢谢你的建议。

This implies the metrics are coming from some kind of backend database rather than a usual in-process exporter.这意味着指标来自某种后端数据库,而不是通常的进程内导出器。 Move the metrics endpoint to a new service connected to the same DB and only run one copy of it.将指标端点移动到连接到同一数据库的新服务,并且只运行它的一个副本。

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

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