Cài đặt prometheus trên CentOS7

Last modified by LongNT on 2023/11/30 11:43

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

Chuẩn bị

  • Mô hình đươn giản được sử dụng gồm hai server:
    • Prometheus server: IP: 192.168.70.71. Là server dùng để thu thập, lưu trữ, sử lý các metric từ các target.
    • Node_exporter: IP: 192.168.68.91. Là node target muốn lấy các thông tin, metric cần giám sát

Tắt Firewall và Selinux

Thực hiện tắt Firewall và Selinux trên các node

's/\(^SELINUX=\).*/\SELINUX=disabled/''s/\(^SELINUX=\).*/\SELINUX=disabled/'

sed -i  /etc/sysconfig/selinux
sed -i  /etc/selinux/config
setenforce 0
systemctl stop firewalld
systemctl disable firewalld

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

Bước 1: Chuẩn bị

yum update -y
yum install vim git wget epel-release -y

Bước 2: Tạo service user

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

Bước 3: Cấu hình 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*cd*cd*

 /opt
wget https://github.com/prometheus/prometheus/releases/download/v2.10.0/prometheus-2.10.0.linux-amd64.tar.gz
tar xvf prometheus-.tar.gz 

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

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

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

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

 ..
rm -rf prometheus-

Bước 5: Cấu hình file config

Tạo một file config mới với nội dung như sau:

<<EOF > /etc/prometheus/prometheus.ymlglobal:  scrape_interval: 15sscrape_configs:  - job_name: 'prometheus'    scrape_interval: 5s    static_configs:      - targets: ['localhost:9090']EOF

cat 








Bước 6: Chạy thử Prometheus bằng câu lệnh sau:

sudo -u prometheus /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

Nếu có lỗi thì xem lại file cấu hình. ctrl + c để thoát.

Bước 7: Đặt tiến trình trong systemd để dễ dàng quản lý

<<EOF > /etc/systemd/system/prometheus.service[Unit]Description=PrometheusWants=network-online.targetAfter=network-online.target[Service]User=prometheusGroup=prometheusType=simpleExecStart=/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.targetEOF

cat 

















Khởi động Prometheus:

enable

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

Truy cập vào đường dẫn để sử dụng giao diện web của Prometheus http://192.168.70.71:9090/graph

https://github.com/trangnth/ghichep-prometheus/raw/master/img/2.png

2. Thêm node cần giám sát

Thực hiện cài đặt và cấu hình trên CentOS 7

Bước 1: Tạo user cho prometheus

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

Bước 2: Tải source code

cd*cd

 /opt
wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz

tar xvf node_exporter-0.18.1.linux-amd64.tar.gz

cp node_exporter-0.18.1.linux-amd64/node_exporter /usr/local/bin
chown node_exporter:node_exporter /usr/local/bin/node_exporter

rm -rf node_exporter-0.18.1.linux-amd64
 -

Bước 3: Chạy exporter dưới systemd

<<EOF >  /etc/systemd/system/node_exporter.service[Unit]Description=Node ExporterWants=network-online.targetAfter=network-online.target[Service]User=node_exporterGroup=node_exporterType=simpleExecStart=/usr/local/bin/node_exporter[Install]WantedBy=multi-user.targetEOF

cat 













Khởi động service:

enable

systemctl daemon-reload
systemctl start node_exporter
systemctl  node_exporter

Truy cập vào đường dẫn để thấy các metric thu thập được http://192.168.68.91:9100/metrics

https://github.com/trangnth/ghichep-prometheus/raw/master/img/1.png

Bước 4: Add thêm jobs vào node Prometheus server

Thêm cấu hình của node exporter mới cài đặt vào file /etc/prometheus/prometheus.yml như sau:

'prometheus''localhost:9090''node_exporter''192.168.68.91:9100'

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 
    scrape_interval: 5s
    static_configs:
      - targets: []
  - job_name: 
    scrape_interval: 5s
    static_configs:
      - targets: []


Khởi động lại service:

systemctl restart prometheus

Giờ ta có thể thực hiện query tới các node exporter từ prometheus server.