Prometheus監控平臺搭建

 

01 Prometheus 監控平臺部署
Linux 操作系統監控 + Mysql 監控

1、平臺監控原理:

環境說明:

本文檔中各組件運行的環境爲 Ubuntu(64 位操作系統)

node_exporter 監控的是 192.168.1.26 機器的操作系統

mysql_exporter 監控的是 192.168.1.30 上安裝的 MySQL 數據庫

Grafana+Prometheus 部署在 192.168.1.33 機器

部署前的準備:

關閉所有 Linux 機器的防火牆:systemctl stop firewalld.service

保證所有 Linux 機器的時間是準確的,執行 date 命令檢查;如果不準確,建議使用ntp 同步最新網絡時間

02 部署 Linux 操作系統監控組件
1、下載監控 Linux 的 exporter
https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz

2、將 node_exporter 包上傳到需要被監控的 Linux 服務器上任意目錄下,執行解壓命令

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

 

3、進入到解壓後的文件夾中,執行啓動腳本
node_exporter-0.18.1.linux-amd64
nohup ./node_exporter &

4、查看 nohup 日誌,出現如下日誌,代表啓動成功
tail -100 nohup.out

 

03 部署prometheus
1、下載包

https://github.com/prometheus/prometheus/releases/download/v2.15.2/prometheus-2.15.2.linux-amd64.tar.gz

2、將 prometheus 上傳到一臺單獨的 Linux 機器上,執行解壓操作

ubuntu@VM-0-8-ubuntu:~$ lsapp logs Project Prometheus README.txt tools VueProjectubuntu@VM-0-8-ubuntu:~$ cd Prometheus/ubuntu@VM-0-8-ubuntu:~/Prometheus$ tar -xvf prometheus-2.15.2.linux-amd64.tar.gzubuntu@VM-0-8-ubuntu:~/Prometheus$ lsgrafana_6.5.3_amd64.deb node_exporter-0.18.1.linux-amd64 prometheus-2.15.2.linux-amd64ubuntu@VM-0-8-ubuntu:~/Prometheus$ cd prometheus-2.15.2.linux-amd64/ubuntu@VM-0-8-ubuntu:~/Prometheus/prometheus-2.15.2.linux-amd64$ lsconsole_libraries consoles data LICENSE nohup.out NOTICE prometheus prometheus.yml promtool tsdb
1
3、進入到解壓後的文件夾中,修改配置文件,添加要監控的服務器信息192.168.1.26
vi prometheus.yml

4、在 scrape_configs 配置項下添加 Linux 監控的 job,其中IP 修改爲上面部署 node_exporter 機器的 ip,端口號爲 9100,注意縮進

# my global config

global:

scrape_interval: 2s # Set the scrape interval to every 15 seconds. Default is every 1 minute.

evaluation_interval: 2s # Evaluate rules every 15 seconds. The default is every 1 minute.

# scrape_timeout is set to the global default (10s).

 

# Alertmanager configuration

alerting:

alertmanagers:

- static_configs:

- targets:

# - alertmanager:9093

 

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.

rule_files:

# - "first_rules.yml"

# - "second_rules.yml"

 

# A scrape configuration containing exactly one endpoint to scrape:

# Here it's Prometheus itself.

scrape_configs:

# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.

- job_name: 'prometheus'

 

# metrics_path defaults to '/metrics'

# scheme defaults to 'http'.

 

static_configs:

- targets: ['localhost:9090']

- job_name: 'node'

static_configs:

 

# 新增的服務器配置信息

- job_name: 'node'

static_configs:

- targets: ['192.168.1.26:9100']
5、保存配置文件,啓動 prometheus
nohup ./prometheus &

6、檢查 nohup.out 日誌,如果有以下信息,啓動成功
vi nohup.out
通過反斜槓 ‘/Server is ready to receive’ 回車查詢

 

7、在瀏覽器中訪問部署的 prometheus:http://192.168.1.33:9090

 

8、點擊菜單欄“Status-Targets”,看到頁面中有如下 node 節點,代表 prometheus 和node_exporter 鏈接成功

 

04 部署 Grafana
1、下載安裝grafana(建議使用8.0.6版本,6.5.3有不兼容問題)
sudo apt-get install -y adduser libfontconfig1
wget https://dl.grafana.com/oss/release/grafana_6.5.3_amd64.deb
sudo dpkg -i grafana_6.5.3_amd64.deb

 

2、啓動 grafana
systemctl start grafana-server

3、在瀏覽器訪問:http://192.168.1.33:3000/
輸入用戶名/密碼:admin/admin 登錄

 

4、添加數據源,選擇 prometheus

 

5、進行數據源配置,因爲我的 prometheus 和 grafana 裝在同一臺機器上,所以 ip 寫的是 localhost,如果沒在一臺機器上,寫上 prometheus 的 IP

 

點擊“save and test”,如果提示 success,代表配置成功

6、官網查找監控模板
打開 grafana 官網,查找官網提供的 prometheus 監控模板
https://grafana.com/grafana/dashboards

 

點擊 Linux 服務器監控的中文模板,記錄該模板的 id:8919

 

7、在 grafana 系統頁面中,通過 id 導入該模板,即可在 grafana 中看到 192.168.1.26 機器的性能監控數據

 

 

8、將數據更新頻率很具需要設置,就可以看到設置時間步長內的各項性能指標。包含了 CPU、Load、內存、網絡、磁盤、IO 耗時等指標。監控數據永久保存,可以隨時查看任意時間點內的歷史統計數據,非常方便。

 

9、grafana服務啓停

設置開機自啓:systemctl start grafana-server

啓動服務 sudo service grafana-server start

重啓 sudo service grafana-server restart

停止服務 sudo service grafana-server stop

05 MySQL 的監控
1、下載 MySQL 的 exporter
https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz

2、上傳至 MySQL 服務器上,解壓包

tar xvf mysqld_exporter-0.12.1.linux-amd64.tar.gz

3、執行命令
export DATA_SOURCE_NAME=‘root:admin1234@(192.168.1.30:3306)/’

4、啓動 mysql exporter
進入到解壓後的文件夾中,執行命令
nohup ./mysqld_exporter &
監控 nohup.out 日誌,有如下日誌代表成功

 

5、修改 prometheus 的配置文件 prometheus.yml vi prometheus.yml
增加監控 MySQL 的 job(注意縮進格式)

# my global config

global:

scrape_interval: 2s # Set the scrape interval to every 15 seconds. Default is every 1 minute.

evaluation_interval: 2s # Evaluate rules every 15 seconds. The default is every 1 minute.

# scrape_timeout is set to the global default (10s).

 

# Alertmanager configuration

alerting:

alertmanagers:

- static_configs:

- targets:

# - alertmanager:9093

 

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.

rule_files:

# - "first_rules.yml"

# - "second_rules.yml"

 

# A scrape configuration containing exactly one endpoint to scrape:

# Here it's Prometheus itself.

scrape_configs:

# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.

- job_name: 'prometheus'

 

# metrics_path defaults to '/metrics'

# scheme defaults to 'http'.

 

static_configs:

- targets: ['localhost:9090']

- job_name: 'node'

static_configs:

 

# 新增的服務器配置信息

- job_name: 'node'

static_configs:

- targets: ['192.168.1.26:9100']

 

# 新增的MySQL服務器配置信息

- job_name: 'mysql'

static_configs:

- targets: ['192.168.1.30:9104']

targets 中的 IP 爲 mysql_exporter 所在機器的 IP,端口號固定爲 9104,重啓 prometheus,進入 prometheus 的 UI 界面,在 status-targets 頁面下,可以看到 MySQL exporter 的狀態

6、在 Grafana 中添加 MySQL 的監控模板

在 grafana 官網模板中找到 MySQL 監控模板 Mysql Overview,id 爲 7362,並導入到 Grafana 中(具體操作步驟參考上面導入 Linux 監控模板操作)
————————————————
版權聲明:本文爲CSDN博主「測試界的飄柔」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/m0_67695717/article/details/127768307

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章