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

相关推荐
川石课堂软件测试1 天前
软件测试|常见面试题整理
数据库·python·jmeter·mysql·appium·postman·prometheus
蜀道山老天师1 天前
云原生监控入门:监控基础概念 + SLI/SLO/SLA 详解 + Prometheus 从零安装配置
linux·运维·云原生·prometheus
专业白嫖怪1 天前
监控平台Prometheus+Grafana的部署
运维·grafana·prometheus
Elastic 中国社区官方博客1 天前
从平均值到任意百分位数:Elasticsearch 在 ES|QL 中原生支持指数直方图
大数据·数据库·sql·elasticsearch·搜索引擎·全文检索·prometheus
Elastic 中国社区官方博客2 天前
在 Elasticsearch 中使用原生 PromQL 支持查询 Prometheus 指标
大数据·elasticsearch·搜索引擎·信息可视化·全文检索·prometheus
Elastic 中国社区官方博客2 天前
Elastic 9.4:Workflows 正式发布、Agent Builder 更新,以及 Prometheus / PromQL 支持
运维·数据库·人工智能·elasticsearch·搜索引擎·信息可视化·prometheus
虎头金猫3 天前
监控机不在被监控的机器上,Prometheus怎么跨网络把数据拉过来
运维·服务器·开发语言·网络·云原生·开源·prometheus
四方云6 天前
Kamailio + Prometheus 集成方案(含完整可执行配置)
prometheus
雨辰AI7 天前
SpringBoot3 + 人大金仓 V9 微服务监控实战|Prometheus+Grafana+SkyWalking 全链路监控
数据库·后端·微服务·grafana·prometheus·skywalking
rKWP8gKv78 天前
Java微服务性能监控:Prometheus与Grafana集成方案
java·微服务·prometheus