Cài đặt Prometheus trên CentOS 7

28/09/2021

Prometheus là một trong số các công cụ dùng để giám sát rất hữu ích hiện nay. Dưới đây sẽ là một loạt các bài hướng dẫn cài đặt cấu hình Prometheus server thu thập metric từ các exporter cơ bản nhất (các thông số trên các node CentOS7, Windows,…), theo dõi tập trung một cách trực quan trên Grafana, cảnh báo đến người dùng qua một số kênh chat như: Gmail, Telegram, Slack.

1. Mô hình

Mô hình tổng quát

Mô hình xây dưng một hệ thống monitor đơn giản với Prometheus

Mô hình lab

IP Planning

2. Cài đặt và cấu hình Prometheus server

Hướng dẫn cài đặt Prometheus server trên CentOS 7 từ source

Bước 1: Cài một số gói cần thiết

yum install wget -y

Bước 2: Tạo một user cho service prometheus

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

Bước 3: Tạo và phân quyền thư mục cấu hình và thư mục lưu trữ cho Prometheus

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

Bước 4: Tải source code prometheus

cd /opt
wget https://github.com/prometheus/prometheus/releases/download/v2.27.1/prometheus-2.27.1.linux-amd64.tar.gz -O prometheus.tar.gz
tar xvf prometheus.tar.gz 
mv prometheus-2.27.1.linux-amd64 prometheus

cp prometheus/prometheus /usr/local/bin/
cp prometheus/promtool /usr/local/bin/

chown prometheus:prometheus /usr/local/bin/prometheus
chown prometheus:prometheus /usr/local/bin/promtool

cp -r prometheus/consoles /etc/prometheus
cp -r prometheus/console_libraries /etc/prometheus

chown -R prometheus:prometheus /etc/prometheus/consoles
chown -R prometheus:prometheus /etc/prometheus/console_libraries

rm -rf prometheus*
cd -

Bước 5: Tạo một file cấu hình để tự scrape metric của chính prometheus server

cat <<EOF > /etc/prometheus/prometheus.yml
global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9090']
EOF

Prometheus sẽ thực hiện scrape metric với chu kỳ là 5s lấy một lần, nếu một job được định nghĩa mà không có scrape_interval thì chu kỳ sẽ được tính theo scrape_interval trong phần global là 15s một lần.

Bước 6: Quản lý service bằng systemd

cat <<EOF > /etc/systemd/system/prometheus.service
[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
EOF

Bước 7: Khởi động Prometheus service:

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

Bước 8: Hoàn tất cài đặt

Truy cập vào giao diện web của Prometheus với link:

http://172.16.72.171:9090/graph

Nếu thành công bạn sẽ thấy giao diện như sau:

Metric của chính Prometheus server có thể truy cập vào http://172.16.72.171:9090/metrics để xem

Về định dạng và chi tiết về các metrics tôi sẽ giới thiệu trong một bài viết sau này.

Như vậy đã cài đặt và cấu hình xong Prometheus server, bài tiếp theo tôi sẽ hướng dẫn các bạn cài đặt và cấu hình Prometheus thu thập metric trên một node CentOS 7.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

What is CentOS 8 Stream

CentOS is one of the best and most popular server operating system. It is basically a free version of Red Hat Enterprise...
29/12/2020

[Cobbler] Tổng quan về PXE – Preboot eXecution Environment

Theo các truyền thống khi bạn muốn cài đặt hệ điều hành cho một thiết bị PC, server nào...
30/12/2020

[Zabbix] Cấu hình Zabbix Server gửi cảnh báo qua gmail

Việc gửi cảnh báo tình trạng của hệ thống là một điều không thể thiếu với mỗi công cụ...
30/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Reliable IPv4 and IPv6 Subnet Rental Services: The Perfect Solution for Global Businesses
23/12/2024

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024