Giám sát VPS bằng Grafana: Cài đặt Prometheus và theo dõi tài nguyên VPS theo thời gian thực

Giám sát VPS bằng Grafana: Cài đặt Prometheus và theo dõi tài nguyên VPS theo thời gian thực

Giám sát VPS bằng Grafana: Cài đặt Prometheus và theo dõi tài nguyên VPS theo thời gian thực

Khi vận hành website, ứng dụng, Docker, GitLab, n8n, Canvas LMS, BigBlueButton hoặc hệ thống doanh nghiệp trên VPS, việc giám sát tài nguyên theo thời gian thực là cực kỳ quan trọng.

Nếu không có hệ thống monitoring, bạn chỉ biết VPS gặp vấn đề khi website đã chậm, database đã treo, ổ cứng đã đầy hoặc khách hàng đã báo lỗi.

Trong bài viết này, CloudX sẽ hướng dẫn bạn triển khai bộ giám sát VPS bằng Prometheus + Node Exporter + Grafana, giúp theo dõi CPU, RAM, Disk, Network, Load Average, I/O và cảnh báo sớm sự cố hệ thống.

Mục lục bài viết
  1. Grafana là gì?
  2. Prometheus là gì?
  3. Node Exporter là gì?
  4. Mô hình giám sát VPS bằng Grafana và Prometheus
  5. Yêu cầu CPU/RAM/NVMe
  6. Cài đặt Node Exporter trên VPS
  7. Cài đặt Prometheus
  8. Cài đặt Grafana
  9. Thêm Prometheus Data Source vào Grafana
  10. Import Dashboard giám sát VPS
  11. Cấu hình Firewall và bảo mật
  12. CloudX hỗ trợ triển khai Monitoring

1. Grafana là gì?

Grafana là nền tảng mã nguồn mở dùng để trực quan hóa dữ liệu, tạo dashboard và cảnh báo hệ thống. Grafana có thể kết nối với nhiều nguồn dữ liệu như Prometheus, Loki, InfluxDB, Elasticsearch, MySQL, PostgreSQL và nhiều hệ thống khác.

Với Grafana, quản trị viên có thể nhìn thấy toàn bộ sức khỏe VPS qua dashboard trực quan:

  • CPU đang dùng bao nhiêu phần trăm.
  • RAM còn trống bao nhiêu.
  • Ổ cứng có sắp đầy không.
  • Network đang tăng bất thường không.
  • Load Average có vượt ngưỡng không.
  • Dịch vụ có dấu hiệu nghẽn tài nguyên không.
Note: Grafana không tự thu thập dữ liệu hệ thống. Grafana thường dùng Prometheus làm nguồn dữ liệu để hiển thị biểu đồ và dashboard.

2. Prometheus là gì?

Prometheus là hệ thống monitoring mã nguồn mở chuyên thu thập, lưu trữ và truy vấn dữ liệu dạng time-series. Prometheus sử dụng ngôn ngữ truy vấn PromQL để phân tích metrics, phục vụ dashboard và cảnh báo.

Prometheus hoạt động theo cơ chế pull, nghĩa là Prometheus sẽ chủ động đi lấy metrics từ các target như Node Exporter, cAdvisor, Docker Exporter hoặc ứng dụng có endpoint metrics.

3. Node Exporter là gì?

Node Exporter là công cụ xuất metrics hệ thống Linux cho Prometheus. Khi cài Node Exporter trên VPS, Prometheus có thể thu thập các thông số như:

  • CPU usage.
  • Memory usage.
  • Disk usage.
  • Disk I/O.
  • Network traffic.
  • Filesystem.
  • Load Average.
  • System uptime.

4. Mô hình giám sát VPS bằng Grafana và Prometheus

Mô hình đơn giản:

VPS cần giám sát
      |
      | Node Exporter :9100
      |
Prometheus :9090
      |
      | Data Source
      |
Grafana :3000
      |
Dashboard theo dõi CPU, RAM, Disk, Network

Có thể cài toàn bộ trên một VPS hoặc tách riêng:

  • VPS ứng dụng: chạy Node Exporter.
  • VPS Monitoring: chạy Prometheus và Grafana.
  • Nhiều VPS: mỗi VPS chạy Node Exporter, Prometheus thu thập tập trung.
Warning: Không nên public trực tiếp port 9100, 9090, 3000 ra Internet nếu chưa có firewall, VPN hoặc reverse proxy bảo mật.

5. Yêu cầu CPU/RAM/NVMe

Quy mô giám sát CPU RAM NVMe Ghi chú
1-5 VPS 2 vCPU 4 GB 40 GB NVMe Phù hợp lab, website nhỏ
5-20 VPS 4 vCPU 8 GB 80-120 GB NVMe Phù hợp doanh nghiệp nhỏ
20-100 VPS 8 vCPU 16 GB 200 GB NVMe Cần tối ưu retention và scrape interval
Hạ tầng lớn Cluster 32 GB+ 300 GB+ Nên tách monitoring, logging, alerting
Note: Với hệ thống giám sát thời gian thực, ổ NVMe giúp Prometheus ghi dữ liệu time-series ổn định hơn, đặc biệt khi số lượng metrics tăng.

6. Cài đặt Node Exporter trên VPS

Tạo user riêng cho Node Exporter:

sudo useradd --no-create-home --shell /usr/sbin/nologin node_exporter

Tải Node Exporter:

cd /tmp
wget https://github.com/prometheus/node_exporter/releases/latest/download/node_exporter-1.9.1.linux-amd64.tar.gz
tar xvf node_exporter-1.9.1.linux-amd64.tar.gz
sudo cp node_exporter-1.9.1.linux-amd64/node_exporter /usr/local/bin/
sudo chown node_exporter:node_exporter /usr/local/bin/node_exporter

Tạo service systemd:

sudo nano /etc/systemd/system/node_exporter.service

Dán nội dung sau:

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

[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target

Khởi động Node Exporter:

sudo systemctl daemon-reload
sudo systemctl enable node_exporter
sudo systemctl start node_exporter
sudo systemctl status node_exporter

Kiểm tra metrics:

curl http://localhost:9100/metrics

7. Cài đặt Prometheus

Tạo user Prometheus:

sudo useradd --no-create-home --shell /usr/sbin/nologin prometheus

Tạo thư mục:

sudo mkdir -p /etc/prometheus
sudo mkdir -p /var/lib/prometheus
sudo chown prometheus:prometheus /etc/prometheus
sudo chown prometheus:prometheus /var/lib/prometheus

Tải Prometheus:

cd /tmp
wget https://github.com/prometheus/prometheus/releases/latest/download/prometheus-3.5.0.linux-amd64.tar.gz
tar xvf prometheus-3.5.0.linux-amd64.tar.gz
cd prometheus-3.5.0.linux-amd64

Copy binary:

sudo cp prometheus promtool /usr/local/bin/
sudo cp -r consoles console_libraries /etc/prometheus/
sudo chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus
sudo chown prometheus:prometheus /usr/local/bin/prometheus /usr/local/bin/promtool

Tạo file cấu hình Prometheus:

sudo nano /etc/prometheus/prometheus.yml

Dán nội dung sau:

global:
  scrape_interval: 15s
  evaluation_interval: 15s

scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]

  - job_name: "node_exporter"
    static_configs:
      - targets: ["localhost:9100"]

Tạo service Prometheus:

sudo nano /etc/systemd/system/prometheus.service

Dán nội dung sau:

[Unit]
Description=Prometheus Monitoring System
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 \
  --storage.tsdb.retention.time=15d

[Install]
WantedBy=multi-user.target

Khởi động Prometheus:

sudo systemctl daemon-reload
sudo systemctl enable prometheus
sudo systemctl start prometheus
sudo systemctl status prometheus

Kiểm tra Prometheus:

http://YOUR_SERVER_IP:9090

8. Cài đặt Grafana

Cài các gói cần thiết:

sudo apt install -y apt-transport-https software-properties-common wget gpg

Thêm Grafana GPG key:

sudo mkdir -p /etc/apt/keyrings
wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor | sudo tee /etc/apt/keyrings/grafana.gpg > /dev/null

Thêm repository Grafana:

echo "deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main" | sudo tee /etc/apt/sources.list.d/grafana.list

Cài Grafana:

sudo apt update
sudo apt install -y grafana

Khởi động Grafana:

sudo systemctl enable grafana-server
sudo systemctl start grafana-server
sudo systemctl status grafana-server

Truy cập Grafana:

http://YOUR_SERVER_IP:3000

Tài khoản mặc định:

Username: admin
Password: admin
Warning: Sau lần đăng nhập đầu tiên, bạn cần đổi ngay mật khẩu admin của Grafana.

9. Thêm Prometheus Data Source vào Grafana

Trong giao diện Grafana:

  • Vào Connections.
  • Chọn Data Sources.
  • Chọn Prometheus.
  • URL nhập: http://localhost:9090.
  • Nhấn Save & Test.

Nếu Grafana báo thành công, nghĩa là Grafana đã kết nối được Prometheus.

10. Import Dashboard giám sát VPS

Bạn có thể import dashboard phổ biến cho Node Exporter:

Dashboard ID: 1860

Các bước:

  • Vào Dashboards.
  • Chọn New.
  • Chọn Import.
  • Nhập ID 1860.
  • Chọn Prometheus Data Source.
  • Nhấn Import.

Sau khi import, bạn sẽ thấy các biểu đồ:

  • CPU Basic.
  • Memory Basic.
  • Network Traffic.
  • Disk Space Used.
  • Disk I/O.
  • System Load.
  • Uptime.

11. Cấu hình Firewall và bảo mật

Nếu chỉ dùng nội bộ, bạn nên giới hạn port theo IP quản trị.

sudo ufw allow OpenSSH
sudo ufw allow from YOUR_ADMIN_IP to any port 3000 proto tcp
sudo ufw allow from YOUR_ADMIN_IP to any port 9090 proto tcp
sudo ufw allow from YOUR_MONITORING_IP to any port 9100 proto tcp
sudo ufw enable
sudo ufw status

Nếu muốn truy cập Grafana bằng domain, nên cấu hình Nginx Reverse Proxy và SSL.

Note: Với hệ thống production, nên đặt Grafana sau HTTPS, bật mật khẩu mạnh, giới hạn IP truy cập và không public Prometheus/Node Exporter ra Internet.

12. CloudX hỗ trợ triển khai Monitoring chuyên nghiệp

CloudX - Giải pháp giám sát VPS, Server và hệ thống doanh nghiệp

CloudX hỗ trợ triển khai hệ thống giám sát tài nguyên theo thời gian thực cho:

  • Cloud VPS.
  • Server vật lý.
  • Docker.
  • GitLab.
  • n8n.
  • Canvas LMS.
  • BigBlueButton.
  • Website doanh nghiệp.
  • Database.
  • Firewall và hệ thống mạng.

Dịch vụ bao gồm:

  • Cài đặt Prometheus.
  • Cài đặt Grafana.
  • Cài đặt Node Exporter.
  • Thiết kế Dashboard theo nhu cầu.
  • Cấu hình cảnh báo CPU/RAM/Disk/Network.
  • Backup và tối ưu retention metrics.
  • Giám sát 24/7.

Hotline/Zalo: 0983.357.585

Kết luận

Grafana và Prometheus là bộ đôi rất mạnh để giám sát VPS theo thời gian thực. Khi kết hợp với Node Exporter, quản trị viên có thể theo dõi toàn bộ tài nguyên hệ thống như CPU, RAM, Disk, Network, Load Average và phát hiện sự cố sớm hơn.

Với doanh nghiệp đang vận hành website, phần mềm, LMS, Docker hoặc hệ thống cloud riêng, monitoring không còn là tùy chọn mà là thành phần bắt buộc để đảm bảo hệ thống ổn định, an toàn và dễ mở rộng.

Tags: Grafana, Prometheus, Node Exporter, Giám sát VPS, Cloud VPS Monitoring, Theo dõi tài nguyên VPS, Monitoring Server, DevOps, Cloud VPS NVMe, CloudX.

BÀI VIẾT CÙNG CHUYÊN MỤC

Không copy được giữa máy Windows và máy ảo qua mRemoteNG/RDP: Nguyên nhân và cách sửa
Không copy được giữa máy Windows và máy ảo qua ...

mRemoteNG Remote Desktop RDP Clipboard Redirection rdpclip.exe VPS Windows ...

Hướng dẫn bật Nested Virtualization trên ESXi để chạy Android Studio Emulator trong máy ảo Windows
Hướng dẫn bật Nested Virtualization trên ESXi để chạy ...

Nested Virtualization ESXi VMware Android Studio Android Emulator WHPX Hyper-V ...

PITR (Point In Time Recovery) là gì? Khôi phục Database về đúng thời điểm cần thiết
PITR (Point In Time Recovery) là gì? Khôi phục ...

PITR, Point In Time Recovery, PITR là gì, Database Recovery, PostgreSQL PITR, ...

Backup Database Full, Incremental, Differential khác nhau thế nào?
Backup Database Full, Incremental, Differential khác ...

Backup Database, Full Backup, Incremental Backup, Differential Backup, Backup ...

Database Cluster là gì? High Availability, Failover và Cluster Database hoạt động như thế nào?
Database Cluster là gì? High Availability, Failover và ...

Database Cluster, Database Cluster là gì, High Availability Database, Database ...