TL;DR

场景:Rocky Linux(类 CentOS)上给 Prometheus 增加主机指标与短任务指标采集。

结论:常驻服务用 node_exporter(pull),短任务/批处理用 pushgateway(push→pull);Pushgateway 需处理”过期数据”和”单点”问题。

产出:node_exporter-1.8.2 与 pushgateway-1.10.0 的安装启动流程、Prometheus job 配置、常见故障定位修复卡。

Node Exporter

下载配置

cd /opt/software
wget https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz

解压配置

cd /opt/software
tar -zxvf node_exporter-1.8.2.linux-amd64.tar.gz
mv node_exporter-1.8.2.linux-amd64 ../servers/

启动服务

cd /opt/servers/node_exporter-1.8.2.linux-amd64
./node_exporter

PushGateway

基本介绍

Prometheus Pushgateway 是一个专门设计的中间组件,用于帮助 Prometheus 监控短期任务和批处理任务。

在标准的 Prometheus 监控体系中,Prometheus 服务器采用主动拉取(pull)模型,通过定期从各被监控服务的 HTTP 端点(通常是/metrics)获取指标数据。这种模式适用于长期运行的守护进程和服务。

然而,对于一些特殊场景:

  1. 短期运行任务:例如一次性执行的脚本、定时任务(cron job)
  2. 批处理作业:如 ETL 流程、数据分析任务等
  3. 无法直接暴露指标的服务:某些运行在受限环境中的任务

Pushgateway 的工作机制

  • 任务在启动时或运行期间将指标数据推送到 Pushgateway
  • Pushgateway 会持久化存储这些指标
  • Prometheus 服务器像监控常规目标一样,定期从 Pushgateway 拉取这些指标
  • 指标会一直保留在 Pushgateway 中,直到被新数据覆盖或手动删除

注意事项

  • 持久性问题:Pushgateway 默认不持久化数据,重启后数据会丢失
  • 数据过时问题:Pushgateway 适合单次批量数据推送,需要推荐通过 push_time_seconds 标签来跟踪数据的推送时间
  • 避免泛用:Pushgateway 适用于短期任务,不建议用于长期任务监控

Pushgateway 下载配置

cd /opt/software
wget https://github.com/prometheus/pushgateway/releases/download/v1.10.0/pushgateway-1.10.0.linux-amd64.tar.gz

tar -zxvf pushgateway-1.10.0.linux-amd64.tar.gz
mv pushgateway-1.10.0.linux-amd64 ../servers/

配置服务

cp pushgateway ../prometheus-2.53.2.linux-amd64/
chmod +x pushgateway

还需要修改配置文件 prometheus.yml 将 pushgateway 配置进去。

使用场景

  • CI/CD 流程中的指标监控:Jenkins、GitLab CI 等工具的管道中用于推送编译、测试、发布的状态数据

  • 自动化脚本和批处理任务:自动化作业完成后推送成功或失败状态

    • 每日定时执行的数据库备份脚本
    • 周期性运行的日志清理任务
  • 批量数据处理作业:Spark、Flink 等完成特定任务后,将关键的作业数据推送到 Pushgateway

错误速查

症状根因定位修复
Prometheus Targets 显示 DOWN(node_exporter)进程没起来/已退出机器上看进程与监听端口重新启动 node_exporter;建议改为 systemd 托管
Targets DOWN 但本机能 curl 到 /metricsPrometheus 到目标网络不通Prometheus 机器侧对目标 ip:port 连通性检查放通端口;同网段/安全组策略调整
Targets 反复 UP/DOWN端口冲突或进程不稳定查看启动日志与系统日志换端口或停止冲突服务
node_exporter 启动失败:permission denied文件没执行权限/下载损坏ls -l 看权限chmod +x node_exporter
看不到预期作业指标没有任何作业 push 指标到 pushgateway访问 Pushgateway 的 /metrics在作业结束时 push 指标
面板长期显示旧数据(Pushgateway)Pushgateway 指标不会自动过期删除观察指标的 push 时间建立清理机制
Prometheus 只看到 Pushgateway 的 UPUP 语义只覆盖 Pushgateway 服务本身对比 Pushgateway target 状态与作业指标用作业侧自定义成功/失败指标