整体架构图如下:
本文主要介绍将flink任务运行的metric发送到Prometheus,通过grafana报表工具展示。
flink流式任务在实时性稳定性方面都有一定的要求,通过Prometheus 采集flink集群的metric,指定一些指标就可以对其进行监控告警。从而能够让开发人员快速反应,及时处理线上问题。
Prometheus是一个开源的监控和报警系统。
https://prometheus.io/docs/introduction/overview/
Prometheus生态系统包含很多组件(大多是都是可选择的)
下面这张图展示了prometheus的建构和prometheus系统可能需要到的组件:
详细配置参考
https://ci.apache.org/projects/flink/flink-docs-stable/monitoring/metrics.html#cpu
进入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
参考 https://github.com/prometheus/pushgateway
下载:https://prometheus.io/download/ wget https://github.com/prometheus/pushgateway/releases/download/v0.9.1/pushgateway-0.9.1.linux-amd64.tar.gz
解压:tar -zxvf pushgateway-0.9.1.linux-amd64.tar.gz
启动:./pushgateway &
访问 http://localhost:9091/#
下载:https://prometheus.io/download/ wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz
解压:tar -zxvf pushgateway-0.9.1.linux-amd64.tar.gz
启动:./node_exporter &
查看进程 netstat -apn | grep -E '9091|3000|9090|9100'
访问url:http://192.168.91.132:9100/metrics
效果如下:
这些都是收集到数据,有了它就可以做数据展示了
下载:
wget https://github.com/prometheus/prometheus/releases/download/v2.12.0/prometheus-2.12.0.linux-amd64.tar.gz
本例prometheus和pushgateway安装到同一机器上
编写prometheus.yml,注意:严格按照.yml文件的编写格式,每行要缩进,否则启动报错!
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
labels:
instance: 'prometheus'
- job_name: 'linux'
static_configs:
- targets: ['localhost:9100']
labels:
instance: 'localhost'
- job_name: 'pushgateway'
static_configs:
- targets: ['localhost:9091']
labels:
instance: 'pushgateway'
./prometheus --config.file=prometheus.yml
查看进程
netstat -apn | grep -E '9091|3000|9090|9100'
这个9091端口就是flink-conf.yml对应的metrics.reporter.promgateway.port: 9091
flink会把一些metric push到9091端口上,然后prometheus采集。
查看 prometheus:ip:9090/targets
如果state 不是 UP 的,等一会就起来了
启动flink集群:
.bin/start-cluster.sh
访问:
http://localhost:9090
下载:
wget https://dl.grafana.com/oss/release/grafana-6.3.6.linux-amd64.tar.gz
解压:
tar -zxvf grafana-6.3.6.linux-amd64.tar.gz
启动:
./bin/grafana-server web &
查看Grafana:
默认用户名密码 :amin/admin.
flink run -m yarn-cluster -ynm LateDataProcess -yn 1 -c com.venn.stream.api.sideoutput.lateDataProcess.LateDataProcess jar/flinkDemo-1.0.jar
查看任务webUI:
PS:任务已经跑了一段时间了
由于上面一句配置好Flink report、 pushgateway、prometheus,并且在Grafana中已经添加了prometheus 数据源,所以Grafana中会自动获取到 flink job的metrics 。
Grafana 首页,点击New dashboard,创建一个新的dashboard.
选中之后,即会出现对应的监控指标
至此,Flink 的metrics 的指标展示在Grafana 中了
flink 指标对应的指标名比较长,可以在Legend 中配置显示内容,在{undefined{key}} 将key换成对应需要展示的字段即可,如:{undefined{job_name}},{undefined{operator_name}}
对应显示如下:
整体Flink metrics数据流转的流程是:
flink metric -> pushgateway -> prometheus ->grafana