Prometheus là một hệ thống giám sát mã nguồn mở mạnh mẽ, chuyên thu thập và lưu trữ các chỉ số (metrics) dưới dạng dữ liệu chuỗi thời gian (time-series database). Kết hợp với Grafana để trực quan hóa và các Exporter để thu thập dữ liệu từ hệ thống, bạn sẽ có một giải pháp giám sát toàn diện.
1. Cài đặt Prometheus
Đầu tiên, tải xuống bản phân phối Prometheus và giải nén vào thư mục hệ thống:
wget https://github.com/prometheus/prometheus/releases/download/v2.35.0/prometheus-2.35.0.linux-amd64.tar.gz
tar -xvf prometheus-2.35.0.linux-amd64.tar.gz -C /usr/local/
mv /usr/local/prometheus-2.35.0.linux-amd64 /usr/local/prometheus
Khởi tạo file cấu hình dịch vụ Systemd để quản lý Prometheus:
sudo nano /etc/systemd/system/prometheus.service
Nội dung file cấu hình:
[Unit]
Description=Prometheus Monitoring System
Wants=network-online.target
After=network-online.target
[Service]
User=root
Group=root
Type=simple
ExecStart=/usr/local/prometheus/prometheus \
--config.file=/usr/local/prometheus/prometheus.yml \
--storage.tsdb.path=/usr/local/prometheus/data \
--web.console.templates=/usr/local/prometheus/consoles \
--web.console.libraries=/usr/local/prometheus/console_libraries
[Install]
WantedBy=multi-user.target
Cập nhật file /usr/local/prometheus/prometheus.yml để định nghĩa các điểm cuối cần thu thập dữ liệu:
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'prometheus_service'
static_configs:
- targets: ['localhost:9090']
- job_name: 'node_metrics'
scrape_interval: 5s
static_configs:
- targets: ['localhost:9100']
- job_name: 'mysql_metrics'
scrape_interval: 5s
static_configs:
- targets: ['localhost:9104']
Khởi chạy dịch vụ:
sudo systemctl daemon-reload
sudo systemctl enable --now prometheus
2. Triển khai Node Exporter
Node Exporter dùng để thu thập các thông số phần cứng như CPU, RAM và Disk.
wget https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz
tar -xvf node_exporter-1.3.1.linux-amd64.tar.gz -C /usr/local/
mv /usr/local/node_exporter-1.3.1.linux-amd64 /usr/local/node_exporter
Tạo file dịch vụ cho Node Exporter:
sudo nano /etc/systemd/system/node_exporter.service
[Unit]
Description=Node Exporter Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/node_exporter/node_exporter
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now node_exporter
3. Cấu hình MySQL và MySQL Exporter
Cài đặt MySQL Server nếu máy chủ chưa có:
sudo apt update
sudo apt install mysql-server -y
Tạo người dùng chuyên dụng để Prometheus thu thập dữ liệu:
sudo mysql -u root -p
-- Trong MySQL shell
CREATE USER 'exporter_user'@'localhost' IDENTIFIED BY 'StrongPassword123' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Cài đặt MySQL Exporter:
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.14.0/mysqld_exporter-0.14.0.linux-amd64.tar.gz
tar -xvf mysqld_exporter-0.14.0.linux-amd64.tar.gz -C /usr/local/
mv /usr/local/mysqld_exporter-0.14.0.linux-amd64 /usr/local/mysqld_exporter
Cấu hình thông tin xác thực cho Exporter:
sudo nano /usr/local/mysqld_exporter/.my.cnf
[client]
user=exporter_user
password=StrongPassword123
Tạo dịch vụ Systemd cho MySQL Exporter:
sudo nano /etc/systemd/system/mysqld_exporter.service
[Unit]
Description=MySQL Exporter
After=network.target
[Service]
User=root
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter \
--config.my-cnf=/usr/local/mysqld_exporter/.my.cnf
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now mysqld_exporter
4. Cài đặt Grafana
Grafana cung cấp giao diện trực quan để quan sát dữ liệu từ Prometheus.
wget https://dl.grafana.com/oss/release/grafana-8.0.4.linux-amd64.tar.gz
tar -zxvf grafana-8.0.4.linux-amd64.tar.gz -C /usr/local/
Tạo file dịch vụ cho Grafana:
sudo nano /etc/systemd/system/grafana-server.service
[Unit]
Description=Grafana Dashboard
After=network-online.target
[Service]
User=root
Type=simple
WorkingDirectory=/usr/local/grafana-8.0.4
ExecStart=/usr/local/grafana-8.0.4/bin/grafana-server --config=/usr/local/grafana-8.0.4/conf/defaults.ini
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now grafana-server
5. Cấu hình giao diện Grafana
Truy cập vào giao diện web qua địa chỉ http://IP_SERVER:3000 (Tài khoản mặc định: admin/admin).
- Thêm Data Source: Vào mục Configuration -> Data Sources, chọn Prometheus. Nhập URL là
http://localhost:9090và nhấn Save & Test. - Import Dashboard cho hệ thống: Chọn biểu tượng "+" -> Import. Nhập mã ID
11074(Node Exporter Full) để theo dõi thông số server. - Import Dashboard cho MySQL: Lặp lại bước Import với mã ID
7362để theo dõi các chỉ số của cơ sở dữ liệu MySQL.
Hệ thống của bạn hiện đã hoàn tất việc thiết lập giám sát tự động cho cả hệ điều hành và cơ sở dữ liệu MySQL.