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 小时前
运维常见面试题_07_配置管理与监控(Ansible · Prometheus · Alertmanager)· 容器编排(Kubernetes)
运维·k8s·ansible·prometheus
玉&心2 天前
在grafana中引入prometheus的数据监控
grafana·prometheus
Dovis(誓平步青云)2 天前
从Redis指标采集到异常告警:redis_exporter + Prometheus 完整实战
服务器·数据库·人工智能·redis·架构·prometheus·vibe coding
吉甫作诵4 天前
Prometheus 安装配置:Consul 自动发现与 Thanos Sidecar 持久化
prometheus·consul
溜达的大象4 天前
Prometheus怎么监控没有公网IP的服务器?Node Exporter远程采集实战
服务器·tcp/ip·prometheus
测试狗科研平台6 天前
从实验室到量产:测试狗新材料中试的技术路径与平台支撑
测试工具·服务发现·材料工程
八角Z6 天前
AI Agent作为经济参与者的兴起:机器经济与传统金融体系的并存与交织
大数据·人工智能·服务发现
Livia要学习9 天前
Prometheus架构解析
prometheus
云烟成雨TD10 天前
Micrometer 系列【63】统一观测:基于 Spring Boot 的生产级演示案例 | 基于 OTLP 集成 Prometheus + Jaeger
spring boot·云原生·prometheus
Anita-lee11 天前
泛娱乐出海 APP 运营研究:用户量下滑成因与多语种客服数据化运营解决方案
大数据·产品运营·服务发现·娱乐