Hướng Dẫn Cài Grafana Và Prometheus Trên VPS Ubuntu
Hướng dẫn cài đặt Grafana + Prometheus + Node Exporter trên VPS Ubuntu bằng Docker Compose, giúp theo dõi CPU, RAM, ổ cứng, Network, Load Average và tình trạng hoạt động của máy chủ thông qua Dashboard trực quan.
Trong quá trình vận hành VPS, Server, website hoặc các hệ thống dịch vụ, việc theo dõi tài nguyên máy chủ là rất quan trọng. CPU tăng cao, RAM cạn, ổ cứng đầy hoặc Network bất thường đều có thể khiến dịch vụ chậm, treo hoặc gián đoạn nếu không được phát hiện sớm.
Thay vì liên tục đăng nhập SSH và sử dụng các lệnh như top, htop, free -h hay df -h, chúng ta có thể xây dựng một hệ thống giám sát trực quan bằng Grafana + Prometheus + Node Exporter.
Trong bài viết này, VPSTTT sẽ hướng dẫn bạn cài Grafana và Prometheus trên VPS Ubuntu bằng Docker Compose từ A–Z, bao gồm cả Node Exporter để theo dõi CPU, RAM, Disk, Network và các thông số quan trọng của VPS.
Bước 1. Cập nhật hệ thống
Trước tiên cập nhật danh sách Package:
sudo apt updateBước 2. Cài các Package cần thiết
Cài đặt:
sudo apt install -y ca-certificates curlBước 3. Thêm Docker Repository
Đầu tiên tạo thư mục chứa Docker GPG Key:
sudo install -m 0755 -d /etc/apt/keyringsTải GPG Key chính thức của Docker:
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.ascCấp quyền đọc:
sudo chmod a+r /etc/apt/keyrings/docker.ascTiếp theo thêm Docker Repository:
sudo tee /etc/apt/sources.list.d/docker.sources >/dev/null <<EOF
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}")
Components: stable
Architectures: $(dpkg --print-architecture)
Signed-By: /etc/apt/keyrings/docker.asc
EOFBước 4. Cài Docker và Docker Compose
chạy
sudo apt updateCài docker
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginCho Docker tự động chạy khi VPS khởi động:
sudo systemctl enable --now dockerBước 5. Tạo thư mục Grafana và Prometheus
Tạo thư mục Prometheus:
sudo mkdir -p /opt/monitoring/prometheusTạo thư mục Grafana:
sudo mkdir -p /opt/monitoring/grafana/provisioning/datasourcesDi chuyển vào thư mục Monitoring:
cd /opt/monitoringBước 6. Tạo cấu hình Prometheus
sudo tee prometheus/prometheus.yml >/dev/null <<'EOF'
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: prometheus
static_configs:
- targets:
- '127.0.0.1:9090'
- job_name: node
static_configs:
- targets:
- '127.0.0.1:9100'
EOFBước 7. Tạo Data Source Prometheus cho Grafana
Chúng ta sẽ cấu hình để Grafana tự động nhận Prometheus ngay khi khởi động.
sudo tee grafana/provisioning/datasources/datasource.yml >/dev/null <<'EOF'
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
access: proxy
url: http://127.0.0.1:9090
isDefault: true
editable: true
EOFBước 8. Tạo tài khoản quản trị Grafana
Chạy
sudo tee .env >/dev/null <<EOF
GF_SECURITY_ADMIN_USER=admin
GF_SECURITY_ADMIN_PASSWORD=$(openssl rand -base64 24)
EOFGiới hạn quyền truy cập file:
sudo chmod 600 .envBước 9. Tạo Docker Compose
Tạo file:
sudo tee docker-compose.yml >/dev/null <<'EOF'
services:
prometheus:
image: prom/prometheus:latest
container_name: prometheus
network_mode: host
restart: unless-stopped
volumes:
- ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
- prometheus_data:/prometheus
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
- '--web.listen-address=127.0.0.1:9090'
grafana:
image: grafana/grafana:latest
container_name: grafana
network_mode: host
restart: unless-stopped
env_file:
- .env
volumes:
- grafana_data:/var/lib/grafana
- ./grafana/provisioning:/etc/grafana/provisioning
node-exporter:
image: quay.io/prometheus/node-exporter:latest
container_name: node-exporter
network_mode: host
pid: host
restart: unless-stopped
command:
- '--path.rootfs=/host'
- '--web.listen-address=127.0.0.1:9100'
volumes:
- '/:/host:ro,rslave'
volumes:
prometheus_data:
grafana_data:
EOFBước 10. Kiểm tra cấu hình và khởi động các container
Di chuyển vào thư mục Monitoring:
cd /opt/monitoringKiểm tra cú pháp file Docker Compose:
sudo docker compose configKhởi động các container:
sudo docker compose up -dSau đó kiểm tra:
sudo docker compose psBước 11. Đăng nhập Grafana
Trên máy tính của bạn, mở trình duyệt và truy cập: http://IP_VPS:3000
User: admin
Để xem lại Password chạy lệnh:
cd /opt/monitoring
sudo cat .envKết quả:
GF_SECURITY_ADMIN_USER=admin
GF_SECURITY_ADMIN_PASSWORD=xxxxxxxxxxxxxxxx
VPSTTT – Nhà Cung Cấp Giải Pháp Hạ Tầng Số Toàn Diện
VPSTTT là nhà cung cấp giải pháp hạ tầng số tại Việt Nam, phục vụ nhu cầu triển khai, vận hành và lưu trữ dữ liệu của cá nhân, lập trình viên và doanh nghiệp. Hệ sinh thái dịch vụ gồm VPS Việt Nam, VPS quốc tế, VPS Anti DDoS, Hosting cPanel, máy chủ riêng, thuê Rack, Proxy IPv4/IPv6, S3 MinIO và Cloud Storage.
Bên cạnh việc cung cấp tài nguyên hạ tầng, VPSTTT còn tập trung hỗ trợ khách hàng trong quá trình triển khai, cấu hình và xử lý các sự cố thực tế. Nội dung trên Blog VPSTTT được xây dựng từ những tình huống thường gặp khi vận hành VPS, máy chủ và hệ thống trực tuyến, giúp người đọc có thể hiểu nguyên nhân và áp dụng giải pháp rõ ràng hơn.
VPSTTT – Hạ tầng ổn định, giải pháp linh hoạt và đồng hành trong suốt quá trình vận hành.
Thông Tin Liên Hệ Đội Ngũ VPSTTT
Website: https://vpsttt.com
Facebook: https://www.facebook.com/trang.vpsttt/
Zalo OA: https://zalo.me/vpstttgroup
Hotline: 0328 812 674
KẾT LUẬN
Như vậy, VPSTTT đã hướng dẫn bạn hoàn tất quá trình cài đặt Grafana + Prometheus trên VPS Ubuntu. Sau khi cấu hình thành công, bạn có thể theo dõi trực quan các thông số quan trọng của máy chủ như CPU, RAM, dung lượng ổ đĩa, tải hệ thống và trạng thái hoạt động của VPS.
Sự kết hợp giữa Prometheus và Grafana giúp việc giám sát hệ thống trở nên trực quan, dễ quản lý và hỗ trợ phát hiện sớm các vấn đề về tài nguyên hoặc hiệu suất máy chủ.
Để hệ thống monitoring hoạt động ổn định và an toàn, bạn nên thường xuyên cập nhật phần mềm, cấu hình Firewall hợp lý, sử dụng mật khẩu mạnh và thiết lập thêm cảnh báo khi tài nguyên VPS vượt ngưỡng cho phép.
Hy vọng bài viết Hướng Dẫn Cài Grafana + Prometheus Trên VPS Ubuntu của VPSTTT sẽ giúp bạn dễ dàng xây dựng một hệ thống giám sát VPS hiệu quả và chuyên nghiệp.
◉ 165
◉ 168
◉ 180