1. 程式人生 > 實用技巧 >【Prometheus】熱載入方法

【Prometheus】熱載入方法

在執行時熱載入Prometheus的配置資訊

Promtheus的時序資料庫在儲存了大量的資料後,每次重啟Prometheus程序的時間會越來越慢。 而在日常運維工作中會經常調整Prometheus的配置資訊,實際上Prometheus提供了在執行時熱載入配置資訊的功能。

Prometheus配置的熱載入

Prometheus配置資訊的熱載入有兩種方式:

第一種熱載入方式:

檢視Prometheus的程序id,傳送SIGHUP訊號:

kill -HUP <pid>

第二種熱載入方式:

傳送一個POST請求到/-/reload,需要在啟動時給定--web.enable-lifecycle

選項:

curl -X POST http://localhost:9090/-/reload

如果配置熱載入成功,Prometheus會打印出下面的log Loading configuration file:

Jan 11 02:48:22 proxy01 prometheus[392]: level=info ts=2021-01-11T02:48:22.215Z caller=main.go:799 msg="Loading configuration file" filena...heus.yml

我們使用的是第一種熱載入方式,systemd unit檔案如下:

我的 prometheus 是在 centos7 系統下面yum 安裝的 rpm 包,預設是沒有 ExecReload 的系統啟動配置,只有 start,所以加上這一句則可。

[root@ld_proxy01 ~]# cat /etc/systemd/system/prometheus.service

[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
CPUAffinity=3
Type=simple
ExecStart=/etc/prometheus/prometheus \
    --config.file /etc/prometheus/prometheus.yml \
    --storage.tsdb.path /var/lib/prometheus/ \
    --storage.tsdb.retention.time=90d \
    --web.console.templates=/etc/prometheus/consoles \
    --web.console.libraries=/etc/prometheus/console_libraries \
    --web.external-url=http://prometheus.op.hwtech.hk:10080
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target

然後重新載入一下配置即可,一定要執行,否則會報錯。

systemctl daemon-reload

在更改完/etc/prometheus/prometheus.yml之後,僅需要重新載入配置,而不需重啟程序時,只需要執行以下命令即可。

systemctl reload prometheus