世面上有很多可視化監控的解決方案,Prometheus+Grafana是其中比較成熟的解決方案之一,通過這兩個開源項目,我們可以很容易的搭建出一個強大且美觀的可視化監控系統。
安裝
本次搭建使用了Prometheus 2.22.0版本和Grafana 7.1.0版本,其中Prometheus的安裝方式使用原生安裝方式,Grafana使用docker官方提供的docker image進行安裝。
Prometheus
Prometheus的安裝文件可以從 https://github.com/prometheus/prometheus/releases/ 獲得,根據自己的平台選擇下載即可,我選擇了下載 prometheus-2.22.0.linux-amd64.tar.gz
的版本。
解壓到 /usr/local/
目錄並重命名文件夾為 prometheus
,然后進入到文件目錄:
[root@arch-master prometheus]# pwd
/usr/local/prometheus
[root@arch-master prometheus]# ll
total 161696
drwxr-xr-x 5 3434 3434 4096 Nov 6 11:51 .
drwxr-xr-x 16 root root 4096 Nov 5 18:07 ..
drwxr-xr-x 2 3434 3434 4096 Oct 15 22:21 console_libraries
drwxr-xr-x 2 3434 3434 4096 Oct 15 22:21 consoles
drwxr-xr-x 8 root root 4096 Nov 6 13:00 data
-rw-r--r-- 1 3434 3434 11357 Oct 15 22:21 LICENSE
-rw-r--r-- 1 3434 3434 3420 Oct 15 22:21 NOTICE
-rwxr-xr-x 1 3434 3434 87729971 Oct 15 20:32 prometheus
-rw-r--r-- 1 3434 3434 1329 Nov 6 11:51 prometheus.yml
-rwxr-xr-x 1 3434 3434 77801407 Oct 15 20:34 promtool
編輯 prometheus.yml
,這個文件定義了所有要監控的軟件或者主機,主要是配置 scrape_configs
這組配置,我的配置如下,監控了一個SpringBoot服務,一台Linux主機和一個MySQL數據庫:
# 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']
# Linux監控
- job_name: 'ubuntu'
static_configs:
- targets: ['10.1.18.90:9100']
# MySQL監控
- job_name: 'mysql'
static_configs:
- targets: ['10.1.18.90:9104']
# 需要監控的項目
#項目名
- job_name: 'datamanager'
metrics_path: '/datamanager/actuator/prometheus'
#需監控項目地址
static_configs:
- targets: ['10.1.18.90:8068']
然后指定配置文件並啟動,Prometheus的安裝就完成了,默認端口是9090,這時候訪問 http://127.0.0.1:9090 就可以看到一個簡單的界面。
./prometheus --config.file=prometheus.yml
Grafana
Grafana的安裝更為簡單,我這里直接使用docker-compose進行啟動,以下是docker-compose的配置文件內容:
version: "3"
services:
grafana:
container_name: grafana
image: grafana/grafana:7.1.0
ports:
- "3000:3000"
volumes:
- /etc/localtime:/etc/localtime
restart: unless-stopped
啟動命令:
docker-compose -f grafana.yaml up -d
訪問 http://127.0.0.1:3000 打開Grafana的界面,默認用戶名密碼admin/admin。
部署Exporter
Exporter是Prometheus的一類數據采集組件的總稱。它負責從目標處搜集數據,並將其轉化為Prometheus支持的格式。與傳統的數據采集組件不同的是,它並不向中央服務器發送數據,而是等待中央服務器主動前來抓取。
想要Prometheus采集到想要的數據,就必須部署相應的Exporter,接下來部署三個Exporter。
官方提供了常用軟件的Exporter,我們可以在這里找到:https://github.com/prometheus 。
MySQL
官方提供了MySQL的Exporter:mysqld_exporter ,我們可以直接下載對應系統的版本,解壓到 /usr/local/promethus-export/mysqld_exporter
,文件夾下的內容如下:
zongwei@LAPTOP-ZONGWEI:/usr/local/promethus-export/mysqld_exporter$ ll
total 14480
drwxr-xr-x 1 3434 3434 512 Nov 6 10:33 ./
drwxr-xr-x 1 root root 512 Nov 6 11:48 ../
-rw-r--r-- 1 3434 3434 11325 Jul 29 2019 LICENSE
-rw-r--r-- 1 3434 3434 65 Jul 29 2019 NOTICE
-rw-r--r-- 1 root root 60 Nov 6 10:33 my.cnf
-rwxr-xr-x 1 3434 3434 14813452 Jul 29 2019 mysqld_exporter*
編輯(或者新建)my.cnf,配置mysql的連接參數:
[client]
host=127.0.0.1
port=3306
user=root
password=123456
然后啟動,如果沒有異常輸出,那就是說明啟動成功了,默認的9104,可以通過 http://127.0.0.1:9104 訪問,驗證是否正常啟動:
./mysqld_exporter --config.my-cnf=my.cnf
Oracle
官方沒有Oracle的Exporter,Github上有一個活躍的第三方項目可以滿足需求:oracledb_exporter 。
這里推薦使用Docker的方式部署,可以避免很多環境上的問題。
docker run -d --name oracledb_exporter --link=oracle -p 9161:9161 -e DATA_SOURCE_NAME=system/oracle@oracle/xe iamseth/oracledb_exporter
Prometheus的配置:
- job_name: oracle
static_configs:
- targets: ['10.20.31.129:9161']
labels:
instance: oracle-129
Grafana Labs上漢化的Oracle Dashboard不多,不過也是有一個效果不錯的Dashboard,Oracledb監控-性能與表空間 。
效果如下:
Linux
監控遠程Linux主機使用的Exporter是node_exporter 組件,同樣選擇對應的版本下載,解壓到 /usr/local/promethus-export/node_exporter
,node_exporter無需配置,直接啟動即可:
./node_exporter
啟動完成后,訪問 http://127.0.0.1:9100 驗證是否正確啟動。
SpringBoot2
本次部署使用的是SpringBoot 2.3.4.RELEASE,對Prometheus的支持非常好,首先引用maven依賴:
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
添加application.yaml
# actuator相關配置
management:
endpoints:
web:
exposure:
include: "*"
base-path: /actuator
endpoint:
prometheus:
enabled: true
metrics:
export:
prometheus:
enabled: true
step: 1ms
descriptions: true
配置自定義bean
@Bean
public MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName) {
return (registry) -> registry.config().commonTags("application", applicationName);
}
完成這三步后啟動Application,就可以使用actuator接口查看到Prometheus收集的數據:http://127.0.0.1:8080/actuator/prometheus 。
最終,訪問Prometheus的target,就能看到我們剛剛部署的這三個exporter:
配置Grafana
Prometheus的查看界面過於簡單,也很難做到一目了然,配合Grafana的強大前端表現力和豐富的dashboard,我們可以很輕松的配置出美觀的界面。
配置數據源
首先添加一個數據源,將Grafana和Prometheus關聯起來。選擇Configuration的Data Sources,點擊Add data source,選擇Prometheus,在URL一欄填寫Prometheus的訪問地址,然后保存即可。
添加Dashboard
數據源配置成功后,選擇Create->Import,在此界面上傳或配置Dashboard,在https://github.com/percona/grafana-dashboards 這個項目,我們可以找到一些成熟的Dashboard配置,對於Linux監控可以選擇 System_Overview.json
,對於MySQL可以選擇 MySQL_Overview.json
,將這兩個文件下載,依次選擇 【Upload JSON file】按鈕進行上傳。
注意:上傳之前請先將所有的pmm-singlestat-panel替換為singlestat,否則會報panel未定義,無法正常顯示。
除了這個開源項目之外,Grafana還提供了官方的Dashboard市場:https://grafana.com/grafana/dashboards 。我們可以在這里找到各種數據源各種收集器的Dashboard配置。
對於Springboot2,我選擇了 https://grafana.com/grafana/dashboards/10280 作為配置,將10280填入第一個輸入框,然后點擊【load】。
至此,所有的配置都已完成,最后來看看SpringBoot的監控效果圖。
參考
Spring Boot 2.1X+Prometheus +Grafana實現監控及可視化