Hướng Dẫn Cài Đặt Prometheus và Grafana Trên Linux

Bài viết này hướng dẫn bạn cách cài PrometheusGrafana trên hệ điều hành Linux (Ubuntu/RockyLinux), phù hợp cho mục đích giám sát server, ứng dụng và hệ thống.


1. Giới thiệu

Prometheus là hệ thống giám sát mã nguồn mở chuyên thu thập dữ liệu dạng time-series. Grafana là công cụ trực quan hóa dữ liệu mạnh mẽ, thường dùng chung với Prometheus để tạo dashboard.


2. Chuẩn Bị

  • 1 server Linux (Ubuntu 20.04+ hoặc RockyLinux 8/9)
  • Quyền root hoặc sudo
  • Kết nối Internet

3. Cài Đặt Prometheus

3.1 Tạo user Prometheus

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

3.2 Tải Prometheus

sudo mkdir -p /etc/prometheus /var/lib/prometheus
cd /tmp
wget https://github.com/prometheus/prometheus/releases/download/v2.54.1/prometheus-2.54.1.linux-amd64.tar.gz
tar -xvf prometheus-2.54.1.linux-amd64.tar.gz

3.3 Cấu hình thư mục

cd prometheus-2.54.1.linux-amd64
sudo cp prometheus promtool /usr/local/bin/
sudo cp -r consoles console_libraries /etc/prometheus/
sudo cp prometheus.yml /etc/prometheus/prometheus.yml
sudo chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus
sudo chown prometheus:prometheus /usr/local/bin/prometheus /usr/local/bin/promtool

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

File: /etc/systemd/system/prometheus.service/etc/prometheus/prometheus.yml

[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 \
  –web.enable-lifecycle
Restart=always
[Install]
WantedBy=multi-user.target

3.5 Khởi động Prometheus

sudo systemctl daemon-reload
sudo systemctl enable –now prometheus
sudo systemctl status prometheus

Prometheus sẽ chạy tại: http://SERVER_IP:9090


4. Cài Đặt Grafana

4.1 Thêm repository

Ubuntu:

sudo apt update
sudo apt install -y software-properties-common
sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
sudo wget -q -O /usr/share/keyrings/grafana.key https://packages.grafana.com/gpg.key
echo "deb [signed-by=/usr/share/keyrings/grafana.key] https://packages.grafana.com/oss/deb stable main" | sudo tee /etc/apt/sources.list.d/grafana.list

RockyLinux:

sudo tee /etc/yum.repos.d/grafana.repo <<‘EOF’
[grafana]
name=Grafana OSS
baseurl=https://packages.grafana.com/oss/rpm
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packages.grafana.com/gpg.key
EOF

4.2 Cài đặt Grafana

sudo yum install -y grafana

4.3 Khởi động Grafana

sudo systemctl enable --now grafana-server
sudo systemctl status grafana-server

Grafana chạy tại: http://SERVER_IP:3000
Đăng nhập mặc định:

  • Username: admin
  • Password: admin

5. Kết nối Grafana với Prometheus

  1. Truy cập Grafana → ConfigurationData Sources
  2. Chọn Add data source
  3. Chọn Prometheus
  4. Nhập URL:
http://localhost:9090
  1. Bấm Save & Test

Như vậy Grafana đã kết nối thành công.


6. Thêm Dashboard

Grafana có nhiều dashboard dựng sẵn trên https://grafana.com/grafana/dashboards/
Các dashboard phổ biến:

  • Node Exporter Full
  • Docker Monitoring
  • MySQL Exporter

Import bằng cách:
Dashboard → Import → Nhập ID hoặc file JSON


7. Kết luận

Bạn đã cài đặt xong PrometheusGrafana trên Linux, cấu hình dữ liệu và có thể bắt đầu giám sát hệ thống. Tiếp theo, bạn có thể cài Node Exporter để giám sát Linux hoặc Windows Exporter cho Windows.

0 0 đánh giá
Article Rating
Theo dõi
Thông báo của
guest
0 Comments
Phản hồi nội tuyến
Xem tất cả bình luận