Prometheus监控运维实战十一(Pushgateway)

当筵意气临九霄,星离雨散不终朝。这篇文章主要讲述Prometheus监控运维实战十一:Pushgateway相关的知识,希望能为你提供帮助。
一. Pushgateway简介Pushgateway为Prometheus整体监控方案的功能组件之一,并做于一个独立的工具存在。它主要用于Prometheus无法直接拿到监控指标的场景,如监控源位于防火墙之后,Prometheus无法穿透防火墙;目标服务没有可抓取监控数据的端点等多种情况。
在类似场景中,可通过部署Pushgateway的方式解决问题。当部署该组件后,监控源通过主动发送监控数据到Pushgateway,再由Prometheus定时获取信息,实现资源的状态监控。

Prometheus监控运维实战十一(Pushgateway)

文章图片



工作流程:
    a. 监控源通过Post方式,发送数据到Pushgateway,路径为/metrics。
    b. Prometheus服务端设置任务,定时获取Pushgateway上面的监控指标。
    c. Prometheus拿到监控指标后,根据配置的告警规则,如果匹配将触发告警到Alertmanager;同时,Grafana可配置数据源调用Prometheus数据,做为数据展示。
    d. Alertmanager收到告警后,根据规则转发到对应接收人及接收介质;Grafana方面,用户可登录并根据数据源的监控指标,配置相关的图表展示 。


二. 安装部署1、二进制安装 
下载安装包


$ wget https://github.com/prometheus/pushgateway/releases/download/v1.4.1/pushgateway-1.4.1.linux-amd64.tar.gz



解压并安装 
?
$ tar -xvf pushgateway-1.4.1.linux-amd64.tar.gz
$ cd pushgateway-1.4.1.linux-amd64
$ sudo cp pushgateway /usr/local/bin/



查看版本号验证是否正常
?
$ pushgateway --version
pushgateway, version 1.4.1 (branch: HEAD, revision: 6fa509bbf4f082ab8455057aafbb5403bd6e37a5)
build user:root@da864be5f3f0
build date:20210528-14:30:10
go version:go1.16.4
platform:linux/amd64



启动服务,默认端口为9091,可通过--web.listen-address更改监听端口
?
pushgateway &



2、docker安装
$ docker pull prom/pushgateway
$ docker run -d --name=pushgateway -p 9091:9091 prom/pushgateway



部署完成后,在浏览器输入 http://$IP:9091 即可看到程序界面 
Prometheus监控运维实战十一(Pushgateway)

文章图片



三. 数据推送Pushgatewaypushgateway的数据推送支持两种方式,Prometheus Client SDK推送和API推送。
?

1、Client SDK推送
?Prometheus本身提供了支持多种语言的SDK,可通过SDK的方式,生成相关的数据,并推送到pushgateway,这也是官方推荐的方案。目前的SDK覆盖语言有官方的?
  • Go
  • java or Scala
  • python
  • Ruby
也有许多第三方的,详情可参见此链接:https://prometheus.io/docs/instrumenting/clientlibs/
示例:
本示例以python为例,讲解SDK的使用
?
from prometheus_client import Counter,Gauge,push_to_gateway
from prometheus_client.core import CollectorRegistry

registry = CollectorRegistry()
data1 = Gauge(\'gauge_test_metric\',\'This is a gauge-test-metric\',[\'method\',\'path\',\'instance\'],registry=registry)
data1.labels(method=\'get\',path=\'/aaa\',instance=\'instance1\').inc(3)

push_to_gateway(\'10.12.61.3:9091\', job=\'alex-job\',registry=registry)



注解:
第一、二行代码:引入相关的Prometheus SDK;
第五行代码:创建相关的指标,类型为Gauge。其中“gauge_test_metric”为指标名称,\'This is a gauge-test-metric\'为指标注释,[\'method\',\'path\',\'instance\']  为指标相关的label。
第六行代码:添加相关的label信息和指标value  值。
第六行代码:push数据到pushgateway,\'10.12.61.3:9091\'为发送地址,job指定该任务名称。
【Prometheus监控运维实战十一(Pushgateway)】

以上代码产生的指标数据等同如下 :
?
# HELP gauge_test_metric This is a gauge-test-metric
# TYPE gauge_test_metric gauge
gauge_test_metric{instance="instance1",method="get",path="/aaa"} 3.0



2、 API推送
通过调用pushgateway  API的方式实现数据的推送。
请求格式:


/metrics/job/< jobname> {/instance/instance_name}



< jobname> 将用作Job标签的值,然后是其他指定的标签。
示例:
本例中定义了两个标签 job=alex-job和instance=instance1,并推送了指标  http_request_total  及其value值,10.12.61.1  为pushgateway地址。


echo \'http_request_total 12\' | http://10.12.61.3:9091/metrics/job/alex-job/instance/instance1



复杂数据发送:
?
$ cat < < EOF | curl --data-binary @- http://10.12.61.3:9091/metrics/job/alex-job/instance/10.2.10.1
# TYPE http_request_total counter
http_request_total{code="200",path="/aaa"} 46
http_request_total{code="200",path="/bbb"} 15
EOF



数据推送完成后,可登录pushgateay地址查看指标情况
Prometheus监控运维实战十一(Pushgateway)

文章图片



假如需要删除pushgateway上面存储的指标信息,可通过如下方式操作:
删除某个组下某个实例的所有数据
?
curl -X DELETE http://10.12.61.3:9091/metrics/job/alex-job/instance/10.2.10.1



删除某个job下所有的数据
 
curl -X DELETE http://10.12.61.3:9091/metrics/job/alex-job





四. prometheus抓取数据Pushgateway只是指标的临时存放点,最终我们需要通过Prometheus将其存放到时间序列数据库里。对此,我们需要在Prometheus上面创建一个job


- job_name: \'pushgateway\'
honor_labels: true
static_configs:
- targets:
- \'10.12.61.3:9091\'



Prometheus监控运维实战十一(Pushgateway)

文章图片



目标任务正常启动后,可在prometheus查看到相关的指标数据
Prometheus监控运维实战十一(Pushgateway)

文章图片

五. 注意事项
  1. 通过Pushgateway方式,Prometheus无法直接检测到监控源服务的状态,故此种方式不适用于监控服务的存活状态等场景。
  2. Pushgateway属于静态代理,它接收的指标不存在过期时间,故会一直保留直到该指标被更新或删除。此种情况下,不再使用的指标可能存在于网关中。
  3. 如上所言,Pushgateway并不算是完美的解决方案,在监控中更多做为辅助方案存在,用于解决Prometheus无法直接获取数据的场景。































    推荐阅读