【2022-12-04】Prometheus+Grafana監控平臺部署文件
阿新 • • 發佈:2022-12-04
一、環境準備
1. 準備兩臺虛擬機器\或者購買雲伺服器
2. 虛擬機器硬體要求
2.1 記憶體不得少於4G,否則專案啟動會有報錯
2.2 CPU2核2G
2.3 硬碟至少20G
3. 系統映象
centos 7.9
二、部署要求
1. 兩臺虛擬機器分別部署以下元件: 1.1 Grafana+Prometheus 部署在 192.168.44.150 機器 1.2 node_exporter監控的是 192.168.44.151 機器的作業系統 1.3 mysql_exporter監控的是 192.168.44.151 上安裝的 MySQL 資料庫 2. 軟體包連結 連結:https://pan.baidu.com/s/1apnbxZw2ZbofeHFqOttazQ 提取碼:anl8 --來自百度網盤超級會員V3的分享
二、系統時鐘同步
# 安裝ntp,兩臺虛擬機器都需要執行此操作
yum install -y ntp
# 時鐘同步
ntpdate pool.ntp.org
# 檢視當前系統時間
date
# 兩臺虛擬機器必須保證時間一致,因為Prometheus的時序資料庫對時間要求很嚴格,Linux伺服器的日期,時間,時區不一致會導致Grafana中監控不到資料
三、關閉防火牆
# 關閉防火牆 systemctl stop firewalld # 臨時關閉,重啟後開啟 systemctl disable firewalld # 永久關閉 # 檢視防火牆狀態 systemctl status firewalld
四、部署node_exporter元件
# 下載node_exporter監控元件 https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz # 將node_exporter包上傳到需要被監控的Linux伺服器上任意目錄下,執行解壓命令 [root@localhost ~]# ls anaconda-ks.cfg mysqld_exporter-0.12.1.linux-amd64.tar.gz node_exporter-0.18.1.linux-amd64.tar.gz # 我這裡以opt目錄為例 [root@localhost ~]# mv mysqld_exporter-0.12.1.linux-amd64.tar.gz node_exporter-0.18.1.linux-amd64.tar.gz /opt/ [root@localhost ~]# [root@localhost ~]# cd /opt/ [root@localhost opt]# ls containerd mysqld_exporter-0.12.1.linux-amd64.tar.gz node_exporter-0.18.1.linux-amd64.tar.gz [root@localhost opt]# [root@localhost opt]# tar xvf node_exporter-0.18.1.linux-amd64.tar.gz node_exporter-0.18.1.linux-amd64/ node_exporter-0.18.1.linux-amd64/node_exporter node_exporter-0.18.1.linux-amd64/NOTICE node_exporter-0.18.1.linux-amd64/LICENSE [root@localhost opt]# [root@localhost opt]# ls containerd mysqld_exporter-0.12.1.linux-amd64.tar.gz node_exporter-0.18.1.linux-amd64 node_exporter-0.18.1.linux-amd64.tar.gz [root@localhost opt]# cd node_exporter-0.18.1.linux-amd64 # 進入到解壓完的資料夾內 [root@localhost node_exporter-0.18.1.linux-amd64]# ls LICENSE node_exporter NOTICE [root@localhost node_exporter-0.18.1.linux-amd64]# # 執行啟動指令碼 [root@localhost node_exporter-0.18.1.linux-amd64]# nohup ./node_exporter & [1] 1697 [root@localhost node_exporter-0.18.1.linux-amd64]# nohup: ignoring input and appending output to ‘nohup.out’ [root@localhost node_exporter-0.18.1.linux-amd64]# # 檢視nohup日誌,如果出現以下回寫,則安裝成功 [root@localhost node_exporter-0.18.1.linux-amd64]# tail -100 nohup.out time="2022-12-03T04:22:00+08:00" level=info msg="Starting node_exporter (version=0.18.1, branch=HEAD, revision=3db77732e925c08f675d7404a8c46466b2ece83e)" source="node_exporter.go:156" time="2022-12-03T04:22:00+08:00" level=info msg="Build context (go=go1.12.5, user=root@b50852a1acba, date=20190604-16:41:18)" source="node_exporter.go:157" time="2022-12-03T04:22:00+08:00" level=info msg="Enabled collectors:" source="node_exporter.go:97" time="2022-12-03T04:22:00+08:00" level=info msg=" - arp" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - bcache" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - bonding" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - conntrack" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - cpu" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - cpufreq" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - diskstats" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - edac" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - entropy" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - filefd" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - filesystem" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - hwmon" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - infiniband" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - ipvs" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - loadavg" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - mdadm" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - meminfo" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - netclass" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - netdev" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - netstat" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - nfs" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - nfsd" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - pressure" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - sockstat" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - stat" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - textfile" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - time" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - timex" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - uname" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - vmstat" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - xfs" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg=" - zfs" source="node_exporter.go:104" time="2022-12-03T04:22:00+08:00" level=info msg="Listening on :9100" source="node_exporter.go:170" # 在瀏覽器中訪問node_exporter http://192.168.44.151:9100/metrics
五、部署Prometheus
# 下載Prometheus包
https://github.com/prometheus/prometheus/releases/download/v2.15.2/prometheus-2.15.2.linux-amd64.tar.gz
# 將prometheus上傳到一臺單獨的虛擬機器上,執行解壓操作
[root@localhost ~]# ls
anaconda-ks.cfg grafana-6.5.2-1.x86_64.rpm prometheus-2.15.2.linux-amd64.tar.gz
[root@localhost ~]#
[root@localhost ~]# mv grafana-6.5.2-1.x86_64.rpm prometheus-2.15.2.linux-amd64.tar.gz /opt/
[root@localhost ~]#
[root@localhost ~]# cd /opt/
[root@localhost opt]#
[root@localhost opt]# ls
grafana-6.5.2-1.x86_64.rpm prometheus-2.15.2.linux-amd64.tar.gz
[root@localhost opt]#
[root@localhost opt]# tar xvf prometheus-2.15.2.linux-amd64.tar.gz
prometheus-2.15.2.linux-amd64/
prometheus-2.15.2.linux-amd64/promtool
prometheus-2.15.2.linux-amd64/consoles/
prometheus-2.15.2.linux-amd64/consoles/node-cpu.html
prometheus-2.15.2.linux-amd64/consoles/index.html.example
prometheus-2.15.2.linux-amd64/consoles/node-overview.html
prometheus-2.15.2.linux-amd64/consoles/prometheus-overview.html
prometheus-2.15.2.linux-amd64/consoles/node-disk.html
prometheus-2.15.2.linux-amd64/consoles/node.html
prometheus-2.15.2.linux-amd64/consoles/prometheus.html
prometheus-2.15.2.linux-amd64/NOTICE
prometheus-2.15.2.linux-amd64/LICENSE
prometheus-2.15.2.linux-amd64/prometheus.yml
prometheus-2.15.2.linux-amd64/prometheus
prometheus-2.15.2.linux-amd64/tsdb
prometheus-2.15.2.linux-amd64/console_libraries/
prometheus-2.15.2.linux-amd64/console_libraries/menu.lib
prometheus-2.15.2.linux-amd64/console_libraries/prom.lib
[root@localhost opt]#
[root@localhost opt]# ls
grafana-6.5.2-1.x86_64.rpm prometheus-2.15.2.linux-amd64 prometheus-2.15.2.linux-amd64.tar.gz
# 進入到解壓完的資料夾內
[root@localhost opt]# cd prometheus-2.15.2.linux-amd64
[root@localhost prometheus-2.15.2.linux-amd64]# ls
console_libraries consoles LICENSE NOTICE prometheus prometheus.yml promtool tsdb
[root@localhost prometheus-2.15.2.linux-amd64]#
[root@localhost prometheus-2.15.2.linux-amd64]# vi prometheus.yml
[root@localhost prometheus-2.15.2.linux-amd64]#
# 在配置檔案中新增如下內容,注意縮排:
- job_name: 'node'
static_configs:
- targets: ['192.168.44.151:9100'] # 被監控的伺服器ip和埠號
# wq儲存退出
# 執行啟動指令碼
[root@localhost prometheus-2.15.2.linux-amd64]# nohup ./prometheus &
[1] 9963
[root@localhost prometheus-2.15.2.linux-amd64]# nohup: ignoring input and appending output to ‘nohup.out’
[root@localhost prometheus-2.15.2.linux-amd64]#
[root@localhost prometheus-2.15.2.linux-amd64]#
# 檢視nohup日誌,如果出現以下回寫,則安裝成功
[root@localhost prometheus-2.15.2.linux-amd64]# tail -100 nohup.out
level=info ts=2022-12-02T12:28:17.703Z caller=main.go:294 msg="no time or size retention was set so using the default time retention" duration=15d
level=info ts=2022-12-02T12:28:17.703Z caller=main.go:330 msg="Starting Prometheus" version="(version=2.15.2, branch=HEAD, revision=d9613e5c466c6e9de548c4dae1b9aabf9aaf7c57)"
level=info ts=2022-12-02T12:28:17.703Z caller=main.go:331 build_context="(go=go1.13.5, user=root@688433cf4ff7, date=20200106-14:50:51)"
level=info ts=2022-12-02T12:28:17.703Z caller=main.go:332 host_details="(Linux 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64 localhost.localdomain (none))"
level=info ts=2022-12-02T12:28:17.703Z caller=main.go:333 fd_limits="(soft=1024, hard=4096)"
level=info ts=2022-12-02T12:28:17.703Z caller=main.go:334 vm_limits="(soft=unlimited, hard=unlimited)"
level=info ts=2022-12-02T12:28:17.704Z caller=main.go:648 msg="Starting TSDB ..."
level=info ts=2022-12-02T12:28:17.704Z caller=web.go:506 component=web msg="Start listening for connections" address=0.0.0.0:9090
level=info ts=2022-12-02T12:28:17.705Z caller=head.go:584 component=tsdb msg="replaying WAL, this may take awhile"
level=info ts=2022-12-02T12:28:17.706Z caller=head.go:632 component=tsdb msg="WAL segment loaded" segment=0 maxSegment=0
level=info ts=2022-12-02T12:28:17.706Z caller=main.go:663 fs_type=XFS_SUPER_MAGIC
level=info ts=2022-12-02T12:28:17.706Z caller=main.go:664 msg="TSDB started"
level=info ts=2022-12-02T12:28:17.707Z caller=main.go:734 msg="Loading configuration file" filename=prometheus.yml
level=info ts=2022-12-02T12:28:17.708Z caller=main.go:762 msg="Completed loading of configuration file" filename=prometheus.yml
level=info ts=2022-12-02T12:28:17.708Z caller=main.go:617 msg="Server is ready to receive web requests."
# 在瀏覽器中訪問promtheus
http://192.168.44.150:9090/
# 在瀏覽器中訪問promtheus
http://192.168.44.150:9090/targets # 檢視node節點是否被監控
六、部署Grafana
# 由於上面的Grafana版本過低,導致我的監控平臺部分資料未顯示,所以這裡重新下了一個版本
wget https://wget.52liangzy.top/prom/grafana-7.5.4-1.x86_64.rpm
# 如果出現如下報錯,則需要安裝一個wget這個工具
[root@localhost opt]# wget https://wget.52liangzy.top/prom/grafana-7.5.4-1.x86_64.rpm
-bash: wget: command not found
# 安裝wget,重新執行上述命令即可
yum install wget -y
# 安裝Grafana
yum localinstall -y grafana-7.5.4-1.x86_64.rpm
# 啟動Grafana
systemctl start grafana-server
# 在瀏覽器中訪問Grafana,密碼可以修改也可以跳過
http://192.168.44.150:3000/
七、監控Linux主機
# 按照下方給出的截圖,依次新增主機節點,就可以實現資料的實時監控
八、匯入監控模板
# 匯入Grafana官方的監控模板
https://grafana.com/grafana/dashboards