Đầu tiên mình triển khai Prometheus trên Ubuntu 20.04.
1. Cài đặt Prometheus
Bước 1: Tạo user riêng cho Prometheus
useradd --no-create-home --shell /bin/false prometheus
Giải thích:
- Tải gói bằng dòng lệnh:
curl -LO https://github.com/prometheus/prometheus/releases/download/v2.52.0/prometheus-2.52.0.linux-amd64.tar.gz
- Giải nén gói: tar -xvf prometheus-2.52.0.linux-amd64.tar.gz
- Di chuyển vào thư mục: cd prometheus-2.52.0.linux-amd64
- Copy 2 file thực thi:
prometheus là chương trình chính, promtool dùng để kiểm tra file cấu hình.
Bước 3: Tạo thư mục cấu hình và dữ liệu
mkdir -p /etc/prometheus /var/lib/prometheus
- Giải thích:
nano /etc/systemd/system/prometheus.service
Thêm vào file với nội dung sau:
[Unit]
Description=Prometheus
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
[Install]
WantedBy=multi-user.target
Giải thích:
Mình khởi động dịch vụ bằng những câu lệnh sau
sudo systemctl daemon-reload
sudo systemctl enable prometheus
sudo systemctl start Prometheus
sudo systemctl status Prometheus

Để lên được web Prometheus, mình cần phải mở port trên tường lửa.
Dùng lệnh:
uwf allow 9090
uwf enable

Truy cập: http://<IP-của-server>:9090 và ra được như hình bên dưới là đã cài đặt Prometheus thành công.
2. Cài đặt Node Exporter để giám sát Server
Bước 1: Tạo user riêng
useradd --no-create-home --shell /bin/false node_exporter
Tương tự Prometheus – bảo mật tốt hơn khi mỗi dịch vụ dùng một user riêng.
Bước 2: Cài Node Exporter
- Tải gói: curl -LO https://github.com/prometheus/node_exporter/releases/download/v1.8.1/node_exporter-1.8.1.linux-amd64.tar.gz
- Giải nén gói
tar -xvf node_exporter-*.tar.gz
p node_exporter /usr/local/bin/
Bước 3: Tạo file dịch vụ Node Exporter
Tương tự Prometheus – cấu hình chạy tự động bằng systemd.
nano /etc/systemd/system/node_exporter.service
Thêm vào file 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=default.target
Bước 4: Khởi động dịch vụ
sudo systemctl daemon-reload
sudo systemctl enable node_exporter
sudo systemctl start node_exporter
sudo systemctl status node_exporter
Node Exporter sẽ mở port 9100 và bắt đầu phục vụ dữ liệu metrics tại.
Vì vậy cần mở port 9110 trên tường lửa.
Bước 5: Cấu hình file prometheus.yml để thu thập dữ liệu từ node_exporter
nano /etc/prometheus/prometheus.yml
Thêm vào file với nội dung sau: ( cần điền đúng định dạng file thì mới có thể thu thập được)
scrape_configs:
- job_name: 'node_exporter'
static_configs:
- targets: ['localhost:9100']
Giải thích:
systemctl restart prometheus
3. Kiểm tra hệ thống
Truy cập trình duyệt: http://<IP-của-server>:9090
4. Mẹo nhỏ cho SysAdmin / DevOps
1. Cài đặt Prometheus
Bước 1: Tạo user riêng cho Prometheus
useradd --no-create-home --shell /bin/false prometheus
Giải thích:
- Tạo một user chuyên biệt (prometheus) để chạy dịch vụ, tăng tính bảo mật.
- --no-create-home: Không tạo thư mục home
- --shell /bin/false: Không cho phép user này đăng nhập shell
- Tải gói bằng dòng lệnh:
curl -LO https://github.com/prometheus/prometheus/releases/download/v2.52.0/prometheus-2.52.0.linux-amd64.tar.gz
- Giải nén gói: tar -xvf prometheus-2.52.0.linux-amd64.tar.gz
- Di chuyển vào thư mục: cd prometheus-2.52.0.linux-amd64
- Copy 2 file thực thi:
- cp prometheus /usr/local/bin/
- cp promtool /usr/local/bin/
prometheus là chương trình chính, promtool dùng để kiểm tra file cấu hình.
Bước 3: Tạo thư mục cấu hình và dữ liệu
mkdir -p /etc/prometheus /var/lib/prometheus
- Giải thích:
- /etc/prometheus: chứa file cấu hình như prometheus.yml
- /var/lib/prometheus: nơi lưu dữ liệu time-series do Prometheus thu thập
nano /etc/systemd/system/prometheus.service
Thêm vào file với nội dung sau:
[Unit]
Description=Prometheus
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
[Install]
WantedBy=multi-user.target
Giải thích:
- Cấu hình Prometheus chạy như một dịch vụ hệ thống (systemd).
- ExecStart: chỉ định file chạy chính và các tham số cần thiết.
- User, Group: chỉ định Prometheus chạy bằng user đã tạo.
Mình khởi động dịch vụ bằng những câu lệnh sau
sudo systemctl daemon-reload
sudo systemctl enable prometheus
sudo systemctl start Prometheus
sudo systemctl status Prometheus
Để lên được web Prometheus, mình cần phải mở port trên tường lửa.
Dùng lệnh:
uwf allow 9090
uwf enable
Truy cập: http://<IP-của-server>:9090 và ra được như hình bên dưới là đã cài đặt Prometheus thành công.
2. Cài đặt Node Exporter để giám sát Server
Bước 1: Tạo user riêng
useradd --no-create-home --shell /bin/false node_exporter
Tương tự Prometheus – bảo mật tốt hơn khi mỗi dịch vụ dùng một user riêng.
Bước 2: Cài Node Exporter
- Tải gói: curl -LO https://github.com/prometheus/node_exporter/releases/download/v1.8.1/node_exporter-1.8.1.linux-amd64.tar.gz
- Giải nén gói
tar -xvf node_exporter-*.tar.gz
p node_exporter /usr/local/bin/
Bước 3: Tạo file dịch vụ Node Exporter
Tương tự Prometheus – cấu hình chạy tự động bằng systemd.
nano /etc/systemd/system/node_exporter.service
Thêm vào file 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=default.target
Bước 4: Khởi động dịch vụ
sudo systemctl daemon-reload
sudo systemctl enable node_exporter
sudo systemctl start node_exporter
sudo systemctl status node_exporter
Node Exporter sẽ mở port 9100 và bắt đầu phục vụ dữ liệu metrics tại.
Vì vậy cần mở port 9110 trên tường lửa.
Bước 5: Cấu hình file prometheus.yml để thu thập dữ liệu từ node_exporter
nano /etc/prometheus/prometheus.yml
Thêm vào file với nội dung sau: ( cần điền đúng định dạng file thì mới có thể thu thập được)
scrape_configs:
- job_name: 'node_exporter'
static_configs:
- targets: ['localhost:9100']
Giải thích:
- scrape_configs: cấu hình Prometheus đi thu thập dữ liệu (scrape job)
- job_name: tên job, có thể tùy ý đặt
- targets: danh sách IP:port hoặc hostname:port của exporter
systemctl restart prometheus
3. Kiểm tra hệ thống
Truy cập trình duyệt: http://<IP-của-server>:9090
- Kiểm tra exporter:
- Vào Status > Targets
- Xem các exporter có trạng thái UP không
4. Mẹo nhỏ cho SysAdmin / DevOps
Prometheus không khởi động | Kiểm tra log: journalctl -u prometheus |
Thay đổi file .yml không áp dụng | Phải restart Prometheus sau mỗi lần sửa |
Bị lỗi YAML | Kiểm tra tại: https://www.yamllint.com/ |
Exporter không hiển thị | Kiểm tra port, firewall, và thử curl trực tiếp |