prometheus基于文件的服务发现

之间讲到,prometheus监控的对象就来自于他的配置文件里面的targets,如果要新增被监控对象,就继续往targets里面加。

但这个缺点是,每次修改完后都得重启prometheus。有没有什么办法,能在不重启的情况下增加target呢?有,那就是prometheus的服务自动发现

今天咱们讲一个最常用的方式,基于文件的服务发现(File-Based-Service-Discovery)

1 将默认配置文件做如下修改:

yaml 复制代码
# ...
scrape_configs:
  - job_name: "prometheus"

# 注释掉之前静态配置的内容
#    static_configs:
#      - targets: ["localhost:9090"]

# 修改服务发现为 File-Based-Service-Discovery:
    file_sd_configs:
    - files:
      - '/prometheus/targets/*.yml'  # 读取此目录下的所有 .yml 和 .json 文件
      - '/prometheus/targets/*.json'
      refresh_interval: 10s  # 每10秒钟读取一次,支持 s/m/h

2 启动promethus容器

bash 复制代码
docker run -d -p 9090:9090 \
-v $(pwd)/prometheus.yml:/opt/bitnami/prometheus/conf/prometheus.yml \ # 使用上面修改的配置文件,替换容器内的配置文件
-v /prometheus/targets:/prometheus/targets \
--name prometheus --rm bitnami/prometheus

3 在上面指定的目录下增加以下配置文件:

node_exporter1.yml:

yaml 复制代码
- targets: ['ip1:9200'] # 一组targets内可写多个target

- targets: ['ip2:9200'] # 另一组targets
  labels: # 为这一组targets内的所有target增加label
    environment: 'production' # labels也可写多个

node_exporter2.json:

json 复制代码
[
  {
    "targets": ["ip3:9200"]
  },
  {
    "targets": ["ip4:9200"],
    "labels": {"environment": "develop"}
  }
]

新增target时,可以增加文件,也可以直接修改文件内容,prometheus都能识别到

进入prometheus网页,点击Status>Targets

相关推荐
云游2 天前
大模型性能指标的监控系统(prometheus3.5.0)和可视化工具(grafana12.1.0)基础篇
grafana·prometheus·可视化·监控
qq_232045573 天前
非容器方式安装Prometheus和Grafana,以及nginx配置访问Grafana
nginx·grafana·prometheus
夜莺云原生监控3 天前
Prometheus 监控 Kubernetes Cluster 最新极简教程
容器·kubernetes·prometheus
SRETalk4 天前
Prometheus 监控 Kubernetes Cluster 最新极简教程
kubernetes·prometheus
川石课堂软件测试4 天前
JMeter并发测试与多进程测试
功能测试·jmeter·docker·容器·kubernetes·单元测试·prometheus
SRETalk5 天前
夜莺监控的几种架构模式详解
prometheus·victoriametrics·nightingale·夜莺监控
Ditglu.6 天前
使用Prometheus + Grafana + node_exporter实现Linux服务器性能监控
服务器·grafana·prometheus
SRETalk6 天前
监控系统如何选型:Zabbix vs Prometheus
zabbix·prometheus
睡觉z6 天前
云原生环境Prometheus企业级监控
云原生·prometheus
归梧谣6 天前
云原生环境 Prometheus 企业级监控实战
云原生·prometheus