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

相关推荐
川石课堂软件测试15 小时前
软件测试:典型面试题库
数据库·python·功能测试·mysql·单元测试·grafana·prometheus
何中应2 天前
Promehteus如何指定数据路径
运维·prometheus·监控
Elastic 中国社区官方博客2 天前
使用 Remote Write 将 Prometheus 指标发送到 Elasticsearch
大数据·运维·elasticsearch·搜索引擎·全文检索·prometheus
要做一个小太阳2 天前
blockbox配置文件详解与优化
运维·网络·prometheus
Elastic 中国社区官方博客2 天前
Prometheus Remote Write 在 Elasticsearch 中的摄取原理
大数据·数据库·elasticsearch·搜索引擎·信息可视化·全文检索·prometheus
rchmin3 天前
Nacos 3.x 优势介绍及接入指南
微服务·服务发现·动态配置
曲幽4 天前
FastAPI服务半夜又挂了?先别急着重启,查查你的数据库连接池“池子”是不是漏了
python·prometheus·fastapi·web·async·sqlalchemy·connection·pool
Dontla6 天前
高基数(High Cardinality)问题介绍(Prometheus、高基数字段、低基数字段)
前端·数据库·prometheus
hqyjzsb6 天前
传统剪辑师升级AI视频生成师后接单效率与收入变化
人工智能·aigc·服务发现·音视频·学习方法·业界资讯·ai写作