1、安裝Prometheus
1.1 、下載Prometheus
下載網址
https://github.com/prometheus/prometheus/releases
選擇需要的版本
wget https://github.com/prometheus/prometheus/releases/download/v2.53.0/prometheus-2.53.0.linux-amd64.tar.gz
1.2、安裝Prometheus軟件
1.2.1、創建用戶組
groupadd prometheus
useradd -g prometheus -m -s /sbin/nologin prometheus
1.2.2、解壓配置
tar -zxvf prometheus-2.53.0.linux-amd64.tar.gz
mv prometheus-2.53.0.linux-amd64 /usr/local/prometheus
sed -i 's/localhost/ 172.25.0.166/g' /usr/local/prometheus/prometheus.yml
1.2.3、將Prometheus添加至系統環境變量
vim /etc/profile
export PROMETHEUS_HOME=/usr/local/prometheus
PATH=$PATH:$PROMETHEUS_HOME
export PATH
重載系統環境變量文件
source /etc/profile
1.2.4、查看Prometheus的版本信息
prometheus --version
1.2.5、創建數據目錄
mkdir /usr/local/prometheus/data/
1.2.6、權限修改
chown -R prometheus.prometheus /usr/local/prometheus
1.2.7、檢查并加載配置文件
cd /usr/local/prometheus/
./promtool check config prometheus.yml
1.2.8、創建systemd服務
cat <<EOF > /usr/lib/systemd/system/prometheus.service
[Unit]
Description=prometheus
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/usr/local/prometheus/data/ --web.enable-lifecycle --storage.tsdb.retention.time=30d
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
參數注解:
- config.file: 指定配置文件
- web.enable-lifecycle: 支持通過http請求重載配置
- storage.tsdb.path: 指定數據存儲目錄(默認當前目錄的的data目錄,若不存在則新建)
- storage.tsdb.retention.time: 指定數據保留時間(默認15d)
1.2.9、啟動服務
systemctl daemon-reload
systemctl start prometheus
systemctl status prometheus && systemctl enable prometheus
1.2.10、確認端口已經被監聽
ss -lnput | grep 9090
tcp LISTEN 0 512 *:9090 *:* users:(("prometheus",pid=20863,fd=7))
1.2.11、檢查并加載配置文件
http://172.25.0.166:9090/metrics
2、監控Linux主機
2.1 、在Linux主機安裝exporter插件
2.1.1、下載node_exporter
下載網址
https://github.com/prometheus/node_exporter/releases
下載需要的版本
wget https://github.com/prometheus/node_exporter/releases/download/v1.8.1/node_exporter-1.8.1.linux-amd64.tar.gz
2.1.2、創建用戶組
groupadd prometheus
useradd -g prometheus -m -s /sbin/nologin prometheus
2.1.3、安裝并解壓
tar -zxvf node_exporter-1.8.1.linux-amd64.tar.gz -C /usr/local/
cd /usr/local/
mv node_exporter-1.8.1.linux-amd64 node_exporter
2.1.4、權限修改
chown -R prometheus.prometheus /usr/local/node_exporter
2.1.5、將node_exporter執行文件目錄添加至環境變量
vim /etc/profile
export NODE_EXPORTER=/usr/local/node_exporter
export PATH=$PATH:$NODE_EXPORTER
export PATH
重載系統環境變量文件
source /etc/profile
2.1.6、創建systemd服務
cat > /usr/lib/systemd/system/node_exporter.service <<EOF[Unit]
Description=node_export
Documentation=https://github.com/prometheus/node_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
2.1.7、啟動服務
systemctl daemon-reload
systemctl enable node_exporter && systemctl start node_exporter
systemctl status node_exporter
2.1.8、確認端口已經被監聽
ss -lnput | grep 9100
tcp LISTEN 0 512 *:9100 *:* users:(("node_exporter",pid=21904,fd=3))
2.2、將Linux主機的exporter插件添加至Prometheus軟件服務
修改Prometheus配置文件
vim /usr/local/prometheus/prometheus.yml
scrape_configs:
... - job_name: 'Linux Node' # 添加監控項的名字static_configs: - targets: ['172.25.0.166:9100'] # 監控主機的ip地址和端口號
重啟prometheus服務
systemctl restart prometheus
2.3 、測試Prometheus的監控Linux主機狀態
http://172.25.0.166:9090/targets?search=&scrapePool=Linux+Node
http://172.25.0.166:9100/metrics
3、使用Grafana展示數據
3.1、下載grafana
下載網址
https://grafana.com/grafana/download
下載需要的版本
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-11.1.0-1.x86_64.rpm
3.2、安裝grafana
rpm -ivh grafana-enterprise-11.1.0-1.x86_64.rpm
3.3、啟動grafana
systemctl daemon-reload
systemctl enable grafana-server && systemctl start grafana-server
3.4、grafana中文化
vim /etc/grafana/grafana.ini
[users]
# 設置默認語言為中文
default_language = zh-Hans
重啟服務
systemctl restart grafana-server
3.5、下載導入grafana模板
https://grafana.com/grafana/dashboards/15172-node-exporter-for-prometheus-dashboard-based-on-11074/
3.6、加載grafana-piechart-panel插件
grafana-cli plugins install grafana-piechart-panel
或
tar -xvf grafana-piechart-panel.tar.gz
mv grafana-piechart-panel /var/lib/grafana/plugins/grafana-piechart-panel
3.7、瀏覽器訪問
4、參考文獻
https://prometheus.io/
https://blog.csdn.net/heian_99/article/details/126989356
https://www.cnblogs.com/saneri/p/14667301.html
https://blog.csdn.net/qq_31725371/article/details/114697770