背景:
由于項(xiàng)目上使用spring
cloud,需要對一些服務(wù)指標(biāo)就需要進(jìn)行監(jiān)控,以便于時刻了解各服務(wù)的運(yùn)行狀態(tài)。經(jīng)過搜索材料,發(fā)現(xiàn)大多方案直接推薦用微服務(wù)最佳實(shí)踐者——Netflix開源的方案(Atlas+Grafana),試著跟著搜索到的一些資料,并進(jìn)行了嘗試,結(jié)果表明成功案例都是在SpringBoot1.x上的;在SpringBoot2.x上,尚無資料表示成功使用上Atlas這方案。
另外,在研究的過程中,發(fā)現(xiàn)SpringBoot2.x上已引入第三方實(shí)現(xiàn)的metrics Facade(micrometer.io
<https://yq.aliyun.com/go/articleRenderRedirect?url=http%3A%2F%2Fmicrometer.io%2F>
,可以同日志的Facade框架Sl4j等同理解,并已覆蓋Atlas、Datadog、Ganglia、Graphite、Influx、JMX、NewRelic、Prometheus、SignalFx、StatsD、Wavefront等業(yè)內(nèi)主流的tsdb實(shí)現(xiàn))。再者,項(xiàng)目最終需要應(yīng)用kubernetes來進(jìn)行容器編排,其官方推薦的容器監(jiān)控服務(wù),就是用
Prometheus配上Grafana作為監(jiān) 控展現(xiàn);為了統(tǒng)一,就轉(zhuǎn)身到Prometheus+Grafana這方案上來。
0. 環(huán)境說明:
Ubuntu 16.04
Spring Boot 2.0.0.RELEASE
初識Prometheus:
Prometheus
<https://yq.aliyun.com/go/articleRenderRedirect?url=https%3A%2F%2Fprometheus.io%2F>
是由 SoundCloud 開源監(jiān)控告警解決方案,從 2012 年開始編寫代碼,再到 2015 年 github 上開源以來,已經(jīng)吸引了 9k+
關(guān)注,以及很多大公司的使用;2016 年 Prometheus 成為繼 k8s 后,第二名 CNCF(Cloud Native Computing
Foundation
<https://yq.aliyun.com/go/articleRenderRedirect?url=https%3A%2F%2Fcncf.io%2F>)
成員。
作為新一代開源解決方案,很多理念與 Google SRE 運(yùn)維之道不謀而合。
主要功能
* 多維 數(shù)據(jù)模型
<https://yq.aliyun.com/go/articleRenderRedirect?url=https%3A%2F%2Fprometheus.io%2Fdocs%2Fconcepts%2Fdata_model%2F>
(時序由 metric 名字和 k/v 的 labels 構(gòu)成)。
* 靈活的查詢語句(PromQL
<https://yq.aliyun.com/go/articleRenderRedirect?url=https%3A%2F%2Fprometheus.io%2Fdocs%2Fquerying%2Fbasics%2F>
)。
* 無依賴存儲,支持 local 和 remote 不同模型。
* 采用 http 協(xié)議,使用 pull 模式,拉取數(shù)據(jù),簡單易懂。
* 監(jiān)控目標(biāo),可以采用服務(wù)發(fā)現(xiàn)或靜態(tài)配置的方式。
* 支持多種統(tǒng)計(jì)數(shù)據(jù)模型,圖形化友好。
核心組件
* Prometheus Server
<https://yq.aliyun.com/go/articleRenderRedirect?url=https%3A%2F%2Fgithub.com%2Fprometheus%2Fprometheus>
, 主要用于抓取數(shù)據(jù)和存儲時序數(shù)據(jù),另外還提供查詢和 Alert Rule 配置管理。
* client libraries
<https://yq.aliyun.com/go/articleRenderRedirect?url=https%3A%2F%2Fprometheus.io%2Fdocs%2Finstrumenting%2Fclientlibs%2F>
,用于對接 Prometheus Server, 可以查詢和上報(bào)數(shù)據(jù)。
* push gateway
<https://yq.aliyun.com/go/articleRenderRedirect?url=https%3A%2F%2Fgithub.com%2Fprometheus%2Fpushgateway>
,用于批量,短期的監(jiān)控?cái)?shù)據(jù)的匯總節(jié)點(diǎn),主要用于業(yè)務(wù)數(shù)據(jù)匯報(bào)等。
* 各種匯報(bào)數(shù)據(jù)的 exporters
<https://yq.aliyun.com/go/articleRenderRedirect?url=https%3A%2F%2Fprometheus.io%2Fdocs%2Finstrumenting%2Fexporters%2F>
,例如匯報(bào)機(jī)器數(shù)據(jù)的 node_exporter, 匯報(bào) MongoDB 信息的MongoDB exporter
<https://yq.aliyun.com/go/articleRenderRedirect?url=https%3A%2F%2Fgithub.com%2Fdcu%2Fmongodb_exporter>
等等。
* 用于告警通知管理的 alertmanager
<https://yq.aliyun.com/go/articleRenderRedirect?url=https%3A%2F%2Fgithub.com%2Fprometheus%2Falertmanager>
。
基礎(chǔ)架構(gòu)
從這個架構(gòu)圖,也可以看出 Prometheus 的主要模塊包含, Server, Exporters, Pushgateway, PromQL,
Alertmanager, WebUI 等。
大致使用邏輯是這樣:
* Prometheus server 定期從靜態(tài)配置的 targets 或者服務(wù)發(fā)現(xiàn)的 targets 拉取數(shù)據(jù)。
* 當(dāng)新拉取的數(shù)據(jù)大于配置內(nèi)存緩存區(qū)的時候,Prometheus 會將數(shù)據(jù)持久化到磁盤(如果使用 remote storage 將持久化到云端)。
* Prometheus 可以配置 rules,然后定時查詢數(shù)據(jù),當(dāng)條件觸發(fā)的時候,會將 alert 推送到配置的 Alertmanager。
* Alertmanager 收到警告的時候,可以根據(jù)配置,聚合,去重,降噪,最后發(fā)送警告。
* 可以使用 API, Prometheus Console 或者 Grafana 查詢和聚合數(shù)據(jù)。
注意
* Prometheus 的數(shù)據(jù)是基于時序的 float64 的值,如果你的數(shù)據(jù)值有更多類型,無法滿足。
* Prometheus
不適合做審計(jì)計(jì)費(fèi),因?yàn)樗臄?shù)據(jù)是按一定時間采集的,關(guān)注的更多是系統(tǒng)的運(yùn)行瞬時狀態(tài)以及趨勢,即使有少量數(shù)據(jù)沒有采集也能容忍,但是審計(jì)計(jì)費(fèi)需要記錄每個請求,并且數(shù)據(jù)長期存儲,這個
Prometheus 無法滿足,可能需要采用專門的審計(jì)系統(tǒng)。
了解更多知識,可以到這里了解:Prometheus實(shí)戰(zhàn)
<https://yq.aliyun.com/go/articleRenderRedirect?url=https%3A%2F%2Fsongjiayang.gitbooks.io%2Fprometheus%2F>
Spring cloud應(yīng)用的配置改動:
* pom.xml 加入相關(guān)包依賴依賴 : <dependency> <groupId>io.micrometer</groupId> <
artifactId>micrometer-core</artifactId> <version>1.0.5</version> </dependency> <
dependency> <groupId>io.micrometer</groupId> <artifactId>
micrometer-registry-prometheus</artifactId> <version>1.0.5</version> <exclusions
> <exclusion> <!-- 由于 micrometer-registry-prometheus 默認(rèn)的core包是
1.0.1版本的,與當(dāng)前的組件版本不適應(yīng),需要排除 --> <groupId>io.micrometer</groupId> <artifactId>
micrometer-core</artifactId> </exclusion> </exclusions> </dependency>
* 修改application.yml: #服務(wù)治理配置 management: security: # 僅限于 開發(fā)環(huán)境可對security進(jìn)行關(guān)閉。
enabled:false metrics: export: prometheus: enabled: true step: 1m
descriptions:true web: server: auto-time-requests: true endpoints: web:
exposure: include:
health,info,env,prometheus,metrics,httptrace,threaddump,heapdump
這里為了方便部署監(jiān)控測試,把服務(wù)治理端的相關(guān)安全認(rèn)證選項(xiàng)禁用掉。更多關(guān)于metrics的配置說明,請參考官方指引
<https://yq.aliyun.com/go/articleRenderRedirect?url=http%3A%2F%2Fmicrometer.io%2Fdocs>
。
關(guān)于endpoints,"prometheus"在本例中,是必須聲明的項(xiàng)目,否則在后續(xù)配置prometheus的job時沒有對應(yīng)的uri可以提供。
由于prometheus是采用主動爬取的方式,所以在SpringCloud的應(yīng)用里面,無需配置prometheus的服務(wù)地址和端口。這點(diǎn)(個人理解),是與Atlas、Ganglia、Graphite、Influx、JMX、StatsD、Wavefront等不同的。
接著啟動你的應(yīng)用,打開瀏覽器輸入http://localhost:PORT/actuator/prometheus
<https://yq.aliyun.com/go/articleRenderRedirect?url=https%3A%2F%2Flink.jianshu.com%2F%3Ft%3Dhttp%3A%2F%2Flocalhost%3A2222%2Fauth%2Fprometheus>
,正常情況下你就可以在頁面上看到很多鍵值對(直接返回的是text):
Prometheus配置:
* 安裝:
去官網(wǎng)
<https://yq.aliyun.com/go/articleRenderRedirect?url=https%3A%2F%2Fprometheus.io%2Fdownload%2F>
下載,根據(jù)你自己的操作系統(tǒng)選擇版本,本例下載的是prometheus-2.3.1.linux-amd64.tar.gz。下載完,解壓,找到
prometheus.yml改配置: # my global config global: scrape_interval: 15s # Set the
scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval:15s # 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'] # The job name is
added as a label `job=<job_name>` to any timeseries scraped from this config. -
job_name:'admin-service' # metrics_path defaults to '/metrics' # metrics_path:
/actuator/metrics metrics_path: /actuator/prometheus # scheme defaults to
'http'. static_configs: - targets: ['localhost:8762']
說明:下載下來的配置文件,metrics_path是被注釋的。在本例使用的是SpringBoot
2.0.0.RELEASE,默認(rèn)監(jiān)控類指標(biāo)輸出,都是在/actuator下。
所以,需要修改為”metrics_path: /actuator/prometheus“,且非注釋。
* 啟動Prometheus:
啟動終端,進(jìn)入到Prometheus的目錄中,運(yùn)行prometheus -config.file=prometheus.yml,啟動成功后,在
http://localhost:9090/targets
<https://yq.aliyun.com/go/articleRenderRedirect?url=https%3A%2F%2Flink.jianshu.com%2F%3Ft%3Dhttp%3A%2F%2Flocalhost%3A9090%2Ftargets>
你會看到你所監(jiān)控的目標(biāo):
要看有哪些指標(biāo)可以現(xiàn)成使用的,可以在Graph菜單下,那個下拉列表中查找:
你可以選中一個指標(biāo),然后execute,就可以看到一個簡陋的圖表,顯示指標(biāo)的數(shù)據(jù):
增強(qiáng)配置:
考慮到后續(xù)需要對同一個服務(wù)增加不用業(yè)務(wù)類型的監(jiān)控,或者直接對同一個job應(yīng)用到不同的host上時,采用static_configs的方式,是需要重啟Prometheus服務(wù)的。
因此,通過研究,可以通過 file_sd_configs來替代static_configs:
(1). 注釋 修改prometheus.yml文件:
#static_configs: #- targets: ['localhost:8762'] file_sd_configs: - files: -
/YOUR_HOME/softs/prometheus/configs/admin/*.json
(2).
在/YOUR_HOME/softs/prometheus/configs/admin/目錄里,創(chuàng)建一個任意名字的json文件。本例是創(chuàng)建base.json文件。內(nèi)容如下:
[ { "targets": ["localhost:8762"] } ]
更多配置說明,請參考官方說明:file_sd_config
<https://yq.aliyun.com/go/articleRenderRedirect?url=https%3A%2F%2Fprometheus.io%2Fdocs%2Fprometheus%2Flatest%2Fconfiguration%2Fconfiguration%2F%23%253Cfile_sd_config%253E>
更好的監(jiān)控UI——Grafana:
*
安裝:
同樣的進(jìn)入官網(wǎng)
<https://yq.aliyun.com/go/articleRenderRedirect?url=https%3A%2F%2Fgrafana.com%2Fgrafana%2Fdownload>
下載,請根據(jù)你的操作系統(tǒng)下載。本例下載的版本是grafana_5.1.3_amd64.deb ,需要使用dpkg命令進(jìn)行安裝。安裝過程如下:
1. 非root用戶 $ sudo dpkg -i grafana_5.1.3_amd64.deb
2. 啟動服務(wù): $ sudo systemctl start grafana-server
3. 訪問http://localhost:3000你就會看到登錄界面,默認(rèn)的賬戶和密碼都是admin。
*
配置:
add datasource ,加上Prometheus的數(shù)據(jù)源配置。
接著我們新加入一個Dashboards:
新增一個Graph:
接著選擇edit:
然后在metric中編輯數(shù)據(jù)源和指標(biāo),比如我們新增一個threads(線程數(shù))的指標(biāo),完成后就可以看到美麗的圖表了
本文轉(zhuǎn)自掘金-Prometheus+Grafana實(shí)現(xiàn)SpringCloud服務(wù)監(jiān)控
<https://yq.aliyun.com/go/articleRenderRedirect?url=https%3A%2F%2Fmy.oschina.net%2Fwerngin%2Fblog%2F1835239>
熱門工具 換一換