Prometheus安裝及教程
??也是一款監(jiān)控軟件,也是一個(gè)時(shí)序數(shù)據(jù)庫(kù)。
??主要用在容器監(jiān)控方面,也可以用于常規(guī)的主機(jī)監(jiān)控。
??使用google公司開(kāi)發(fā)的go語(yǔ)言編寫(xiě)。
??Prometheus是一個(gè)框架,可以與其他組件完美結(jié)合。

部署Prometheus服務(wù)器
??prometheus:192.168.4.10
??被控端node1:192.168.4.11
[root@zzgrhel8 ~]# scp /linux-soft/2/prometheus_soft.tar.gz 192.168.4.10:/root/
[root@prometheus ~]# tar xf prometheus_soft.tar.gz
[root@prometheus ~]# cd prometheus_soft/
# prometheus是經(jīng)過(guò)編譯后的go語(yǔ)言程序,相當(dāng)于綠色軟件,解壓即用
[root@prometheus prometheus_soft]# tar xf prometheus-2.17.2.linux-386.tar.gz
[root@prometheus prometheus_soft]# mv prometheus-2.17.2.linux-386 /usr/local/prometheus
# 修改配置文件
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
?29 ????- targets: ['192.168.4.10:9090']
# 注意:prometheus主機(jī)的時(shí)間需要與真機(jī)時(shí)間一致,如果不一致,修改時(shí)間
[root@prometheus ~]# date -s "20220105 14:18:00"
# 檢查語(yǔ)法
[root@prometheus ~]# /usr/local/prometheus/promtool check config /usr/local/prometheus/prometheus.yml
Checking /usr/local/prometheus/prometheus.yml
??SUCCESS: 0 rule files found
# 創(chuàng)建服務(wù)文件
[root@prometheus ~]# vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus Monitoring System
After=network.target
[Service]
ExecStart=/usr/local/prometheus/prometheus \
??--config.file=/usr/local/prometheus/prometheus.yml \
??--storage.tsdb.path=/usr/local/prometheus/data/
[Install]
WantedBy=multi-user.target
# 啟服務(wù)
[root@prometheus ~]# systemctl daemon-reload
[root@prometheus ~]# systemctl enable prometheus.service --now
[root@prometheus ~]# ss -tlnp | grep :9090
LISTEN ????0 ?????128 ????????:::9090
查看監(jiān)控頁(yè)面
??訪問(wèn)http://192.168.4.10:9090

??被監(jiān)控的對(duì)象稱為targets

prometheus已經(jīng)在監(jiān)控自己了

查看監(jiān)控圖像:

添加需要查看的監(jiān)控項(xiàng):


查看監(jiān)控項(xiàng)的圖形信息:


添加被監(jiān)控端
??監(jiān)控方式:
–?拉取:pull。監(jiān)控端聯(lián)系被監(jiān)控端,采集數(shù)據(jù)
–?推送:push。被監(jiān)控端主動(dòng)把數(shù)據(jù)發(fā)給監(jiān)控端。在prometheus中,push的方式需要額外的組件pushgateway
??被監(jiān)控端根據(jù)自身運(yùn)行的服務(wù),可以運(yùn)行不同的exporter(被監(jiān)控端安裝的、可以與Prometheus通信,實(shí)現(xiàn)數(shù)據(jù)傳遞的軟件)
??exporter列表:https://prometheus.io/docs/instrumenting/exporters/
部署通用的監(jiān)控exporter
??node-exporter用于監(jiān)控硬件和系統(tǒng)的常用指標(biāo)
??exporter運(yùn)行于被監(jiān)控端,以服務(wù)的形式存在。每個(gè)exporter所使用的端口號(hào)都不一樣。
??在node1[192.168.4.11]上部署node exporter
# 部署
[root@prometheus prometheus_soft]# scp node_exporter-1.0.0-rc.0.linux-amd64.tar.gz 192.168.4.11:/root/
[root@node1 ~]# tar xf node_exporter-1.0.0-rc.0.linux-amd64.tar.gz
[root@node1 ~]# mv node_exporter-1.0.0-rc.0.linux-amd64 /usr/local/node_exporter
[root@node1 ~]# vim /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/node_exporter/node_exporter
[Install]
WantedBy=multi-user.target
[root@node1 ~]# systemctl daemon-reload
[root@node1 ~]# systemctl enable node_exporter.service --now
[root@node1 ~]# ss -tlnp | grep :9100
LISTEN ????0 ?????128 ????????:::9100
??在Prometheus服務(wù)器上添加監(jiān)控節(jié)點(diǎn)
# 在配置文件中追加以下內(nèi)容。特別注意縮進(jìn)
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
??- job_name: 'node1'
????static_configs:
????- targets: ['192.168.4.11:9100']
[root@prometheus ~]# systemctl restart prometheus.service
查看結(jié)果:

??查看node1節(jié)點(diǎn)的1分鐘平均負(fù)載


部署grafana
??裝包、啟服務(wù)
[root@prometheus ~]# cd prometheus_soft/
[root@prometheus prometheus_soft]# ls *rpm
grafana-6.7.3-1.x86_64.rpm
[root@prometheus prometheus_soft]# yum install -y grafana-6.7.3-1.x86_64.rpm
[root@prometheus ~]# systemctl enable grafana-server.service --now
??修改配置,對(duì)接prometheus
訪問(wèn)http://192.168.4.10:3000。初始用戶名和密碼都是admin。第一次登陸時(shí),要求改密碼,我的密碼改為tedu.cn。


對(duì)接數(shù)據(jù)的步驟:
添加數(shù)據(jù)源
為數(shù)據(jù)展示選擇展示方式(dashboard儀表盤)
查看結(jié)果




選擇展示方式。導(dǎo)入模板文件,展示不同主題風(fēng)格。




查看結(jié)果:

展示node1的監(jiān)控信息
??grafana模板下載:https://grafana.com/grafana/dashboards/
??導(dǎo)入主機(jī)監(jiān)控模板
# 在真機(jī)上解壓prometheus_soft.tar.gz
[root@zzgrhel8 ~]# tar xf /linux-soft/2/prometheus_soft.tar.gz?





監(jiān)控mariadb數(shù)據(jù)庫(kù)
在node1節(jié)點(diǎn)上安裝數(shù)據(jù)庫(kù)
[root@node1 ~]# cat /etc/yum.repos.d/local.repo
[local_repo]
name=CentOS-$releasever - Base
baseurl=ftp://192.168.4.254/centos-1804
enabled=1
gpgcheck=0
[root@node1 ~]# yum install -y mariadb-server
[root@node1 ~]# systemctl enable mariadb --now
在node1上安裝mysql exporter
??mysql exporter需要訪問(wèn)數(shù)據(jù)庫(kù),所以需要在數(shù)據(jù)庫(kù)中為exporter創(chuàng)建授權(quán)用戶
[root@node1 ~]# mysql
MariaDB [(none)]> grant all on *.* to jerry@'localhost' identified by '123'; ???# 創(chuàng)建用戶jerry,密碼是123
MariaDB [(none)]> exit
??配置mysql exporter
[root@prometheus prometheus_soft]# scp mysqld_exporter-0.12.1.linux-amd64.tar.gz 192.168.4.11:/root/
[root@node1 ~]# tar xf mysqld_exporter-0.12.1.linux-amd64.tar.gz
[root@node1 ~]# mv mysqld_exporter-0.12.1.linux-amd64 /usr/local/mysqld_exporter
# 編寫(xiě)用于連接mysql服務(wù)的配置文件
[root@node1 ~]# vim /usr/local/mysqld_exporter/.my.cnf
[client]
host=127.0.0.1
port=3306
user=jerry
password=123
# 創(chuàng)建service文件
[root@node1 ~]# vim /usr/lib/systemd/system/mysqld_exporter.service
[Unit]
Description=mysqld_exporter
After=network.target
[Service]
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter \
--config.my-cnf=/usr/local/mysqld_exporter/.my.cnf
[Install]
WantedBy=multi-user.target
# 啟服務(wù)
[root@node1 ~]# systemctl daemon-reload
[root@node1 ~]# systemctl enable mysqld_exporter.service --now
[root@node1 ~]# ss -tlnp | grep :9104
LISTEN ????0 ?????128 ????????:::9104
配置prometheus監(jiān)控mysql
# 修改配置文件,追加以下內(nèi)容:
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
??- job_name: 'mysql'
????static_configs:
????- targets: ['192.168.4.11:9104']
[root@prometheus ~]# systemctl restart prometheus.service
查看狀態(tài):

在grafana中展示mysql exporter數(shù)據(jù)





模板切換
查看其模板

