Grafana – Prometheus, giám sát hệ thống (Monitoring) là trái tim của việc vận hành Server. Nếu không có nó, bạn giống như đang lái xe bịt mắt mà không biết tốc độ hay lượng xăng còn lại.
Trong bài viết này, chúng ta sẽ đi sâu vào việc xây dựng hệ thống giám sát mạnh mẽ nhất hiện nay: Cài đặt Prometheus và Grafana trực tiếp trên nền tảng Linux (Ubuntu/Debian/CentOS). Giải pháp này giúp bạn thu thập dữ liệu (Metrics) và vẽ biểu đồ trực quan để theo dõi sức khỏe máy chủ theo thời gian thực.Hình ảnh Dashboard giám sát trực quan trên Grafana – Prometheus
1. Hướng dẫn cài đặt Prometheus
Prometheus đóng vai trò là “người thu thập dữ liệu”. Nó sẽ định kỳ lấy các thông số (metrics) từ các dịch vụ và lưu trữ lại.
1.1. Tải về gói cài đặt
Truy cập terminal của máy chủ và tải phiên bản mới nhất (Ví dụ v3.1.0):
wget https://github.com/prometheus/prometheus/releases/download/v3.1.0/prometheus-3.1.0.linux-amd64.tar.gz
1.2. Giải nén và sắp xếp thư mục
Chúng ta cần giải nén và di chuyển các file thực thi vào đúng vị trí chuẩn của Linux:
# Giải nén
tar -xvf prometheus-3.1.0.linux-amd64.tar.gz
# Di chuyển file chạy
cp prometheus-3.1.0.linux-amd64/prometheus /usr/local/bin/
# Di chuyển thư mục cấu hình và thư viện
mv prometheus-3.1.0.linux-amd64 /usr/local/prometheus/
mkdir -p /etc/prometheus
mv /usr/local/prometheus/prometheus.yml /etc/prometheus
1.3. Cấu hình Service (Systemd)
Để Prometheus tự động chạy khi khởi động máy chủ, ta cần tạo một service file.
Tạo file mới: vi /etc/systemd/system/prometheus.service và dán nội dung sau:
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
[Service]
User=root
Group=root
Type=simple
ExecStart=/usr/local/bin/prometheus \
--config.file /etc/prometheus/prometheus.yml \
--storage.tsdb.path /usr/local/prometheus/ \
--web.console.templates=/usr/local/prometheus/consoles \
--web.console.libraries=/usr/local/prometheus/console_libraries
[Install]
WantedBy=multi-user.target
Lưu ý: Đường dẫn `ExecStart` đã được trỏ về `/usr/local/bin/prometheus` như đã copy ở bước 1.2.
1.4. Khởi động Prometheus
Chạy các lệnh sau để kích hoạt dịch vụ:
systemctl daemon-reload
systemctl enable prometheus
systemctl restart prometheus
systemctl status prometheus
Nếu thấy trạng thái Active (running) màu xanh lá là thành công. Bạn có thể truy cập qua: http://IP-Server:9090.
2. Hướng dẫn cài đặt Grafana
Grafana là “người họa sĩ”, nó lấy dữ liệu từ Prometheus để vẽ nên các biểu đồ đẹp mắt.
2.1. Cài đặt các gói phụ thuộc
Trên Ubuntu/Debian, hãy chạy lệnh:
sudo apt-get install -y adduser libfontconfig1 musl
2.2. Tải và cài đặt Grafana Enterprise
Chúng ta sẽ sử dụng file .deb để cài đặt nhanh chóng:
wget https://dl.grafana.com/enterprise/release/grafana-enterprise_11.4.0_amd64.deb
sudo dpkg -i grafana-enterprise_11.4.0_amd64.deb
2.3. Khởi động Grafana Server
systemctl daemon-reload
systemctl enable grafana-server
systemctl restart grafana-server
systemctl status grafana-server
- URL truy cập:
http://IP-Server:3000 - Tài khoản mặc định:
admin - Mật khẩu mặc định:
admin(Hệ thống sẽ yêu cầu đổi pass ngay lần đầu).
3. Kết nối Prometheus vào Grafana
Cài đặt xong 2 công cụ nhưng chúng chưa “nói chuyện” với nhau. Bạn cần thực hiện bước cuối cùng này:
- Đăng nhập vào giao diện Grafana.
- Vào mục Connections > Data Sources > Nhấn Add new data source.
- Chọn Prometheus.
- Tại phần Prometheus server URL, điền:
http://localhost:9090(Nếu cài chung trên 1 server) hoặchttp://IP-Prometheus:9090. - Kéo xuống dưới cùng và nhấn nút Save & Test.
Nếu hiện thông báo màu xanh: “Successfully queried the Prometheus API” tức là bạn đã thiết lập thành công!
Kết luận
Vậy là bạn đã hoàn tất việc cài đặt Prometheus và Grafana. Bây giờ hệ thống đã sẵn sàng thu thập dữ liệu. Tuy nhiên, để Prometheus có thể lấy được các thông số như CPU, RAM, ổ cứng của máy chủ, bạn cần cài đặt thêm một thành phần nhỏ nữa gọi là Node Exporter.
Một vài exporter thông dụng:
| Exporter | Dành cho | Thu thập |
|---|---|---|
| Node Exporter | Linux | CPU, RAM, Disk, Network |
| Windows Exporter | Windows | Counters, RAM, Disk, Network |
| cAdvisor | Docker/K8s | Container metrics |
| Asterisk Exporter | PBX | Call stats, SIP peers |
| Blackbox Exporter | Website/API | Ping, HTTP, DNS |
| MySQL/Postgres Exporter | Database | QPS, queries, connections |
| Nginx Exporter | Web server | Requests, traffic |
| Redis Exporter | Redis cache | Memory, keys, client stats |