Prometheus Pushgateway

Prometheus Pushgateway

一、Pushgateway概述

1.1、Pushgateway简介

Pushgateway是prometheus的一个组件,prometheus server默认是通过exporter主动获取数据(默认采取pull拉取数据),pushgateway则是通过被动方式推送数据到prometheus server,用户可以写一些自定义的监控脚本把需要监控的数据发送给pushgateway, 然后pushgateway再把数据发送给Prometheus server

1.2、Pushgateway优点

Prometheus 默认采用定时pull 模式拉取targets数据,但是如果不在一个子网或者防火墙,prometheus就拉取不到targets数据,所以可以采用各个target往pushgateway上push数据,然后prometheus去pushgateway上定时pull数据

在监控业务数据的时候,需要将不同数据汇总, 汇总之后的数据可以由pushgateway统一收集,然后由 Prometheus 统一拉取。

1.3、pushgateway缺点

1)Prometheus拉取状态只针对 pushgateway, 不能对每个节点都有效;

2)Pushgateway出现问题,整个采集到的数据都会出现问题

3)监控下线,prometheus还会拉取到旧的监控数据,需要手动清理 pushgateway不要的数据。

二、Pushgateway安装及使用

2.1、Pushgateway安装

1)安装Pushgateway

# 在k8s-node节点操作
[root@k8s-node1 ~]# docker run -d --name pushgateway -p 9091:9091 prom/pushgateway
[root@k8s-node1 ~]# docker ps -a|grep pushgateway
505f7dc4a17b   prom/pushgateway                                    "/bin/pushgateway"       13 seconds ago   Up 13 seconds               0.0.0.0:9091->9091/tcp, :::9091->9091/tcp       pushgateway

image-20210713090544531

此时pushgateway上还没有数据

2)修改prometheus-alertmanager-cfg.yaml文件,添加job

[root@k8s-master1 prometheus]# vim prometheus-alertmanager-cfg.yaml
    - job_name: 'pushgateway'
      scrape_interval: 5s
      static_configs:
      - targets: ['192.168.40.181:9091']
      honor_labels: true

# honor_labels: true可以避免targets列表中的job_name是pushgateway的job,instance 和上报到pushgateway数据的job和instance冲突
# 更新
[root@k8s-master1 prometheus]# kubectl apply -f prometheus-alertmanager-cfg.yaml
[root@k8s-master1 prometheus]# kubectl delete -f prometheus-alertmanager-deploy.yaml 
[root@k8s-master1 prometheus]# kubectl apply -f prometheus-alertmanager-deploy.yaml

image-20210713090907864

3)测试发送数据

# 推送指定的数据格式到pushgateway
# 向 {job="test_job"} 添加单条数据:
echo "metric 3.6" | curl --data-binary @- http://192.168.40.181:9091/metrics/job/test_job

image-20210713091105295

# 添加复杂数据
cat <<EOF | curl --data-binary @- http://192.168.40.181:9091/metrics/job/test_job/instance/test_instance
node_memory_usage 36
node_memory_total 36000
EOF

image-20210713091756662

# 删除某个组下某个实例的所有数据
curl -X DELETE http://192.168.40.181:9091/metrics/job/test_job/instance/test_instance

# 删除某个组下的所有数据:
curl -X DELETE http://192.168.40.181:9091/metrics/job/test_job

2.2、Pushgateway使用

把数据上报到pushgateway,在被监控服务所在的机器配置数据上报,想要把192.168.40.181这个机器的内存数据上报到pushgateway

1)编写shell脚本

[root@k8s-node1 ~]# vim push.sh
node_memory_usages=$(free -m | grep Mem | awk '{print $3/$2*100}')
job_name="memory"
instance_name="192.168.40.181"
cat <<EOF | curl --data-binary @- http://192.168.40.181:9091/metrics/job/$job_name/instance/$instance_name
# TYPE node_memory_usages gauge
node_memory_usages $node_memory_usages
EOF

2)打开pushgateway web ui界面,可看到如下

image-20210713092705624

3)打开prometheus ui界面,可看到如下node_memory_usages的metrics指标

image-20210713092834979

4)设置计划任务,定时上报数据

[root@k8s-node1 ~]# crontab -e
*/1 * * * * /usr/bin/bash  /root/push.sh
作者:Lawrence

-------------------------------------------

个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!

扫描上面二维码关注我
如果你真心觉得文章写得不错,而且对你有所帮助,那就不妨帮忙“推荐"一下,您的“推荐”和”打赏“将是我最大的写作动力!
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.
原文地址:https://www.cnblogs.com/hujinzhong/p/15005036.html