Pushgateway 介绍

发布时间: 更新时间: 总字数:1065 阅读时间:3m 作者:IP:上海 网址

Pushgateway 主要解决 Prometheus Server 不能直接采集短期数据而提供的缓冲

介绍

Prometheus 采用 Pull(拉取)模型采集监控指标,但在很多实际场景中,存在大量短生命周期作业(如定时备份脚本、CronJob、离线批处理任务)。这些任务在两次 Prometheus 拉取间隔之间就可能已执行完毕并退出,使得 Prometheus 无法直接拉取到任务的运行状态与指标。

为了解决短作业指标采集问题,Prometheus 官方提供了 Pushgateway 作为指标缓存代理。

核心定位与工作原理

Pushgateway 充当批处理任务与 Prometheus Server 之间的中继缓存:

  1. 客户端推送:短作业在执行期间或结束时,通过 HTTP REST API(POST/PUT)将指标主动推送到 Pushgateway。
  2. 指标缓存:Pushgateway 在内存中缓存这些指标数据。
  3. 服务端拉取:Prometheus Server 按照正常的抓取周期,定时从 Pushgateway 的 /metrics 端点拉取(Pull)指标。
text
+-------------------+      HTTP Push
| Batch Job / Script| --------------------> +------------------+
+-------------------+                       |                  |     HTTP Pull     +-------------------+
                                            |   Pushgateway    | <---------------- | Prometheus Server |
+-------------------+                       | (Metrics Buffer) |                   +-------------------+
| Scheduled CronJob | --------------------> +------------------+
+-------------------+

使用注意事项

Pushgateway 虽能解决批处理任务的监控问题,但在架构设计上存在以下局限性:

  • 无自动过期机制:Pushgateway 默认不会自动清理已推送的指标。如果某项任务已废弃,其指标仍会常驻在 Pushgateway 中持续被 Prometheus 拉取,需要通过 API 手动 DELETE 或借助定时清理脚本维护。
  • 单点故障风险:多个作业汇聚于同一 Pushgateway,可能导致其成为单点故障瓶颈。
  • 健康检查丧失:Prometheus 的 up 指标只能反映 Pushgateway 自身的存活,无法反映实际批处理任务的健康状态。

因此,官方建议仅将 Pushgateway 用于临时短作业、批处理作业,常驻服务应直接暴露 /metrics 端点供 Prometheus 拉取。

部署

Docker 部署

使用 Docker 可以快速启动 Pushgateway 实例:

bash
docker run -d \
  --name pushgateway \
  --restart always \
  -p 9091:9091 \
  prom/pushgateway

若需持久化数据(避免重启后指标丢失),可挂载持久化路径并指定 --persistence.file:

bash
docker run -d \
  --name pushgateway \
  --restart always \
  -p 9091:9091 \
  -v /data/pushgateway:/data \
  prom/pushgateway \
  --persistence.file=/data/metric.store \
  --persistence.interval=5m

二进制与 systemd 部署

  1. 从官方 GitHub Releases 下载解压可执行文件:
bash
wget https://github.com/prometheus/pushgateway/releases/download/v1.9.0/pushgateway-1.9.0.linux-amd64.tar.gz
tar -zxvf pushgateway-1.9.0.linux-amd64.tar.gz
sudo mv pushgateway-1.9.0.linux-amd64/pushgateway /usr/local/bin/
  1. 创建 systemd 服务配置文件 /etc/systemd/system/pushgateway.service:
ini
[Unit]
Description=Prometheus Pushgateway
After=network.target

[Service]
User=nobody
Group=nobody
Type=simple
ExecStart=/usr/local/bin/pushgateway \
  --web.listen-address=":9091" \
  --persistence.file="/var/lib/pushgateway/metric.store" \
  --persistence.interval=5m
Restart=on-failure

[Install]
WantedBy=multi-user.target
  1. 启动并启用服务:
bash
sudo mkdir -p /var/lib/pushgateway && sudo chown nobody:nobody /var/lib/pushgateway
sudo systemctl daemon-reload
sudo systemctl enable --now pushgateway
sudo systemctl status pushgateway

Prometheus 抓取配置

在 prometheus.yml 中添加 Pushgateway 的抓取任务。注意建议设置 honor_labels: true,以保留被推送指标自身的标签(避免被 Prometheus 覆盖 job/instance 标签):

yaml
scrape_configs:
  - job_name: 'pushgateway'
    honor_labels: true
    static_configs:
      - targets: ['localhost:9091']

推送测试指标验证

使用 curl --data-binary 发送一条测试指标:

bash
# 推送单个指标到指定 job 和 instance
cat <<EOF | curl --data-binary @- http://localhost:9091/metrics/job/backup_job/instance/node-01
# TYPE backup_last_success_timestamp_seconds gauge
# HELP backup_last_success_timestamp_seconds Last time backup job succeeded
backup_last_success_timestamp_seconds $(date +%s)
# TYPE backup_records_processed counter
backup_records_processed 1024
EOF

访问 http://localhost:9091/metrics 或打开浏览器查看 Pushgateway Web UI(http://localhost:9091),即可看到刚才上报的指标。

若需清理该 job 下推送的指标,调用 DELETE 接口即可:

bash
curl -X DELETE http://localhost:9091/metrics/job/backup_job/instance/node-01