【监控】-Pushgateway

xiaohai 2019-11-24 19:00:07 6023人围观 标签: 监控 
简介Pushgateway是一个独立的服务,Pushgateway位于应用程序发送指标和Prometheus服务器之间。

参考文档:https://www.cnblogs.com/xiao987334176/p/9933963.html

Pushgateway 是 Prometheus 生态中一个重要工具,使用它的原因主要是:

  • Prometheus 采用 pull 模式,可能由于不在一个子网或者防火墙原因,导致 Prometheus 无法直接拉取各个 target 数据。
  • 在监控业务数据的时候,需要将不同数据汇总, 由 Prometheus 统一收集。

由于以上原因,不得不使用 pushgateway,但在使用之前,有必要了解一下它的一些弊端:

  • 将多个节点数据汇总到 pushgateway, 如果 pushgateway 挂了,受影响比多个 target 大。
  • Prometheus 拉取状态 up 只针对 pushgateway, 无法做到对每个节点有效。
  • Pushgateway 可以持久化推送给它的所有监控数据。

因此,即使你的监控已经下线,prometheus 还会拉取到旧的监控数据,需要手动清理pushgateway不要的数据。

image.png

本文我们主要介绍如何安装何使用Pushgateway:

一、安装阶段
1、下载,就在Prometheus的下载页面,https://prometheus.io/download/
[root@localhost ~]# wget https://github.com/prometheus/pushgateway/releases/download/v1.0.0/pushgateway-1.0.0.linux-amd64.tar.gz
2、解压和安装
#解压 [root@localhost ~]# tar -zxvf pushgateway-1.0.0.linux-amd64.tar.gz -C /apps/ #接入到解压目录 [root@localhost ~]# cd /apps/ #创建软连接 [root@localhost apps]# ln -s pushgateway-1.0.0.linux-amd64 pushgateway
3、创建数据文件目录
[root@localhost pushgateway]# mkdir /apps/pushgateway/data
4、添加到服务
#vim /usr/lib/systemd/system/pushgateway.service #内容如下 [Unit] Description=pushgateway After=network.target [Service] Type=simple WorkingDirectory=/apps/pushgateway ExecStart=/apps/pushgateway/pushgateway --persistence.file=/apps/pushgateway/data/pushgateway.data Restart=on-failure [Install] WantedBy=multi-user.target
5、通过服务进行启动
#加入到开机启动 systemctl enable pushgateway #启动 systemctl start pushgateway #停止 systemctl stop pushgateway
6、pushgateway默认端口为9091
7、配置Prometheus
- job_name: pushgateway static_configs: - targets: ['127.0.0.1:9091'] labels: instance: pushgateway

image.png

配置完后重启Prometheus,然后在Prometheus界面查看targets

image.png

以上就安装完了pushgateway,下面我们介绍如何使用呢?

二、使用pushgateway

一般我们会使用客户端SDK推送数据到pushgateway, 但是我们还可以通过API来管理, 例如:

1、将单个样本推入由{job=“some_job”}以下项标识的组中:
echo "some_metric 10" | curl --data-binary @- http://localhost:9091/metrics/job/job_one

由于未提供类型信息,因此some_metric将为type untyped。

  • –data-binary 表示发送二进制数据,注意:它是使用POST方式发送。
  • job_one:是我们的job名称
  • some_metric:指标名称

推送成功后可以在Prometheus中查看该指标了

image.png

2、数据会带上instance, 表示来源位置
echo "some_metric 12" | curl --data-binary @- http://localhost:9091/metrics/job/job_one/instance/instance_1
3、推送更复杂数据
cat <<EOF | curl --data-binary @- http://localhost:9091/metrics/job/job_two/instance/instance_1 # TYPE some_metric counter some_metric_2{label="val1"} 42 # TYPE another_metric gauge # HELP another_metric Just an example. another_metric 2398.283 EOF

注意:必须是指定的格式才行

4、删除某个组下的某实例的所有数据
curl -X DELETE http://localhost:9091/metrics/job/job_one/instance/instance_1
5、删除某个组下的所有数据
curl -X DELETE http://localhost:9091/metrics/job/job_one

更多的使用请参考:https://github.com/prometheus/pushgateway

可以发现pushgateway中的数据我们通常按照job和instance分组分类,所以这两个参数不可缺少。因为Prometheus配置pushgateway 的时候,也会指定job和instance, 但是它只表示pushgateway实例,不能真正表达收集数据的含义。所以在prometheus中配置pushgateway的时候,需要添加 honor_labels: true 参数, 从而避免收集数据本身的 job 和 instance 被覆盖。
注意,为了防止 pushgateway 重启或意外挂掉,导致数据丢失,我们可以通过 -persistence.file 和 -persistence.interval 参数将数据持久化下来。