提要
本文主要介紹將flink任務(wù)運(yùn)行的metric發(fā)送到Prometheus
監(jiān)控的意義
flink流式任務(wù)在實(shí)時(shí)性穩(wěn)定性方面都有一定的要求,通過(guò)Prometheus 采集flink集群的metric,指定一些指標(biāo)就可以對(duì)其進(jìn)行監(jiān)控告警。從而能夠讓開(kāi)發(fā)人員快速反應(yīng),及時(shí)處理線上問(wèn)題。
1.Prometheus 簡(jiǎn)介
Prometheus是一個(gè)開(kāi)源的監(jiān)控和報(bào)警系統(tǒng)。https://prometheus.io/docs/introduction/overview/
2.1特性
- 多維度的數(shù)據(jù)模型(通過(guò)指標(biāo)名稱(chēng)和標(biāo)簽鍵值對(duì)標(biāo)識(shí))
- 靈活的查詢(xún)語(yǔ)言
- 單機(jī)工作模式,不依賴(lài)于分布式存儲(chǔ)
- 通過(guò)pull模式(HTTP)收集監(jiān)控?cái)?shù)據(jù)
- 通過(guò)使用中間件可以支持push監(jiān)控?cái)?shù)據(jù)到prometheus
- 通過(guò)服務(wù)發(fā)現(xiàn)或者靜態(tài)配置發(fā)現(xiàn)目標(biāo)(監(jiān)控?cái)?shù)據(jù)源)
- 支持多模式的畫(huà)圖和儀表盤(pán)
2.2組件
Prometheus生態(tài)系統(tǒng)包含很多組件(大多是都是可選擇的)
-
Prometheus server(抓取、存儲(chǔ)時(shí)間序列數(shù)據(jù))
-
client libraries(幫助應(yīng)用支持prometheus數(shù)據(jù)采集)
- a push gateway(支持短生命周期的jobs,接收push的監(jiān)控?cái)?shù)據(jù))(prometheus原生支持pull工作模式,為了兼容push工作模式)
- exporters(用于支持開(kāi)源服務(wù)的監(jiān)控?cái)?shù)據(jù)采集,比如:HAProxy、StatsD、Graphite等)(也就是agent)
-
alertmanager(處理警報(bào))
2.3架構(gòu)
下面這張圖展示了prometheus的建構(gòu)和prometheus系統(tǒng)可能需要到的組件:

3 flink集成prometheus
3.1 flink配置
詳細(xì)配置參考
https://ci.apache.org/projects/flink/flink-docs-stable/monitoring/metrics.html#cpu
進(jìn)入flink目錄

拷貝 opt目錄下的flink-metrics-prometheus-1.7.2.jar 到lib目錄。
編輯conf/flink-conf.yml
metrics.reporter.promgateway.class: org.apache.flink.metrics.prometheus.PrometheusPushGatewayReporter
metrics.reporter.promgateway.host: test01.cdh6.local
metrics.reporter.promgateway.port: 9091
metrics.reporter.promgateway.jobName: myJob
metrics.reporter.promgateway.randomJobNameSuffix: true
metrics.reporter.promgateway.deleteOnShutdown: false
3.2 pushgateway安裝
參考 https://github.com/prometheus/pushgateway
訪問(wèn) http://test01.cdh6.local:9091/#

3.3 prometheus安裝
參考 http://www.itdecent.cn/p/3a9ede07d963
本例prometheus和pushgateway安裝到同一機(jī)器上
編寫(xiě) prometheus.yml
scrape_configs:
- job_name: 'pushgateway'
static_configs:
- targets: ['localhost:9091']
labels:
instance: 'pushgateway'
啟動(dòng)
./prometheus --config.file=prometheus.ym l
這個(gè)9091端口就是flink-conf.yml 對(duì)應(yīng)的metrics.reporter.promgateway.port: 9091
flink會(huì)把一些metric push到9091端口上,然后prometheus采集。
4效果
啟動(dòng)flink集群 .bin/start-cluster.sh
訪問(wèn) http://test01.cdh6.local:9090

總結(jié)
flink metric數(shù)據(jù)流轉(zhuǎn)的流程是 flink metric -> pushgateway -> prometheus