深色模式
Pushgateway 短期任务
脚本跑 30 秒就结束,Prometheus 还没来得及拉就没了。Pushgateway 让任务主动把结果推上来。
适用环境
- 已部署 Prometheus
- 有定时批处理 / CI 任务需要监控
bash
# 确认推送目标可达
curl -s http://localhost:9091/-/ready1
2
2
操作步骤
1. 安装并启动 Pushgateway
bash
VERSION=v1.10.0
cd /opt
wget https://github.com/prometheus/pushgateway/releases/download/v${VERSION}/pushgateway-${VERSION}.linux-amd64.tar.gz
tar xf pushgateway-${VERSION}.linux-amd64.tar.gz
cp pushgateway-${VERSION}.linux-amd64/pushgateway /usr/local/bin/
pushgateway &1
2
3
4
5
6
2
3
4
5
6
2. 任务结束前推送指标
bash
# 推送一组指标,group 用 job/instance 标识
echo "
# TYPE batch_rows_total counter
batch_rows_total 12345
# TYPE batch_duration_seconds gauge
batch_duration_seconds 12.5
" | curl -X POST --data-binary @- \
http://localhost:9091/metrics/job/batch_etl/instance/host011
2
3
4
5
6
7
8
2
3
4
5
6
7
8
3. Prometheus 抓取 Pushgateway
yaml
- job_name: pushgateway
honor_labels: true
static_configs:
- targets: ['localhost:9091']1
2
3
4
2
3
4
验证
promql
batch_rows_total{job="batch_etl"}1
常见坑
指标不会自动过期
任务推送后指标常驻,任务不再跑时数值仍是旧的,需配合 up 或删除 API 清理。
别用它做长期服务监控
Pushgateway 是给短期任务用的,长期服务请用普通 pull,否则单点故障丢数据。