SIEM/Log Management [Prometheus] Thiết Lập Cụm Prometheus High Availability (HA) Mô Hình Active-Standby Kết Hợp Keepalived

1. Vấn đề​

Trong các hệ thống giám sát hạ tầng, Prometheus là một tiêu chuẩn. Để đảm bảo High Availability (HA), cách truyền thống là chạy nhiều node Prometheus song song cùng thu thập metric.
Tuy nhiên, nếu bạn sử dụng các Database backend không hỗ trợ tính năng tự động loại bỏ dữ liệu trùng lặp (Deduplication) như OpenSearch hay Elasticsearch để lưu metric, việc nhiều node Prometheus cùng đẩy remote_write sẽ khiến dữ liệu bị nhân đôi, nhân ba. Hệ quả là đồ thị trên Grafana/Dashboards bị sai lệch hoàn toàn.
Giải pháp: Xây dựng cụm Prometheus HA theo mô hình Active - Standby.
  • Cả 3 node Prometheus đều liên tục thu thập dữ liệu (scrape) để duy trì tính liên tục của time-series.
  • Sử dụng Keepalived để bầu chọn Leader. Tại một thời điểm, chỉ có duy nhất 1 node (Active) được phép đẩy dữ liệu (remote_write) về Backend. Các node còn lại (Standby) chỉ lưu tại local.

2. Kiến trúc hệ thống​

  • Node 1 (Master): IP 10.123.100.217
  • Node 2 (Backup 1): IP 10.123.100.218
  • Node 3 (Backup 2): IP 10.123.100.219
  • Virtual IP (VIP): 192.168.1.200 (Quản lý trạng thái cụm)
  • Backend Receiver (Ví dụ Vector/Logstash): IP 10.123.100.220:9001

3. Triển khai​

Bước 1: Cài đặt Prometheus và Mở Lifecycle API​

Đây là bước cực kỳ quan trọng. Ta phải cho phép Prometheus nạp lại cấu hình (Hot-reload) thông qua API mà không cần restart dịch vụ.

1. Tạo user và thư mục:​

sudo useradd --no-create-home --shell /bin/false prometheus
sudo mkdir /etc/prometheus /var/lib/prometheus
sudo chown prometheus:prometheus /etc/prometheus /var/lib/prometheus

2. Cài đặt file Systemd Service (/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.enable-lifecycle

[Install]
WantedBy=multi-user.target
Reload daemon và khởi chạy:
sudo systemctl daemon-reload
sudo systemctl enable --now prometheus

Bước 2: Chuẩn bị Kịch bản Cấu hình (Active & Standby)​

Thay vì sử dụng trực tiếp file <span>prometheus.yml</span>, chúng ta tạo 2 template. Kịch bản Keepalived sẽ tự động copy template tương ứng đè lên file chính.

1. File cấu hình Active (/etc/prometheus/prometheus-active.yml):​

Có phần remote_write để đẩy dữ liệu:
global:
scrape_interval: 60s

scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["10.123.100.217:9090", "10.123.100.218:9090", "10.123.100.219:9090"]

# Node làm Master sẽ kích hoạt đoạn này
remote_write:
- url: "http://10.123.100.220:9001/api/v1/write" # Nhớ thêm path API tùy backend

2. File cấu hình Standby (/etc/prometheus/prometheus-standby.yml):​

KHÔNG chứa remote_write, chỉ scrape để dự phòng data
global:
scrape_interval: 60s

scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["10.123.100.217:9090", "10.123.100.218:9090", "10.123.100.219:9090"]
Gán quyền cho thư mục:
sudo chown -R prometheus:prometheus /etc/prometheus/

Bước 3: Cấu hình Keepalived & Script Chuyển Trạng Thái​

1. Cài đặt Keepalived:​

sudo apt install keepalived -y

2. Tạo Script Notify (/etc/keepalived/notify.sh):​

Script này chịu trách nhiệm đổi file cấu hình và gọi API reload của Prometheus.
#!/bin/bash

TYPE=$1
NAME=$2
STATE=$3

PROMETHEUS_DIR="/etc/prometheus"
LOG_FILE="/var/log/keepalived_prom.log"

echo "$(date '+%Y-%m-%d %H:%M:%S') - Transitioning to $STATE state" >> $LOG_FILE

case $STATE in
"MASTER")
cp $PROMETHEUS_DIR/prometheus-active.yml $PROMETHEUS_DIR/prometheus.yml
chown prometheus:prometheus $PROMETHEUS_DIR/prometheus.yml
curl -s -X POST http://localhost:9090/-/reload
echo "Prometheus reloaded with ACTIVE config (remote_write enabled)" >> $LOG_FILE
;;
"BACKUP"|"FAULT"|"STOP")
cp $PROMETHEUS_DIR/prometheus-standby.yml $PROMETHEUS_DIR/prometheus.yml
chown prometheus:prometheus $PROMETHEUS_DIR/prometheus.yml
curl -s -X POST http://localhost:9090/-/reload
echo "Prometheus reloaded with STANDBY config (remote_write disabled)" >> $LOG_FILE
;;
*)
echo "Unknown state: $STATE" >> $LOG_FILE
exit 1
;;
esac
Cấp quyền thực thi:
sudo chmod +x /etc/keepalived/notify.sh

3. Cấu hình Keepalived (/etc/keepalived/keepalived.conf):​

Thêm script check tiến trình Prometheus để tự động hạ cấp node nếu ứng dụng lỗi.
Trên Node 1 (Master):
vrrp_script chk_prometheus {
script "pidof prometheus"
interval 2
weight -20 # Hạ 20 điểm priority nếu Prometheus chết
}

vrrp_instance VI_PROMETHEUS {
state MASTER
interface ens33 # Đổi thành tên card mạng thực tế
virtual_router_id 51
priority 100 # Priority cao nhất
advert_int 1

virtual_ipaddress {
192.168.1.200/24
}

track_script {
chk_prometheus
}

notify "/etc/keepalived/notify.sh"
notify_stop "/etc/keepalived/notify.sh STOP" # Bắt sự kiện stop
}
Trên Node 2 và 3 (Standby): Cấu hình tương tự Node 1, nhưng thay đổi state thành BACKUP và hạ priority xuống (ví dụ: Node 2 priority 90, Node 3 priority 80).

4. Vận hành và Kiểm tra (Troubleshooting)​

Đọc log Keepalived để xem tiến trình thăng cấp/hạ cấp:
tail -f /var/log/keepalived_prom.log
 
Back
Top