Hướng dẫn reload cấu hình dịch vụ Prometheus

Hướng dẫn reload cấu hình dịch vụ PrometheusCuongquach.com | Một trong những câu hỏi cũng khá phổ biến khi quản lý dịch vụ giám sát Prometheus thời gian đầu, đó là có cách nào để reload config mà không cần phải khởi động lại dịch vụ Prometheus mỗi lần thay đổi hay không ?

lam-sao-reload-config-prometheus

Tin vui là bạn có cách để reload config Prometheus đấy. Tính đến phiên bản Prometheus lúc viết bài này 2.x thì bạn có 2 cách để thực hiện việc này :

  • Gửi tín hiệu SIGHUP đến pid của tiến trình Prometheus
  • Gửi HTTP Post đến API Prometheus

1. Tín hiệu SIGHUP

Gửi tín hiệu SIGHUP đến pid của tiến trình Prometheus nhìn chung là cách khá củ chuối, nhưng Prometheus có hỗ trợ thì bạn sử dụng cách này cũng được.

Xác định pid của tiến trình Prometheus.

# ps aux| grep -i prometheus | grep -v "grep" | awk '{print $2}'
35953

Gửi tín hiệu SIGHUP đến Prometheus.

# kill -HUP 35953

Khi bạn xem trạng thái dịch vụ Prometheus sẽ thấy có log ghi nhận việc reload file config thành công hay thất bại.

# systemctl status prometheus
● prometheus.service - Prometheus
Loaded: loaded (/etc/systemd/system/prometheus.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2019-08-22 15:41:08 +07; 1h 9min ago
Main PID: 35953 (prometheus)
CGroup: /system.slice/prometheus.service
└─35953 /usr/local/bin/prometheus --config.file /etc/prometheus/prometheus.yml --storage.tsdb.path /var/lib/prometheus/ --web.console.templates=/etc/prometheus/consoles --web....
..
..
Aug 22 16:50:48 gtd-ibe-ntp-02 prometheus[35953]: level=info ts=2019-08-22T09:50:48.402Z caller=main.go:740 msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
Aug 22 16:50:48 gtd-ibe-ntp-02 prometheus[35953]: level=info ts=2019-08-22T09:50:48.410Z caller=main.go:768 msg="Completed loading of configuration file" filename=/etc/prometh...metheus.yml

Nếu mà không reload được cấu hình Prometheus thì thường sẽ có log như thế này :

level=info ts=2019-08-22T09:50:48.402Z caller=main.go:740 "Loading configuration file prometheus.yml source=main.go:196"
level=error ts=2019-08-22T09:50:48.410Z caller=main.go:768 "Couldn't load configuration (-config.file=prometheus.yml): yaml: line 1: mapping values are not allowed in this context source=main.go:208"

2. Gửi HTTP Post đến API Prometheus

Bạn cần kích hoạt tính năng ‘web.enable-lifecycle‘ trong file khởi động dịch vụ Prometheus.

# vi /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
--config.file /etc/prometheus/prometheus.yml \
--storage.tsdb.path /var/lib/prometheus/ \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries \
--web.enable-lifecycle

[Install]
WantedBy=multi-user.target

Khởi động lại dịch vụ Prometheus.

# systemctl daemon-reload
# systemctl restart prometheus

Giờ nếu bạn muốn reload config của dịch vụ Prometheus, bạn chỉ cần gọi lên API URL của Prometheus theo cú pháp dưới để báo Prometheus reload config mới.

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

Khi bạn kiểm tra lại trạng thái dịch vụ Prometheus sẽ thấy log reload config giống như cách 1.

# systemctl status prometheus
...

Aug 22 17:02:47 gtd-ibe-ntp-02 prometheus[38257]: level=info ts=2019-08-22T10:02:47.756Z caller=main.go:740 msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
Aug 22 17:02:47 gtd-ibe-ntp-02 prometheus[38257]: level=info ts=2019-08-22T10:02:47.837Z caller=main.go:768 msg="Completed loading of configuration file" filename=/etc/prometh...metheus.yml

Nguồn: https://cuongquach.com/

Previous articleEbook Giáo trình Bảo mật Wifi – Athena
Next articleCấu hình Prometheus tự động giám sát AWS EC2 – Service Discovery
Bạn đang theo dõi website "https://cuongquach.com/" nơi lưu trữ những kiến thức tổng hợp và chia sẻ cá nhân về Quản Trị Hệ Thống Dịch Vụ & Mạng, được xây dựng lại dưới nền tảng kinh nghiệm của bản thân mình, Quách Chí Cường. Hy vọng bạn sẽ thích nơi này !