热重载Prometheus和AlertManager配置文件

文章目录
  1. 1. Prometheus配置文件重载
  2. 2. AlertManager文件重载

如果每次修改配置文件都需要重启服务,那就太操蛋了。

Prometheus配置文件重载

必要条件:Prometheus在2.0版本后hot reload 功能是默认关闭的,如需开启,需要在启动Prometheus的时候,添加 --web.enable-lifecycle 参数。

1
2
# Docker 运行示例
docker run -d --name prom-server prometheus --web.enable-lifecycle --config.file=/etc/prometheus/prometheus.yml

重载文件

1
2

curl -X POST http://WEBURL/-/reload

接下来就可以看文件有没有reload日志打印了

1
2
3
4
5
6
ts=2024-10-08T07:38:57.506Z caller=main.go:1367 level=info msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
ts=2024-10-08T07:38:57.508Z caller=main.go:1415 level=info msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml totalDuration=1.791304ms db_storage=1.533µs remote_storage=1.493µs web_handler=471ns query_engine=1.122µs scrape=74.5µs scrape_sd=39.955µs notify=17.532µs notify_sd=11.622µs rules=1.293589ms tracing=2.935µs
ts=2024-10-08T07:39:24.210Z caller=main.go:1367 level=info msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
ts=2024-10-08T07:39:24.212Z caller=main.go:1415 level=info msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml totalDuration=1.753011ms db_storage=1.512µs remote_storage=1.503µs web_handler=541ns query_engine=1.182µs scrape=84.148µs scrape_sd=37.81µs notify=15.93µs notify_sd=9.338µs rules=1.194914ms tracing=3.727µs
ts=2024-10-08T08:02:58.620Z caller=main.go:1367 level=info msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
ts=2024-10-08T08:02:58.622Z caller=main.go:1415 level=info msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml totalDuration=1.705142ms db_storage=1.603µs remote_storage=1.803µs web_handler=501ns query_engine=1.062µs scrape=81.222µs scrape_sd=34.655µs notify=15.208µs notify_sd=10.019µs rules=1.183322ms tracing=3.437µs

AlertManager文件重载

AlertManager默认支持热重载文件,通过以下命令进行重载操作。

1
http://WEBURL/-/reload

查看日志是否有reload

1
2
ts=2024-10-08T08:13:15.421Z caller=coordinator.go:113 level=info component=configuration msg="Loading configuration file" file=/etc/alertmanager/alertmanager.yml
ts=2024-10-08T08:13:15.422Z caller=coordinator.go:126 level=info component=configuration msg="Completed loading of configuration file" file=/etc/alertmanager/alertmanager.yml