Prometheus Pushgateway 安装配置与临时任务监控实践
前言 在运维与开发过程中,Prometheus 监控大盘通常能完整展示长期运行服务的状态,但凌晨执行的备份脚本、几分钟结束的 CI/CD 任务或临时调度的数据处理作业等短生命周期任务,往往因执行完毕即退出而无法被 Prometheus 的拉取(Pull)机制捕获,形成监控盲区。 针对此类场景,Prometheus 官方提供了 Pushgateway 组件。将介绍 Pushgateway 的安装…

前言 在运维与开发过程中,Prometheus 监控大盘通常能完整展示长期运行服务的状态,但凌晨执行的备份脚本、几分钟结束的 CI/CD 任务或临时调度的数据处理作业等短生命周期任务,往往因执行完毕即退出而无法被 Prometheus 的拉取(Pull)机制捕获,形成监控盲区。 针对此类场景,Prometheus 官方提供了 Pushgateway 组件。将介绍 Pushgateway 的安装…

在运维与开发过程中,Prometheus 监控大盘通常能完整展示长期运行服务的状态,但凌晨执行的备份脚本、几分钟结束的 CI/CD 任务或临时调度的数据处理作业等短生命周期任务,往往因执行完毕即退出而无法被 Prometheus 的拉取(Pull)机制捕获,形成监控盲区。
针对此类场景,Prometheus 官方提供了 Pushgateway 组件。本文将介绍 Pushgateway 的安装配置方法,并演示如何将临时任务的执行数据主动推送至 Prometheus,实现全链路监控。
本文演示环境基于 Linux 虚拟机,相关软件版本如下:
tar -zxvf pushgateway-1.11.2.linux-amd64.tar.gz
mv pushgateway-1.11.2.linux-amd64 pushgateway
rm -rf pushgateway-1.11.2.linux-amd64.tar.gz
/etc/systemd/system/pushgateway.service:[Unit]
Description=Pushgateway for Prometheus
Documentation=https://github.com/prometheus/pushgateway
After=network-online.target
[Service]
Type=simple
User=prometheus
Group=prometheus
ExecStart=/app/pushgateway/pushgateway \
--web.listen-address=:9091 \
--web.enable-admin-api \
--log.level=info
WorkingDirectory=/app/pushgateway
Restart=on-failure
RestartSec=5
StandardOutput=journal
StandardError=journal
SyslogIdentifier=pushgateway
[Install]
WantedBy=multi-user.target
sudo chown -R prometheus:prometheus /app/pushgateway
sudo chmod +x /app/pushgateway/pushgateway
sudo systemctl daemon-reload
sudo systemctl start pushgateway
sudo systemctl enable pushgateway
sudo systemctl status pushgateway
ps aux | grep pushgateway
curl http://localhost:9091/metrics
journalctl -u pushgateway -f
sudo firewall-cmd --permanent --add-port=9091/tcp
sudo firewall-cmd --reload
此时访问 http://<ip>:9091 即可看到 Pushgateway Web UI。访问 http://<ip>:9091/metrics 可查看其自身暴露的 Go 运行时及进程指标。
使用官方镜像快速部署:
docker pull prom/pushgateway
docker run -d --name=pg -p 9091:9091 prom/pushgateway
启动后访问 http://<ip>:9091 即可。
编辑 Prometheus 配置文件 prometheus.yml,添加对应的 scrape job:
scrape_configs:
- job_name: 'pushgateway'
honor_labels: true
static_configs:
- targets: ['localhost:9091']
labels:
app: "pushgateway"
注意:必须配置
honor_labels: true,否则 Pushgateway 推送的job和instance标签会被 Prometheus 覆盖。
重启 Prometheus 服务:
systemctl restart prometheus
systemctl status prometheus
访问 Prometheus UI (http://<ip>:9090) 的 Targets 页面,确认 Pushgateway 状态为 UP。
Pushgateway 提供标准 HTTP API 用于接收指标。默认推送 URL 格式为:
http://<pushgateway-addr>:9091/metrics/job/{JOB_NAME}{/LABEL_NAME/LABEL_VALUE...}
其中 job 为必填项,后续可追加任意标签对(如 instance/<INSTANCE_NAME>)以区分数据来源。
echo "test_metric 123456" | curl --data-binary @- http://192.168.42.140:9091/metrics/job/test_job
推送成功后,Pushgateway 会自动生成 push_time_seconds 和 push_failure_time_seconds 指标。在 Prometheus UI 中即可查询到 test_metric。
cat <<EOF | curl --data-binary @- http://192.168.42.140:9091/metrics/job/some_job/instance/some_instance
# TYPE some_metric counter
some_metric{label="val1"} 42
# TYPE another_metric gauge
# HELP another_metric Just an example.
another_metric 2398.283
EOF
curl -X DELETE http://192.168.42.140:9091/metrics/job/some_job/instance/some_instance
curl -X DELETE http://192.168.42.140:9091/metrics/job/some_job
模拟备份任务并推送耗时与状态指标:
#!/bin/bash
JOB_NAME="daily_backup"
INSTANCE="server01"
PUSHGATEWAY_URL="http://localhost:9091"
start_time=$(date +%s)
echo "Starting backup..."
sleep 3
backup_success=1 # 1 表示成功,0 表示失败
end_time=$(date +%s)
duration=$((end_time - start_time))
cat <<EOF | curl --data-binary @- ${PUSHGATEWAY_URL}/metrics/job/${JOB_NAME}/instance/${INSTANCE}
# HELP backup_duration_seconds Duration of the backup job in seconds
# TYPE backup_duration_seconds gauge
backup_duration_seconds ${duration}
# HELP backup_success Whether the backup succeeded (1) or failed (0)
# TYPE backup_success gauge
backup_success ${backup_success}
EOF
echo "Metrics pushed to Pushgateway."
执行后,Prometheus 中可查询到:
backup_duration_seconds{job="daily_backup", instance="server01"} 3
backup_success{job="daily_backup", instance="server01"} 1
import requests
import time
def push_metrics(job, instance, records_processed, success):
metrics = f"""# HELP data_records_processed Number of records processed
# TYPE data_records_processed gauge
data_records_processed {records_processed}
# HELP data_job_success Job success status (1 = success, 0 = failure)
# TYPE data_job_success gauge
data_job_success {int(success)}"""
url = f"http://localhost:9091/metrics/job/{job}/instance/{instance}"
response = requests.post(url, data=metrics.encode('utf-8'))
if response.status_code == 202:
print("Metrics pushed successfully.")
else:
print(f"Failed to push metrics: {response.status_code}")
# 模拟任务执行
start = time.time()
try:
records = 1500
time.sleep(2)
success = True
except Exception as e:
records = 0
success = False
push_metrics(
job="data_pipeline",
instance="worker-node-01",
records_processed=records,
success=success
)
执行后,Prometheus 中将生成对应的 data_records_processed 与 data_job_success 指标。
Pushgateway 作为 Prometheus 生态的补充组件,通过'推送 + 中转'机制有效解决了短生命周期任务无法被拉取监控的痛点。在实际应用中,只需合理配置 job 与 instance 标签,即可将 Shell 脚本、Python 批处理等临时作业的执行状态、耗时及业务指标无缝接入 Prometheus 监控体系。配合规范的指标清理策略,可确保监控数据的准确性与时效性。

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML 转 Markdown 互为补充。 在线工具,Markdown 转 HTML在线工具,online
将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML 转 Markdown在线工具,online
通过删除不必要的空白来缩小和压缩JSON。 在线工具,JSON 压缩在线工具,online