Prometheus06-pushgateway
6.pushgateway
pushgateway的作用和适用情况
作用
Pushgateway本质上是一种用于监控Prometheus服务器无法抓取的资源的解决方案
适用情况
·网络环境中的安全访问策略,服务或应用程序仅允许访问特定端口,使得prommetheus无法直接去拉取各个目标的监控数据
·目标资源的生命周期太短。例如,在容器启动、执行和停止时,运行一个Prometheus作业,发现目标在很短的周期内完成执行,结果不能获取该目标的信息。
·目标资源没有可以抓取的端点。例如,批处理作业一般不可能具有可以被抓取的正在运行的HTTP服务,即使该作业运行的时间足够长。”
pushgateway拓扑简图
pushgateway工作原理及优点
工作原理
Pushgateway是一个独立的服务组件,可在HTTP REST API上接收Prometheus指标,位于发送指标的应用服务程序和Prometheus服务器之间。Pushgateway接收来自应用服务器的指标,prometheus从pushgateway拉取指标
运维人员通过写自己的脚本程序,抓自己想要的监控数据,转换成 K/V 的形式;
然后推送到 Pushgateway(HTTP POST) 再由 Prometheus 拉取 Pushgateway 的数据
优点
1.实现灵活监控,自定义监控数据
2.节约资源,exporter采集的数据过多,大部分用不到
二进制安装pushgateway
1.安装
wget https://github.com/prometheus/pushgateway/releases/download/v1.5.1/pushgateway-1.5.1.linux-amd64.tar.gz
tar -zxf pushgateway-1.5.1.linux-amd64.tar.gz
cp pushgateway /usr/local/bin/
pushgateway --version
2.配置和运行
pushgateway --web.listen-address="0.0.0.0:9091" # 开箱即用,无需配置
数据默认存在于内存中
可以通过指定--persistence.file参数将指标持久存储于磁盘路径
默认文件每五分钟持久化写入一次,可以使用--persistence.interval参数覆盖它
pushgateway --persistence.file="/tmp/pushgateway_persist"
3.pushgateway与prometheus集成
vi prometheus.yml
- job_name: push_pushgateway
honor_labels: true
static_configs:- targets: ['192.168.24.74:9091']”
http://127.0.0.1:9091/metrics
http://127.0.0.1:9091
向pushgateway发送指标
echo "batchjob1_user_counter 2" | curl --data-binary @-
http://localhost:9091/metrics/job/job_name/instance/instance_name
·url中指定了job名称和实例名称
可以通过在推送中传递TYPE和HELP语句来向监控指标添加类型描述,并向监控指标组添加更多指标
cat <<EOF | curl --data-binary @- http:// localhost:9091/metrics/job/some_job/instance/
game_server
TYPE some_metric counter
some_metric{label="label-1"} 52
TYPE another_metric gauge
HELP another_metric Just an example.
another_metric 2019.113
EOF
pushgateway文档
https://github.com/prometheus/pushgateway/releases
删除pushgateway的指标
删除作业some_job1的所有监控指标
curl -X DELETE localhost:9091/metrics/job/some_job1
仅删除特定实例中的监控指标
curl -X DELETE localhost:9091/metrics/job/some_job/instance/game_server
基于推送的prometheus监控结构图
结构介绍
实际生产中,服务器可能安装有多个exporter,为了减少端口的暴露,可以使用基于推送的监控架构
在node_exporter和pushgateway之间添加了一个转发器。
转发器获取监控指标并将其发送给pushgateway,Prometheus从pushgateway中获取数据,而不是直接从node_exporter中获取数据”在这里,node_exports仍然监听特定端口以公开监控指标,但只接受内部请求,这样有助于保护节点免受被外部恶意访问。
转发器介绍
转发器是两个curl的操作,可以编写为脚本并加入计划任务
vim trans.sh
curl -s http://
curl --data-binary @- http://
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?