根據產品需求在自己的系統中添加一個系統監控的頁面,其中有主機信息的顯示,也有一些業務信息的顯示。調研后的方案是 主機信息通過Prometheus采集和存儲,業務信息通過自己系統的調度任務統計后存儲在Mysql中,使用Grafana對接Prometheus和Mysql數據源來制作圖表,然后嵌入到自己系統的頁面中進行展示。
重點解決問題:
1)Prometheus和NodeExporter的安全認證問題
2)Grafana的安全認證問題
1. 部署Prometheus
Prometheus分為服務端和客戶端(xxxExporter),Prometheus采用服務端主動去客戶端拉取數據的方式。本次需求為采集linux服務器主機信息,因此客戶端使用NodeExporter。
1.1 部署NodeExporter
根據我們服務器的情況部署NodeExporter采用Docker和二進制包兩種方式。
1.1.1 Docker部署
考慮到nodeExporter訪問的安全,使用基礎的安全認證方式。
1.1.1.1 配置安全認證
創建文件/opt/node_exporter/web-config.yml,內容:
# Usernames and passwords required to connect.
# Passwords are hashed with bcrypt: https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md#about-bcrypt.
basic_auth_users:{Username}: {Hashed password}
說明:
{Username}: {Hashed password}可以有多組,其中
{Username}是用戶名
{Hashed password}經過hash加密后的密碼,linux下可以使用以下命令對密碼加密:
htpasswd -nBC 10 "" | tr -d ':\n'
示例:
basic_auth_users:exporter-user: $2y$10$7dZ80a5HdLu2cpxjQehXdewVOPqygLlwMbbZUtGpKVxFDz7d8je3o
1.1.1.2 啟動容器
docker run -d \--name node-exporter \--restart=always \--net="host" \--pid="host" \-v "/:/host:ro,rslave" \-v "/opt/node_exporter/web-config.yml:/web-config.yml" \quay.io/prometheus/node-exporter:latest \--path.rootfs=/host --web.config.file=/web-config.yml --web.listen-address=:9100
說明:
--web.listen-address參數可以指定Exporter的端口號,默認為9100。
容器啟動后可以通過docker ps或者訪問http://主機IP:端口號 檢查服務是否啟動成功。
1.1.2?二進制包部署
1.1.2.1 下載文件
下載地址:Download | PrometheusAn open-source monitoring system with a dimensional data model, flexible query language, efficient time series database and modern alerting approach.https://prometheus.io/download/#node_exporter
將下載后文件解壓到/opt目錄下,并改名為node_exporter
tar -xzvf node_exporter-1.7.0.linux-amd64.tar.gz -C /opt
cd /opt
mv node_exporter-1.7.0.linux-amd64 node_exporter
1.1.2.2 配置安全認證
同1.1.1.1
1.1.2.3 啟動服務
nohup /opt/node_exporter/node_exporter --web.listen-address=:9100 --web.config.file=/opt/node_exporter/web-config.yml >/dev/null 2>&1 &
說明:
--web.listen-address參數可以指定Exporter的端口號,默認為9100。
?訪問http://主機IP:端口號 檢查服務是否啟動成功。
1.1.2.4?配置開機自啟
編輯/etc/rc.local文件,新起一行,添加啟動服務的命令,同1.1.2.3
1.2 部署Prometheus Server
本文使用docker-compose方式部署,其他方式類似。
1.2.1 配置安全認證
創建配置目錄:mkdir -p?/opt/prometheus/config
創建文件/opt/prometheus/config/web-config.yml
文件格式同1.1.1.1,但用戶和密碼用途是不一樣的,這個是訪問Prometheus數據源是需要的。
示例:
basic_auth_users:prometheus-user: $2y$10$A.vlBjkqhhI6BTrW2ubXK.rsoSW/gcHhCXNJJlmuRX.LC0k7dj48s
1.2.2 配置服務參數
創建文件/opt/prometheus/config/prometheus.yml
參考文檔:Configuration | PrometheusAn open-source monitoring system with a dimensional data model, flexible query language, efficient time series database and modern alerting approach.https://prometheus.io/docs/prometheus/latest/configuration/configuration/
示例:
global:scrape_interval: 15s # By default, scrape targets every 15 seconds.# Attach these labels to any time series or alerts when communicating with# external systems (federation, remote storage, Alertmanager).external_labels:monitor: 'codelab'# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:- job_name: 'node-monitor'basic_auth:username: 'exporter-user'password: 'password'# Override the global default and scrape targets from this job every 5 seconds.scrape_interval: 5sstatic_configs:- targets: ['x.x.x.1:9100','x.x.x.2:9100']labels:group: 'test'
說明:
password為nodeExporter端配置的明文密碼。
targets 字段為監控的服務器列表,其內容為安裝了node exporter的主機ip及端口號,多個主機逗號分隔。
1.2.3 啟動服務
創建數據目錄:mkdir -p?/opt/prometheus/data
編寫docker-compose.yml文件,內容:
version: '3'
services:prometheus:image: prom/prometheuscontainer_name: prometheusrestart: alwaysuser: '0'ports:- 9090:9090volumes:- /opt/prometheus/config:/etc/prometheus- /opt/prometheus/data:/prometheuscommand: --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/prometheus --web.console.libraries=/usr/share/prometheus/console_libraries --web.console.templates=/usr/share/prometheus/consoles --web.config.file=/etc/prometheus/web-config.yml
說明
如果端口沖突可以通過ports端口映射修改端口
啟動容器
docker-compose up -d
2. 部署Grafana
本文使用docker-compose方式部署,其他方式類似。
2.1 啟動服務
創建數據目錄mkdir -p /opt/grafana/data
編寫docker-compose.yml文件,如果與Prometheus在同一臺機器部署可以共用一個docker-compose文件,內容:
version: '3'
services:grafana:image: grafana/grafana:8.5.27container_name: grafanarestart: alwaysuser: '0'ports:- 3000:3000volumes:- /data/grafana/data:/var/lib/grafanaenvironment:- GF_SECURITY_ALLOW_EMBEDDING=true- GF_SECURITY_ADMIN_USER=admin- GF_SECURITY_ADMIN_PASSWORD=123@test- GF_AUTH_PROXY_ENABLED=true- GF_SERVER_ROOT_URL=http://localhost:8080/grafana/- GF_USERS_DEFAULT_THEME=light
# - GF_SERVER_ROUTER_LOGGING=true
說明:
可以通過環境變量覆蓋grafana.ini中的配置,詳細配置參考:Configuration | Grafana documentationConfiguration documentationhttps://grafana.com/docs/grafana/v8.3/administration/configuration/
GF_SECURITY_ALLOW_EMBEDDING 開啟允許嵌入頁面
GF_SECURITY_ADMIN_USER 設置默認管理員賬號
GF_SECURITY_ADMIN_PASSWORD 設置默認管理員密碼
GF_AUTH_PROXY_ENABLED 開啟代理認證,即在反向代理中進行用戶認證
GF_SERVER_ROOT_URL 通過反向代理的訪問地址
GF_USERS_DEFAULT_THEME 用戶默認主題配置為亮色
GF_SERVER_ROUTER_LOGGING 是否開啟路由日志,調試時使用,默認不開啟
2.2 初始配置
使用默認管理員賬號和密碼登錄Grafana http://主機IP:3000,
根據需要添加Prometheus數據源和Mysql數據源,注意Prometheus數據源要啟用basic auth,
導入或者新建Dashboard,編輯好圖表。
3. 系統集成Grafana
3.1 配置Grafana的反向代理和代理認證
本系統使用了SpringCloudGateway做微服務網關,在Gateway中添加路由配置,示例:
{"id": "grafana","order": 11,"predicates": [{"name": "Path","args": {"_genkey_0": "/grafana/**"}}],"filters": [{"name": "StripPrefix","args": {"_genkey_0": 1}},{"name": "RemoveRequestHeader","args": {"_genkey_0": "Origin"}},{"name": "AddRequestHeader","args": {"_genkey_0": "X-WEBAUTH-USER","_genkey_1": "system-user"}}],"uri": "http://x.x.x.x:3000"
}
說明:
predicates 通過路徑判斷是否需要代理的請求。
filters?StripPrefix?請求轉發給后端時去掉第一段路徑,即請求轉發給后端時路徑沒有/grafana,該參數可與Grafana的配置項server.serve_from_sub_path配合使用。
filters?RemoveRequestHeader 刪除請求頭中的Origin屬性,解決因源請求中Origin值和后端服務器不匹配問題,也可將其改寫成后端服務器地址。
filters?RemoveRequestHeader 請求頭中添加一個屬性X-WEBAUTH-USER,該名稱要與Grafana中的配置匹配,其默認為X-WEBAUTH-USER,其值system-user為通過該代理訪問Grafana所使用的用戶,默認不存在Grafana會自動注冊,詳細可參考Grafana文檔。
uri 為反向代理的后端服務地址,即Grafana服務的地址。
3.2 用戶認證
原系統(a.com)登錄后會將token寫在cookie中,并且在Gateway網關中做了用戶登錄認證,現在Grafana使用反向代理,所有通過訪問http://a.com/grafana/**的請求都會帶上a.com域名所屬的cookie,可以直接利用原來網關的認證邏輯進行用戶認證。
如果原系統沒有在網關中做用戶登錄認證,可以在后端服務中寫一個代理程序,并做用戶認證,可以參考:https://www.cnblogs.com/xiaoqi/p/grafana.html
3.3 前端嵌入Grafana組件
如果想將Dashboard中的某個圖表嵌入到自己系統的頁面中,可以在某Panel上的Share->Embed中獲取到圖表的url,注意使用代理的地址。
其他問題:
1. 在Grafana中自己創建或編輯后保存的Dashboard,保存為json文件,在其他環境中再導入使用時,會報數據源無法找到的問題。
解決辦法,編輯導出的json文件,在其文件的頭部加上數據源的變量定義,具體可參考:Grafana導入 json 文件的 dashboard 錯誤 Templating Failed to upgrade legacy queries Datasource xxx not found_failed to upgrade legacy queries datasource promet-CSDN博客文章瀏覽閱讀6.3k次,點贊5次,收藏11次。從一套環境導出 dashboard 為 json 文件,在另一套環境中導入,如果出現了標題中報錯, 怎么解決這個問題?文章給出了三種解決方案,按各自實際需要選擇_failed to upgrade legacy queries datasource prometheus was not foundhttps://blog.csdn.net/shaochenshuo/article/details/128735528