1、Pushgetway安装和使用

在这里插入图片描述

1.1 Pushgateway是什么

pushgateway 是另一种数据采集的方式,采用被动推送来获取监控数据的prometheus插件,它可以单独运行在

任何节点上,并不一定要运行在被监控的客户端。

首先通过用户自定义编写的脚本把需要监控的数据发送给 pushgateway,pushgateway 再将数据推送给对应的

Prometheus 服务。

对于短时运行、不支持轮询的任务,可以引入 pushgateway,将指标数值以 push 的方式推送到 pushgateway暂

存,然后 prometheus 从 pushgateway 中轮询。

PushGateway:短期存储指标数据。主要用于临时性的任务,各个目标主机可以上报数据到 pushgateway,然后

prometheus server 统一从 pushgateway 拉取数据。

Pushgateway 是 prometheus 的一个组件,prometheus server 默认是通过 exporter 主动获取数据(默认采取

pull 拉取数据),pushgateway 则是通过被动方式推送数据到 prometheus server,用户可以写一些自定义的监控

脚本把需要监控的数据发送给 pushgateway, 然后 pushgateway 再把数据发送给 Prometheus server。

1.2 使用Pushgateway的主要原因

1、因为Prometheus 采用 pull 模式,可能由于不在一个子网或者防火墙,导致 Prometheus 无法直接拉取各个

target 数据。

Prometheus 在一些情况下无法直接拉取各个 target 数据。

2、在监控业务数据的时候,需要将不同数据汇总,由 Prometheus 统一收集。

1.3 使用Pushgateway的弊端

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

1、将多个节点数据汇总到 pushgateway,如果pushgateway 挂了,受影响比多个 target 大。

通过单个 Pushgateway 监控多个实例时, Pushgateway 将会成为单点故障和潜在瓶颈。

2、Prometheus 拉取状态 up 只针对 pushgateway, 无法做到对每个节点有效。

3、Pushgateway 可以持久化推送给它的所有监控数据。 因此,即使你的监控已经下线,prometheus 还会拉取

到旧的监控数据,需要手动清理 pushgateway 不要的数据。

总:

  • Prometheus拉取状态只针对 pushgateway,不能对每个节点都有效。

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

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

1.4 Pushgateway流程图

在这里插入图片描述

我们编写脚本将数据发送到 Pushgateway,Pushgateway将数据 push 到 Prometheus。

1.5 与Prometheus结合使用流程图

通过客户端 POST数据至pushgateway,prometheus拉取pushgateway里数据,经过alertmanager报警规则触

发,到prometheusalert自定义模板,最后飞书机器人发送该报警信息。

在这里插入图片描述

1.6 Pushgateway使用

1.6.1 下载部署包

下载地址:

https://github.com/prometheus/pushgateway/releases/

这里下载 pushgateway-1.4.3.linux-amd64.tar.gz

https://github.com/prometheus/pushgateway/releases/download/v1.4.3/pushgateway-1.4.3.linux-amd64.tar.gz
1.6.2 解压
tar -xvf pushgateway-1.4.3.linux-amd64.tar.gz
1.6.3 启动
nohup ./pushgateway >> nohup.out 2>&1 &
1.6.4 测试

访问:http://192.168.54.195:9091/

在这里插入图片描述

1.6.5 在Prometheus配置
- job_name: pushgateway
  static_configs:
    - targets: ['192.168.54.195:9091']
      labels:
        instance: pushgateway

重新启动 Prometheus:

nohup ./prometheus --config.file=prometheus.yml >> nohup.out 2>&1 &

在这里插入图片描述

1.6.6 测试发送数据
# 推送指定的数据格式到pushgateway
# 将"metrics"字节赋值"3.6",向{job="test_job"}添加单条数据
$ echo "metric 3.6" | curl --data-binary @- http://192.168.54.195:9091/metrics/job/test_job

查看结果:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

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

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

# 利用shell脚本完成数据推送
$ vim push.sh
# 内容
node_memory_usages=$(free -m | grep Mem | awk '{print $3/$2*100}')
job_name="memory"
instance_name="192.168.54.195"
cat <<EOF | curl --data-binary @- http://192.168.54.195:9091/metrics/job/$job_name/instance/$instance_name
# TYPE node_memory_usages gauge
node_memory_usages $node_memory_usages
EOF
# 执行
./push.sh

在这里插入图片描述

1.6.7 定时任务定时推送数据
# 新建定时任务
crontab -e 
或者
vim /etc/crontab

第一种方式 crontab -e

  • 输入 crontab -e
  • 按下 a 键进入到编辑模式
  • 输入定时任务
  • 同时按下 ctrl+c 退出编辑模式
  • 按下 shift+: 输入 wq 退出 crontab

第二种方式 vim /etc/crontab

$ vim /etc/crontab
# 写入如下内容
1 * * * * sh /home/zhangshixing/prometheus/prometheus-2.34.0-rc.2.linux-amd64/push.sh
# 查看定时任务
# 第一种方式
$ crontab -l
1 * * * * sh /home/zhangshixing/prometheus/prometheus-2.34.0-rc.2.linux-amd64/push.sh
# 第二种方式
$ cat /var/log/cron
Mar  3 09:23:01 zsx crond[27252]: (root) RELOAD (/var/spool/cron/root)
$ cat /var/spool/cron/root
1 * * * * sh /home/zhangshixing/prometheus/prometheus-2.34.0-rc.2.linux-amd64/push.sh
1.6.8 删除某个组下的某实例的所有数据
curl -X DELETE http://192.168.54.195:9091/metrics/job/some_job/instance/some_instance
curl -X DELETE http://192.168.54.195:9091/metrics/job/some_job

1.7 Pushgateway使用方法

$ ./pushgateway --help
usage: pushgateway [<flags>]

The Pushgateway

Flags:
  -h, --help                     		Show context-sensitive help (also try --help-long and --help-man).
      --web.config.file=""       		[EXPERIMENTAL] Path to configuration file that can enable TLS or uthentication.
      --web.listen-address=":9091"		Address to listen on for the web interface, API, and telemetry.
      --web.telemetry-path="/metrics"	Path under which to expose metrics.
      --web.external-url=        		The URL under which the Pushgateway is externally reachable(可从外部访问Pushgateway的URL).
      --web.route-prefix=""      		Prefix for the internal routes of web endpoints. Defaults to the path of --web.external-url(web端点内部路由的前缀,默认为-web.external-url的路径).
      --web.enable-lifecycle     		Enable shutdown via HTTP request.
      --web.enable-admin-api     		Enable API endpoints for admin control actions.
      --persistence.file=""      		le to persist metrics. If empty, metrics are only kept in memory(持久化metrics的文件,如果该文件为空,则metrics只保留在内存中).
      --persistence.interval=5m  		The minimum interval at which to write out the persistence file(写入持久性文件的最小间隔).
      --push.disable-consistency-check	Do not check consistency of pushed metrics. DANGEROUS.                  
      --log.level=info           		Only log messages with the given severity or above. One of: [debug, nfo, warn, error]
      --log.format=logfmt        		Output format of log messages. One of: [logfmt, json]
      --version                  		Show application version.

1.8 docker部署pushgateway

搜索镜像:

$ docker search pushgateway

在这里插入图片描述

$ docker search prom/pushgateway

在这里插入图片描述

拉取镜像:

$ docker pull prom/pushgateway

在这里插入图片描述

启动:

$ docker run -d --name pushgateway -p 9091:9091 --network host prom/pushgateway

在这里插入图片描述

通过http://192.168.54.195:9091/进行访问:

在这里插入图片描述

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐