[英]How do you add scrape targets to a Prometheus server that was installed with Kubernetes-Helm?
Background背景
I have installed Prometheus on my Kubernetes cluster (hosted on Google Container Engineer) using the Helm chart for Prometheus .我已经使用Prometheus的Helm 图表在我的 Kubernetes 集群(托管在 Google Container Engineer 上) 上安装了 Prometheus 。
The Problem问题
I cannot figure out how to add scrape targets to the Prometheus server.我不知道如何将抓取目标添加到 Prometheus 服务器。 The prometheus.io site describes how I can mount a prometheus.yml file (which contains a list of scrape targets) to a Prometheus Docker container -- I have done this locally and it works.
prometheus.io 站点描述了我如何将 prometheus.yml 文件(其中包含抓取目标列表)挂载到 Prometheus Docker 容器——我已经在本地完成了这个并且它有效。 However, I don't know how to specify scrape targets for a Prometheus setup installed via Kubernetes-Helm.
但是,我不知道如何为通过 Kubernetes-Helm 安装的 Prometheus 设置指定抓取目标。 Do I need to add a volume to the Prometheus server pod that contains the scrape targets, and therefore update the YAML files generated by Helm??
我是否需要向包含抓取目标的 Prometheus 服务器 pod 添加一个卷,从而更新 Helm 生成的 YAML 文件?
I am also not clear on how to expose metrics in a Kubernetes Pod -- do I need to forward a particular port?我也不清楚如何在 Kubernetes Pod 中公开指标——我是否需要转发特定端口?
You need to add annotations to the service you want to monitor. 您需要为要监视的服务添加注释。
apiVersion: v1
kind: Service
metadata:
annotations:
prometheus.io/scrape: 'true'
From the prometheus.yml in the chart: 来自图表中的prometheus.yml:
prometheus.io/scrape
: Only scrape services that have a value of true
prometheus.io/scrape
:只抓取值为true
prometheus.io/scheme
: http or https prometheus.io/scheme
或https prometheus.io/path
: override if the metrics path is not /metrics
prometheus.io/path
:如果指标路径不是/metrics
则覆盖 prometheus.io/port
: If the metrics are exposed on a different port prometheus.io/port
:如果度量标准在不同的端口上公开 And yes you need to expose the port with metrics to the service so Prometheus could access it 是的,您需要向服务公开端口,以便Prometheus可以访问它
First of all, you need to create a Service Monitor, which is a custom K8s resource.首先需要创建一个Service Monitor,它是一个自定义的K8s资源。 Just create a
servicemonitor.yaml
in the manifests folder.只需在清单文件夹中创建一个
servicemonitor.yaml
。
Since when we are deploying on K8s, we don't have access to the Prometheus.yaml file to mention the targets, we create the servicemonitor, which in-turn adds the target to the scrap_config in the Prometheus.yaml file.由于在 K8s 上部署时,我们无法访问 Prometheus.yaml 文件来提及目标,因此我们创建了 servicemonitor,它将目标添加到 Prometheus.yaml 文件中的 scrap_config 中。 You can read about it more fromhere .
您可以从这里阅读更多相关信息。
This is a sample servicemonitor.yaml
file for exposing Flask App metrics in Prometheus.这是一个示例
servicemonitor.yaml
文件,用于在 Prometheus 中公开 Flask App 指标。
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: flask-metrics
namespace: prometheus # namespace where prometheus is running
labels:
app: flask-app
release: prom # name of the release
# ( VERY IMPORTANT: You need to know the correct release name by viewing
# the servicemonitor of Prometheus itself: Without the correct name,
# Prometheus cannot identify the metrics of the Flask app as the target.)
spec:
selector:
matchLabels:
# Target app service
app: flask-app # same as above
release: prom # same as above
endpoints:
- interval: 15s # scrape interval
path: /metrics # path to scrape
port: http # named port in target app
namespaceSelector:
matchNames:
- flask # namespace where the app is running
Also add this Release Label to the Services and Deployments file too, in the metadata and spec section.还要将此版本标签添加到服务和部署文件的元数据和规范部分。
If you encounter a situation where Prometheus is showing the Target but not the endpoints, take a look at this: https://github.com/prometheus-operator/prometheus-operator/issues/3053如果您遇到 Prometheus 显示目标而不显示端点的情况,请查看: https : //github.com/prometheus-operator/prometheus-operator/issues/3053
Some useful links:一些有用的链接:
I have concluded my 12-hour research in this answer.我已经在这个答案中结束了我 12 小时的研究。 Please Upvote the answer if you find it useful.
如果您觉得有用,请为答案点赞。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.