Quay lại danh sách
Tin tức công nghệ

Monitoring VPS như Pro: Hướng dẫn triển khai Grafana + Prometheus + Alerting hoàn toàn miễn phí

14 tháng 5, 2026

Tại sao cần Monitoring cho VPS?

Trong thời đại số hóa, việc duy trì uptime và hiệu suất ổn định cho hệ thống là yếu tố sống còn đối với mọi doanh nghiệp. Một VPS không được giám sát có thể gặp phải các vấn đề nghiêm trọng mà bạn không hề hay biết: CPU quá tải, RAM cạn kiệt, disk đầy, hoặc các dịch vụ ngừng hoạt động.

Thay vì phải trả hàng trăm đô la mỗi tháng cho các giải pháp monitoring thương mại như Datadog hay New Relic, bạn hoàn toàn có thể xây dựng một hệ thống giám sát chuyên nghiệp với bộ ba Grafana + Prometheus + Alertmanager – hoàn toàn miễn phí và mã nguồn mở.

Tổng quan về Stack Monitoring

Prometheus: Trái tim của hệ thống

Prometheus là một hệ thống monitoring và alerting mã nguồn mở được phát triển bởi SoundCloud, hiện đang được duy trì bởi Cloud Native Computing Foundation (CNCF). Prometheus hoạt động theo mô hình pull-based, định kỳ thu thập metrics từ các target được cấu hình.

Ưu điểm nổi bật:

  • Time-series database mạnh mẽ với ngôn ngữ truy vấn PromQL linh hoạt
  • Service discovery tự động cho các môi trường động
  • Không phụ thuộc vào storage phân tán, dễ dàng triển khai
  • Hỗ trợ đa chiều data model với labels
  • Cộng đồng lớn với hàng nghìn exporter có sẵn

Grafana: Giao diện trực quan hóa

Grafana là nền tảng phân tích và trực quan hóa dữ liệu hàng đầu, cho phép bạn tạo các dashboard đẹp mắt và tương tác từ nhiều nguồn dữ liệu khác nhau. Khi kết hợp với Prometheus, Grafana trở thành công cụ không thể thiếu để theo dõi metrics theo thời gian thực.

Alertmanager: Hệ thống cảnh báo thông minh

Alertmanager xử lý các cảnh báo được gửi từ Prometheus, thực hiện deduplication, grouping, và routing đến các kênh thông báo phù hợp như Email, Slack, Telegram, PagerDuty. Đây là thành phần quan trọng giúp bạn phản ứng kịp thời với các sự cố.

Chuẩn bị môi trường triển khai

Trước khi bắt đầu, đảm bảo VPS của bạn đáp ứng các yêu cầu tối thiểu:

  • Hệ điều hành: Ubuntu 20.04/22.04 hoặc CentOS 7/8
  • RAM: Tối thiểu 2GB (khuyến nghị 4GB)
  • CPU: 2 cores trở lên
  • Disk: 20GB trống cho time-series data
  • Quyền truy cập: Root hoặc sudo privileges

Cập nhật hệ thống trước khi cài đặt:

sudo apt update && sudo apt upgrade -y

Triển khai Prometheus

Bước 1: Tạo user và thư mục

Tạo user riêng cho Prometheus để tăng cường bảo mật:

sudo useradd --no-create-home --shell /bin/false prometheus

Tạo các thư mục cần thiết:

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

Bước 2: Download và cài đặt Prometheus

Truy cập trang GitHub của Prometheus để lấy phiên bản mới nhất, sau đó download và giải nén:

cd /tmp
wget https://github.com/prometheus/prometheus/releases/download/v2.45.0/prometheus-2.45.0.linux-amd64.tar.gz
tar -xvf prometheus-2.45.0.linux-amd64.tar.gz
cd prometheus-2.45.0.linux-amd64

Copy các file binary và cấu hình:

sudo cp prometheus promtool /usr/local/bin/
sudo cp -r consoles console_libraries /etc/prometheus/

Bước 3: Cấu hình Prometheus

Tạo file cấu hình /etc/prometheus/prometheus.yml với nội dung cơ bản:

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']

Bước 4: Tạo systemd service

Tạo file /etc/systemd/system/prometheus.service để quản lý Prometheus như một service:

Sau khi tạo file service, khởi động Prometheus:

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

Kiểm tra trạng thái: sudo systemctl status prometheus

Cài đặt Node Exporter

Node Exporter là exporter chính thức của Prometheus để thu thập metrics từ hệ thống Linux, bao gồm CPU, memory, disk, network và nhiều metrics khác.

Download và cài đặt tương tự Prometheus:

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

Tạo systemd service cho Node Exporter và khởi động. Sau khi cài đặt, bạn có thể truy cập metrics tại http://your-vps-ip:9100/metrics.

Triển khai Grafana

Cài đặt Grafana

Thêm repository chính thức của Grafana:

sudo apt-get install -y software-properties-common
sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
sudo apt-get update
sudo apt-get install grafana

Khởi động Grafana:

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

Truy cập Grafana tại http://your-vps-ip:3000 với tài khoản mặc định admin/admin.

Kết nối Prometheus với Grafana

Trong Grafana, thực hiện các bước sau:

  1. Vào Configuration → Data Sources
  2. Click Add data source và chọn Prometheus
  3. Nhập URL: http://localhost:9090
  4. Click Save & Test để kiểm tra kết nối

Import Dashboard có sẵn

Grafana có hàng nghìn dashboard được chia sẻ bởi cộng đồng. Để giám sát VPS, bạn có thể import dashboard Node Exporter Full (ID: 1860):

  1. Vào Dashboards → Import
  2. Nhập ID: 1860
  3. Chọn Prometheus data source
  4. Click Import

Dashboard này cung cấp cái nhìn toàn diện về CPU, RAM, Disk, Network, và nhiều metrics khác với giao diện trực quan đẹp mắt.

Cấu hình Alerting với Alertmanager

Cài đặt Alertmanager

Download và cài đặt Alertmanager:

cd /tmp
wget https://github.com/prometheus/alertmanager/releases/download/v0.26.0/alertmanager-0.26.0.linux-amd64.tar.gz
tar -xvf alertmanager-0.26.0.linux-amd64.tar.gz
sudo cp alertmanager-0.26.0.linux-amd64/alertmanager /usr/local/bin/

Cấu hình cảnh báo qua Telegram

Tạo bot Telegram thông qua @BotFather để lấy token, sau đó cấu hình file /etc/alertmanager/alertmanager.yml:

route:
  receiver: 'telegram'
  group_wait: 10s
  group_interval: 10s
  repeat_interval: 1h

receivers:
  - name: 'telegram'
    telegram_configs:
      - bot_token: 'YOUR_BOT_TOKEN'
        chat_id: YOUR_CHAT_ID
        parse_mode: 'HTML'

Tạo Alert Rules

Tạo file /etc/prometheus/alert_rules.yml với các rule cơ bản:

groups:
  - name: system_alerts
    rules:
      - alert: HighCPUUsage
        expr: 100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 80
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "High CPU usage detected"

      - alert: HighMemoryUsage
        expr: (node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes * 100 > 85
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "High memory usage detected"

Cập nhật file prometheus.yml để load alert rules và kết nối với Alertmanager.

Best Practices và Tối ưu hóa

Retention và Storage

Mặc định Prometheus lưu trữ data trong 15 ngày. Điều chỉnh retention phù hợp với nhu cầu:

--storage.tsdb.retention.time=30d

Bảo mật hệ thống

  • Sử dụng reverse proxy (Nginx/Caddy) với HTTPS cho Grafana
  • Cấu hình authentication mạnh mẽ, bật 2FA cho Grafana
  • Hạn chế truy cập Prometheus và Alertmanager bằng firewall
  • Thường xuyên cập nhật các component lên phiên bản mới nhất

Monitoring nhiều VPS

Để giám sát nhiều VPS từ một Prometheus server trung tâm, cài đặt Node Exporter trên mỗi VPS và thêm chúng vào scrape_configs của Prometheus.

Kết luận

Việc triển khai hệ thống monitoring chuyên nghiệp với Grafana, Prometheus và Alertmanager không chỉ giúp bạn tiết kiệm chi phí mà còn mang lại quyền kiểm soát hoàn toàn đối với infrastructure. Với stack này, bạn có thể:

  • Giám sát real-time mọi metrics quan trọng của VPS
  • Nhận cảnh báo tức thì khi có sự cố qua nhiều kênh
  • Phân tích xu hướng và capacity planning hiệu quả
  • Tùy chỉnh dashboard và alert rules theo nhu cầu cụ thể

Hệ thống này có thể mở rộng để giám sát databases, applications, containers, và nhiều services khác thông qua các exporter tương ứng. Đầu tư thời gian để thiết lập đúng cách ngay từ đầu sẽ giúp bạn tránh được nhiều đêm mất ngủ vì sự cố hệ thống trong tương lai.

Bắt đầu monitoring VPS của bạn ngay hôm nay và trải nghiệm sự khác biệt của một hệ thống được giám sát chuyên nghiệp!