date: 2020-10-14title: Pushgateway部署及应用 #标题
tags: 监控 #标签
categories: 监控 # 分类
Pushgateway是prometheus的一个组件,prometheus server默认是通过exporter主动获取数据(默认采取pull拉取数据),pushgateway则是通过被动方式推送数据到prometheus server,用户可以写一些自定义的监控脚本把需要监控的数据发送给pushgateway, 然后pushgateway再把数据发送给Prometheus server。
Prometheus拉取状态只针对 pushgateway, 不能对每个组件都有效。
Pushgateway优缺点
优点
- Prometheus 默认采用定时pull模式拉取targets数据,但是如果不在一个子网或者防火墙,prometheus就拉取不到targets数据,所以可以采用各个target往pushgateway上push数据,然后prometheus去pushgateway上定时pull数据
- 在监控业务数据的时候,需要将不同数据汇总,汇总之后的数据可以由pushgateway统一收集,然后由 Prometheus 统一拉取。
缺点
- Pushgateway出现问题,整个采集到的数据都会出现问题
- 监控下线,prometheus还会拉取到旧的监控数据,需要手动清理pushgateway不要的数据。
部署Pushgateway
分别写下二进制部署及k8s部署两种方式,两种方式二选一即可。
二进制部署Pushgateway
$ wget https://github.com/prometheus/pushgateway/releases/download/v1.3.0/pushgateway-1.3.0.linux-amd64.tar.gz$ tar zxf pushgateway-1.3.0.linux-amd64.tar.gz -C /opt/$ ln -sf /opt/pushgateway-1.3.0.linux-amd64 /opt/pushgateway$ cd /opt/pushgateway$ ./pushgateway --help # 可以执行此指令,查看命令支持的选项# 启动pushgateway$ nohup ./pushgateway --web.listen-address=":9091" --web.enable-lifecycle &$ ss -lnput | grep 9091 # 确定端口在监听tcp LISTEN 0 128 [::]:9091 [::]:* users:(("pushgateway",pid=17900,fd=3))
浏览器访问
访问pushgateway所在机器的9091端口,看到如下空白页面(因为还没数据):

k8s部署Pushgateway
# 编写yaml文件如下$ cat >> deploy_pushgateway.yaml < EOF---apiVersion: apps/v1kind: Deploymentmetadata:annotations:k8s.kuboard.cn/service: NodePortk8s.kuboard.cn/workload: monitor-pushgatewaylabels:k8s.kuboard.cn/layer: monitork8s.kuboard.cn/name: monitor-pushgatewayname: monitor-pushgatewaynamespace: promspec:replicas: 1selector:matchLabels:k8s.kuboard.cn/layer: monitork8s.kuboard.cn/name: monitor-pushgatewaystrategy:rollingUpdate:maxSurge: 25%maxUnavailable: 25%type: RollingUpdatetemplate:metadata:labels:k8s.kuboard.cn/layer: monitork8s.kuboard.cn/name: monitor-pushgatewayspec:containers:- name: pushgatewayimage: prom/pushgateway:latestimagePullPolicy: IfNotPresentargs:- '--web.enable-lifecycle'---apiVersion: v1kind: Servicemetadata:name: monitor-pushgatewaynamespace: promspec:externalTrafficPolicy: Clusterports:- nodePort: 30099port: 9091protocol: TCPtargetPort: 9091selector:k8s.kuboard.cn/layer: monitork8s.kuboard.cn/name: monitor-pushgatewaytype: NodePortEOF$ kubectl apply -f deploy_pushgateway.yaml # 执行yaml文件$ kubectl get pods -n prom # 确定容器已运行NAME READY STATUS RESTARTS AGEmonitor-pushgateway-655db76c4d-ksv8p 1/1 Running 0 2m22s.......... # 省略部分内容# 确定宿主机端口在监听$ ss -lnput | grep 30099tcp LISTEN 0 128 *:30099 *:* users:(("kube-proxy",pid=8263,fd=18))
添加pushgateway至Prometheus
# Prometheus配置文件中增加如下job(targets指定的是pushgateway的IP+端口):- job_name: 'pushgateway'scrape_interval: 5sstatic_configs:- targets: ['192.168.20.2:30099']honor_labels: true
honor_labels: true一般会在添加pushgateway时填写,其含义是为了防止推送到pushgateway上的job和instance标签产生冲突,官方文档解释如下:

增加job后,重启Prometheus生效,Prometheus的targets列表中看到如下,即表示配置完成:

推送指定的数据到pushgateway
参考:Github官方文档。
推送简单数据到pushgateway
$ echo "some_metric 10.12" | curl --data-binary @- http://192.168.20.2:30099/metrics/job/test_job# --data-binary:表示发送二进制数据,注意:它是使用POST方式发送的!# @- http://192.168.20.2:30099/metrics/job/:为固定格式,只需将IP+Port改为你自己pushgateway的即可。# test_job:为推送到pushgateway的job名称。
访问pushgateway看到如下数据,即表示推送成功:

访问Prometheus亦可以查询到刚刚推送的数据:

推送复杂数据到pushgateway
$ cat <<EOF | curl --data-binary @- http://192.168.20.2:30099/metrics/job/test_job/instance/192.168.20.3# TYPE some_metric counterone_metric{label="va11"} 42# TYPE another_metric gauge# HELP another_metric Just an example.another_metric 2398.283EOF# 在上面的url中,instance为固定路径,192.168.20.3为你想定义的实例名称
pushgateway查看数据如下:

删除pushgateway的数据
# 删除某个组下某个实例的所有数据curl -X DELETE http://192.168.20.2:30099/metrics/job/test_job/instance/192.168.20.3# 删除某个组下的所有数据$ curl -X DELETE http://192.168.20.2:30099/metrics/job/test_job
亦可以直接在pushgateway界面进行删除,如下:

Pushgateway实践
自定义数据采集至Prometheus
此处的示范可套用于实际工作中,我只是简单的提取两个指标,可以在脚本中提取自己感兴趣的指标。
$ cat nginx_chk.sh # 以下脚本以监控nginx服务为例#!/usr/bin/env bashjob_name="nginx_port_check"instance_name=(192.168.20.2:80192.168.20.3:8085)for i in ${instance_name[@]}doresult=$(curl -o /dev/null -s -w %{http_code}"\n" ${i})#echo $i $resultcat <<EOF | curl --data-binary @- http://192.168.20.2:30099/metrics/job/$job_name/instance/$i#TYPE node_memory_usages gaugenginx_status_code ${result}EOFdone$ chmod 755 nginx_chk.sh # 增加执行权限$ crontab -e # 编写计划任务,每分钟执行一次*/1 * * * * /root/nginx_chk.sh
系统自带计划任务,仅支持每分钟运行,若想精准到秒,可以写个死循环脚本,执行一次循环体后休眠几秒,然后再执行一次….. 把这个死循环脚本放置后台运行即可,无需计划任务。
一分钟后访问pushgateway,即可看到如下数据:

访问Prometheus查询相关指标,即可看到如下值:

针对采集数据配置报警
# 修改Prometheus的rules文件,添加如下告警字段groups:- name: examplerules: # rules下增加如下配置- alert: web服务异常expr: nginx_status_code != 200for: 2slabels:severity: warnningannotations:description: "{{$labels.instance}}的{{$labels.job}}异常,目前状态码为:{{$value}}(状态码为0表示无法连接web服务)"
模拟web服务出现403状态码,你会收到如下告警信息:

待服务正常后,即可收到告警恢复邮件:

