当前位置: 首页 > news >正文

@Prometheus动态配置管理-ConsulConfd

文章目录

      • 动态配置管理 Consul + Confd
        • **一、目标**
        • **二、架构组件**
        • **三、环境准备**
        • **四、配置 Consul**
          • 1. 注册监控目标(服务发现)
          • 2. 存储告警规则(KV 存储)
        • **五、配置 Confd**
          • 1. 监控目标模板配置
          • 2. 告警规则模板配置
        • **六、配置 Prometheus**
        • **七、启动 Confd**
        • **八、验证流程**
          • 1. **测试服务发现**
          • 2. **测试告警规则更新**
        • **九、故障排查**
        • **十、维护说明**

动态配置管理 Consul + Confd


一、目标

实现 Prometheus 监控目标和告警规则的动态管理:

  1. 服务自动注册到 Consul 时,Prometheus 自动发现并监控
  2. 修改 Consul 中的告警规则时,Prometheus 自动加载新规则
  3. 避免手动修改配置文件和重启 Prometheus

二、架构组件
组件作用
Consul服务注册与配置存储中心,存储监控目标元数据和告警规则
Confd监听 Consul 变化,动态生成 Prometheus 配置文件
Prometheus监控系统,通过 Confd 生成的配置文件加载监控目标和告警规则

三、环境准备
  1. 安装 Consul(以 Linux 为例):

    wget https://releases.hashicorp.com/consul/1.15.3/consul_1.15.3_linux_amd64.zip
    unzip consul_1.15.3_linux_amd64.zip
    sudo mv consul /usr/local/bin/
    consul agent -dev -client=0.0.0.0 &  # 启动开发模式
    
  2. 安装 Confd

    wget https://github.com/kelseyhightower/confd/releases/download/v0.16.0/confd-0.16.0-linux-amd64
    mv confd-0.16.0-linux-amd64 /usr/local/bin/confd
    chmod +x /usr/local/bin/confd
    mkdir -p /etc/confd/{conf.d,templates}
    
  3. 安装 Prometheus(略,假设已安装)


四、配置 Consul
1. 注册监控目标(服务发现)
# 注册 Node Exporter 示例
curl -X PUT http://localhost:8500/v1/agent/service/register -d '{"ID": "node-exporter-1","Name": "node-exporter","Tags": ["prometheus", "node"],"Address": "192.168.1.100","Port": 9100,"Meta": {"job": "node"}
}'
2. 存储告警规则(KV 存储)
# 添加 CPU 告警规则
consul kv put prometheus/rules/cpu_high 'groups:
- name: cpu_alertsrules:- alert: HighCPUexpr: avg(rate(node_cpu_seconds_total{mode!="idle"}[5m])) by (instance) > 0.8for: 10mlabels:severity: critical'

五、配置 Confd
1. 监控目标模板配置

创建模板定义文件 /etc/confd/conf.d/prometheus-targets.toml

[template]
src = "targets.tmpl"       # 模板文件名
dest = "/etc/prometheus/targets/node.json"  # 生成的配置文件路径
keys = ["/prometheus/targets/node"]  # 监听的 Consul KV 路径
reload_cmd = "/bin/bash -c 'curl -X POST http://localhost:9090/-/reload'"  # 触发 Prometheus 重载

创建模板文件 /etc/confd/templates/targets.tmpl

[{{range $index, $item := ls "/prometheus/targets/node"}}{{if $index}},{{end}}{"targets": [ "{{.Value}}" ],"labels": {"job": "node","instance": "{{.Key}}"}}{{end}}
]
2. 告警规则模板配置

创建模板定义文件 /etc/confd/conf.d/prometheus-rules.toml

[template]
src = "rules.tmpl"
dest = "/etc/prometheus/rules/alerts.yml"
keys = ["/prometheus/rules"]  # 监听整个 rules 目录
reload_cmd = "/bin/bash -c 'curl -X POST http://localhost:9090/-/reload'"

创建模板文件 /etc/confd/templates/rules.tmpl

groups:
{{range $key, $value := getvs "/prometheus/rules/*"}}{{$value | toYAML | nindent 2}}
{{end}}

六、配置 Prometheus

修改 prometheus.yml 启用动态配置:

scrape_configs:- job_name: 'node'file_sd_configs:- files: ['/etc/prometheus/targets/*.json']  # 加载 Confd 生成的目标文件rule_files:- /etc/prometheus/rules/*.yml  # 加载 Confd 生成的规则文件

启动 Prometheus(启用配置重载 API):

prometheus --web.enable-lifecycle --config.file=/etc/prometheus/prometheus.yml

七、启动 Confd
confd -backend consul -node localhost:8500 -watch -log-level debug

参数说明:

  • -watch:实时监听 Consul 变化
  • -log-level debug:输出详细日志(生产环境建议用 info

八、验证流程
1. 测试服务发现
# 在 Consul 注册新服务
consul kv put prometheus/targets/node/web-server "192.168.1.101:9100"# 检查生成的目标文件
cat /etc/prometheus/targets/node.json# 在 Prometheus Web 界面(9090 端口)检查 Targets
2. 测试告警规则更新
# 修改 Consul 中的告警规则
consul kv put prometheus/rules/memory_high 'groups:
- name: memory_alertsrules:- alert: HighMemoryexpr: node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes < 0.2'# 检查生成的规则文件
cat /etc/prometheus/rules/alerts.yml# 在 Prometheus Web 界面检查 Rules

九、故障排查
  1. Confd 未生成文件

    • 检查 Consul KV 路径是否正确:consul kv get -recurse /prometheus
    • 查看 Confd 日志:journalctl -u confd(若使用 systemd 托管)
  2. Prometheus 未重载配置

    • 确保启动参数包含 --web.enable-lifecycle
    • 手动触发重载:curl -X POST http://localhost:9090/-/reload
  3. 配置语法错误

    • 使用 Prometheus 校验工具:promtool check rules /etc/prometheus/rules/*.yml

十、维护说明
  1. 备份策略

    • 定期备份 Consul 数据:consul snapshot save backup.snap
    • 备份 Confd 模板文件(/etc/confd 目录)
  2. 扩展建议

    • 为不同环境(dev/test/prod)使用 Consul 前缀隔离:/env/prod/prometheus/...
    • 添加 Prometheus 配置版本管理(如与 Git 集成)

附:架构图

注册
注册
KV 变更通知
生成
生成
重载配置
Node Exporter
Consul
App Service
Confd
Prometheus 目标文件
Prometheus 规则文件
Prometheus

通过这篇文档,可实现 Prometheus 监控系统的全动态管理,提升运维自动化程度。

http://www.lqws.cn/news/140329.html

相关文章:

  • 鸿蒙应用开发之uni-app x实践
  • Windows系统工具:WinToolsPlus 之 SQL Server 日志清理
  • SQL进阶之旅 Day 15:动态SQL与条件查询构建
  • Web攻防-SQL注入高权限判定跨库查询文件读写DNS带外SecurePriv开关绕过
  • 路凯智行助力华润水泥长治矿区开启无人运输新场景
  • 6. MySQL基本查询
  • 大语言模型备案与深度合成算法备案的区别与联系
  • NLP中的input_ids是什么?
  • 虚拟机无法开启-关掉虚拟化
  • FAST(Features from Accelerated Segment Test)角检测算法原理详解和C++代码实现
  • 打包成windows exe
  • 群论在现代密码学中的应用探索与实践 —— 从理论到C语言实现
  • 卡特兰数简单介绍
  • gateway 网关 路由新增 (已亲测)
  • 极客时间-《搞定音频技术》-学习笔记
  • L2-056 被n整除的n位数 - java
  • Unity 中实现可翻页的 PageView
  • C++--vector的使用及其模拟实现
  • 【统计方法】蒙特卡洛
  • OpenProject:一款功能全面的开源项目管理软件
  • Android Studio 打包时遇到了签名报错问题:Invalid keystore format
  • PostgreSQL的扩展 pg_buffercache
  • ubuntu 常用操作指令(与域控制器交互相关)
  • 使用qt 定义全局钩子 捕获系统的键盘事件
  • 聊聊芯片Debug模块及其应用
  • 如何快速找出某表的重复记录 - 数据库专家面试指南
  • 618浴室柜推荐,小户型浴室柜怎么选才省心?
  • JAVA 集合进阶 Map集合的实现类 TreeMap
  • 第八部分:第三节 - 事件处理:响应顾客的操作
  • C++ 变量二