Prometheus服务的动态发现
一、概述
想要发现被监控节点的机器,需要在Prometheus的配置文件里面添加被监控的机器的ip或者url,每次新增都需要配置,都需要重载,新增、减少都需要重新加载,非常麻烦,不适合大规模的集群环境。
![34-[信创]-Prometheus自动发现-基于文档自动发现_加载](https://s2.51cto.com/images/blog/front/202507/75719b907ba9c12c0fa228a9b6ea3770e930b7.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
Prometheus提供了这种问题的解决方案,方案有两种:
基于文档的自动发现和基于网络的自动发现
二、基于文档的自动发现
在Prometheus机器上加一个文档自动发现,新增的机器追加到文档里面,就不用去配置了
修改Prometheus配置文件
[root@prometheus ~]# cat /data/prometheus/soft/prometheus/prometheus.yml
#通用设置
global:
#抓取监控的间隔时间,多长时间获取一次数据(生产环境,建议15-30s):
scrape_interval: 3s
#多久读一次规则
evaluation_interval: 15s
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
#被监控的配置
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
# #被监控的主体
# - job_name: "node-exporter"
# static_configs:
# #被监控的数据抓取地址
# - targets: ["10.1.19.57:9100"]
- job_name: "node-exporter"
#基于文档自动发现
file_sd_configs:
- files: #支持两种文件格式 yaml和json
#- /data/prometheus/soft/prometheus/file-sd.json
- /data/prometheus/soft/prometheus/file-sd.yaml重新加载
curl -X POST http://10.1.19.74:9090/-/reload
#不生效就 systemctl restart prometheus编辑发现文件
yaml文件写法
[root@prometheus ~]# vim /data/prometheus/soft/prometheus/file-sd.yaml
- targets:
- '10.1.19.57:9100'
- '10.1.19.58:9100'
- '10.1.19.59:9100'
- '10.1.19.61:9100'
- '10.1.19.53:9100'
- '10.1.19.65:9100'
labels: #标签可写可不写
linux: prometheus-learn
office: www.guoguo.comjson文件写法
[root@prometheus ~]# vim /prometheus/soft/prometheus/file-sd.yaml
[
{
"targets":["10.1.19.57:9100","10.1.19.58:9100","10.1.19.59:9100"],
"lables":{
"linux":"prometheus-learn","office":"www.guoguo.com"
}
}
]![34-[信创]-Prometheus自动发现-基于文档自动发现_加载_02](https://s2.51cto.com/images/blog/front/202507/74ae39422ac32bd5954980928302d49d3d7a37.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
有了文档的自动发现,就不需要修改Prometheus的配置文件了,更不需要重新加载Prometheus的服务了
















