Prometheus学习笔记(4)什么是pushgateway???
一、pushgateway介绍
pushgateway是另一种数据采集的方式,采用被动推送来获取监控数据的prometheus插件,它可以单独运行在任何节点上,并不一定要运行在被监控的客户端。而后通过用户自定义编写的脚本把需要监控的数据发送给pushgateway,pushgateway再将数据推送给prometheus server。
二、pushgateway的安装运行和配置
2.1、pushgateway安装
官方下载地址:https://prometheus.io/download/#pushgateway
pushgateway和prometheus、node_exporter一样,直接下载,解压后直接运行即可。pushgateway启动后,默认监听9091端口,如下一顿操作:
# 下载
[root@prometheus ~]# wget https://github.com/prometheus/pushgateway/releases/download/v1.0.0/pushgateway-1.0.0.linux-amd64.tar.gz
# 解压
[root@prometheus ~]# tar -zxf pushgateway-1.0.0.linux-amd64.tar.gz -C /usr/local/
[root@prometheus ~]# mv /usr/local/pushgateway-1.0.0.linux-amd64 /usr/local/pushgateway-1.0.0
[root@prometheus ~]# ln -sv /usr/local/pushgateway-1.0.0 /usr/local/pushgateway
‘/usr/local/pushgateway’ -> ‘/usr/local/pushgateway-1.0.0’
# 运行
[root@prometheus ~]# cd /usr/local/pushgateway
[root@prometheus pushgateway]# ll
total 16136
-rw-r--r-- 1 3434 3434 11357 Oct 16 04:10 LICENSE
-rw-r--r-- 1 3434 3434 487 Oct 16 04:10 NOTICE
-rwxr-xr-x 1 3434 3434 16505766 Oct 16 03:58 pushgateway
[root@prometheus pushgateway]# ./pushgateway &
[1] 28453
[root@prometheus pushgateway]# level=info ts=2019-12-11T05:44:40.631Z caller=main.go:81 msg="starting pushgateway" version="(version=1.0.0, branch=HEAD, revision=cc61f46971f5eb7a5be64e80c2ee03857ddbb41a)"
level=info ts=2019-12-11T05:44:40.631Z caller=main.go:82 build_context="(go=go1.13.1, user=root@58be538fc30e, date=20191015-19:58:18)"
level=info ts=2019-12-11T05:44:40.697Z caller=main.go:142 listen_address=:9091
[root@prometheus pushgateway]# netstat -tulnp |grep 9091
tcp6 0 0 :::9091 :::* LISTEN 28453/./pushgateway
再通过编写systemd的管理脚本对pushgateway进行管理:
[root@prometheus ~]# vim /usr/lib/systemd/system/pushgateway.service
[Unit]
Description=pushgateway
Documentation=https://prometheus.io/docs/introduction/overview
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=simple
PIDFile==/var/run/pushgateway.pid
ExecStart=/usr/local/pushgateway/pushgateway
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
[root@prometheus ~]# systemctl daemon-reload
[root@prometheus ~]# ps -ef |grep push
root 28453 27712 0 13:44 pts/1 00:00:12 ./pushgateway
root 30549 30468 0 17:56 pts/3 00:00:00 grep --color=auto push
[root@prometheus ~]# kill 28453
[root@prometheus ~]# ps -ef |grep push
root 30551 30468 0 17:56 pts/3 00:00:00 grep --color=auto push
[root@prometheus ~]# systemctl start pushgateway
[root@prometheus ~]# ps -ef |grep push
root 30558 1 5 17:56 ? 00:00:00 /usr/local/pushgateway/pushgateway
root 30565 30468 0 17:56 pts/3 00:00:00 grep --color=auto push
[root@prometheus ~]# netstat -tulnp |grep 9091
tcp6 0 0 :::9091 :::* LISTEN 30558/pushgateway
启动后,可以通过web界面进行访问pushgateway,192.168.0.143:9091,也可以通过命令行测试发送监控数据到pushgateway,如下:
[root@node02 ~]# echo "testjob 3.1415926" |curl --data-binary @- http://192.168.0.143:9091/metrics/job/testjob

这里的数据,仅仅只是发送到了pushgateway,还需要在prometheus server上进行配置收集。这里需要注意的是最后是将key & value推送给pushgateway,curl --data-binary是将HTTP POST请求中的数据发送给HTTP服务器(pushgateway),和用户提交THML表单时浏览器的行为是外安全一样的,HTTP POST请求中的数据为纯二进制数据。
2.2、pushgateway配置
在prometheus.yml配置文件中,单独定义一个job,然后将target指向pushgateway运行所在主机的主机名或ip和运行端口即可。如下:
...
- job_name: 'pushgateway'
scrape_interval: 5s
static_configs:
- targets: ['192.168.0.143:9091']
labels:
instance: pushgateway
配置完成后,重启prometheus,再在prometheus的web端查询数据,即可获得刚才的测试数据,如图:

三、自定义脚本发送pushgateway
pushgateway本身没有任何抓取监控数据的功能,它只是被动的等待数据推送过来,下面在来搞搞抓取TCP_ESTABLISHED的瞬时数量,如下脚本:
[root@node02 ~]# cd /usr/local/node_exporter
[root@node02 node_exporter]# cat tcp_establish.sh
#!/bin/bash
instance_name=$(hostname) #本机主机名变量,用于后面的标签
if [ $instance_name == "localhost" ];then
echo "Must change the hostname."
exit 1
fi
label="count_tcp_established" #定义一个新的key
count_tcp_established=$(netstat -an |grep -i ESTABLISHED |wc -l)
echo "$label $count_tcp_established" | curl --data-binary @- http://192.168.0.143:9091/metrics/job/pushgateway/instance/$instance_name
# 增加可执行权限后,多执行几次
[root@node02 node_exporter]# chmod +x tcp_establish.sh
[root@node02 node_exporter]# sh tcp_establish.sh
执行完成后,在pushgateway 的web端以及prometheus server的web端查询数据图,如下:


这里需要了解的是最后收集数据pushgateway的组成,主要是通过linux的命令行去获取数据,并通过echo的方式建立key value数据推送到pushgateway。http://192.168.0.143:9091/metrics/job/pushgateway/instance/$instance_name,这条连接主要分为三个部分:
http://192.168.0.143:9091/metrics/job/pushgateway:这是URL的主location,发送到哪个URL
job/pushgateway:表示是推送到哪个prometheus定义的job里面,上面我们定义的job_name为pushgateway
instance/$instance_name:表示推送后显示的主机名称是什么,从上面pushgateway图也可以看出
需要周期不断地收集该指标数据,当然免不了使用crontab,脚本配合crontab即可发挥强大功效。下面可以配置每5s收集一次该指标数据,又得写个脚本了,如下:
[root@node02 node_exporter]# vim tcp_establish.sh
#!/bin/bash
#每5秒钟刷新一次
step=5
for (( i = 0; i < 60; i=(i+step) )); do
/bin/sh /usr/local/node_exporter/tcp_establish.sh
sleep $step
done
[root@node02 node_exporter]# crontab -e
* * * * * /bin/sh /usr/local/node_exporter/crontab_tcp.sh
四、使用pushgateway的优缺点
这里也有官方的英文解释说明:https://prometheus.io/docs/practices/pushing/ ,总体来说主要下面2点:
(1)pushgateway是一个单点的瓶颈,如果有多个脚本同时发送给一个pushgateway的进程,如果该进程挂掉的话,那么监控数据也就木有了。
(2)pushgateway不能对发送过来的数据进行智能化判断,如果脚本中间采集有问题,那么pushgateway是会照单全收的发送给prometheus server
Prometheus学习笔记(4)什么是pushgateway???的更多相关文章
- Prometheus学习笔记(7)PromQL玩法入门
目录 1.什么是PromQL??? 2.如何查询??? 1.什么是PromQL??? PromQL是Prometheus内置的数据查询语言,其提供对时间序列数据丰富的查询,聚合以及逻辑运算能力的支持. ...
- Prometheus学习笔记(1)Prometheus架构简介
Prometheus简介和架构 Prometheus 是由 SoundCloud 开源监控告警解决方案.架构图如下: 如上图,Prometheus主要由以下部分组成: Prometheus Serve ...
- Prometheus学习笔记之教程推荐
最近学习K8S和基于容器的监控,发现了如下的教程质量不错,记录下来以备参考 K8S最佳实战(包括了K8S的Prometheus监控和EFK日志搜集) https://jimmysong.io/kube ...
- Prometheus学习笔记(6)Alertmanager告警
目录 一.Alertmanager简介 二.Alertmanager部署 三.Alertmanager配置 四.自定义告警规则和发送 五.自定义告警模板 一.Alertmanager简介 Promet ...
- Prometheus学习笔记(5)Grafana可视化展示
目录 一.Grafana安装和启动 二.配置数据源 三.配置dashboard 四.配置grafana告警 一.Grafana安装和启动 Grafana支持查询Prometheus.从Grafana ...
- Prometheus学习笔记(3)什么是node_exporter???
目录 Node_exporter安装配置启动 Node_exporter安装配置启动 node_exporter安装在被监控端,安装方式也比较简单,直接下载解压安装即可,默认启动后监听9100端口. ...
- Prometheus学习笔记(2)Prometheus部署
目录 Prometheus的安装配置启动 Prometheus的安装配置启动 1.Prometheus二进制安装 Prometheus下载链接:https://prometheus.io/downlo ...
- 【Prometheus学习笔记】主机监控 -node_exporter
Exporter for machine metrics prometheus/node_exporter 安装 Prometheus sudo tar -zxvf prometheus-*.tar. ...
- Contour 学习笔记(一):使用 Contour 接管 Kubernetes 的南北流量
原文链接:Contour 学习笔记(一):使用 Contour 接管 Kubernetes 的南北流量 在 Kubernetes 中运行大规模以 Web 为中心的工作负载,最关键的需求之一就是在 L7 ...
随机推荐
- Android 开发基础入门篇: 复制一个工程作为一个新的工程
说明 咱们做项目很多时候都需要复制一份工程出来作为一个新的工程 把第一节的工程拷贝到这一节 修改工程名字 打开软件导入此工程 修改包名 第一节的时候说了,一个APP一个包名 自行添加修改 自行修改 自 ...
- [算法模板]Kruskal重构树
[算法模板]Kruskal重构树 kruskal重构树是一个很常用的图论算法.主要用于解决u->v所有路径上最长边的最小值,就是找到\(u->v\)的一条路径,使路径上的最长边最小. 图片 ...
- 每日一问:简述 View 的绘制流程
Android 开发中经常需要用一些自定义 View 去满足产品和设计的脑洞,所以 View 的绘制流程至关重要.网上目前有非常多这方面的资料,但最好的方式还是直接跟着源码进行解读,每日一问系列一直追 ...
- python 项目实战之Django 邮件发送
发送邮件¶ 虽然 Python 借助 smtplib 模块简化了发送邮件的流程,但是 Django 在其基础上提供了更简化的支持.这些封装意在加快邮件发送,方便在开发时测试发送邮件,在不支持 SMTP ...
- jenkins更新为国内源
系统管理->插件管理->高级->升级站点->URL https://jenkins-zh.gitee.io/update-center-mirror/tsinghua/upda ...
- JAVA读写CSV文件
最近工作需要,需要读写CSV文件的数据,简单封装了一下 依赖读写CSV文件只需引用`javacsv`这个依赖就可以了 <dependency> <groupId>net.sou ...
- Sitecore 8.2 防火墙规则的权威指南
如今,使用多层安全保护您的数据不再是奢侈品; 这是不容谈判的.此外,您需要确保Sitecore解决方案保持运行并与集成服务(例如SQL,Mongo,Solr)通信,同时保持相同的安全级别. 让我们假设 ...
- Java学习:等待唤醒机制
等待唤醒机制 线程的状态 NEW 至今尚未启动的线程处于这种状态 RUNNABLE 正在Java虚拟机中执行的线程处于这种状态 BLOCKED 受阻塞并等待某个监视器锁的线程处于这种状态 WA ...
- 可落地的DDD(4)-如何利用DDD进行微服务的划分(2)
摘要 在前面一篇介绍了如何通过DDD的思想,来调整单体服务内的工程结构,为微服务的拆分做准备.同时介绍了我们在进行微服务拆分的时候踩过的一些坑. 这篇介绍下我们最终的方案,不一定对,欢迎留言讨论. 微 ...
- java面向对象的基本概念
面向对象的基本概念 这里先介绍面向对象程序设计的一些关键概念,并开始使用类,你需要学习一些术语,我们尽量用比较浅显的语言来介绍,因为这些内容都比较重要,所以希望大家好好好理解. 一.什么是对象和面向对 ...