@Pushgateway配置与使用

发布于:2025-05-31 ⋅ 阅读:(23) ⋅ 点赞:(0)

Pushgateway配置与使用

Pushgateway是Prometheus生态中的重要组件,用于解决Prometheus无法直接拉取目标数据的场景。以下是Pushgateway的完整配置与使用标准操作流程。

一、Pushgateway简介与适用场景

1.1 基本概念

Pushgateway是一个独立服务,位于应用程序和Prometheus服务器之间,接收应用程序推送的指标数据,然后由Prometheus服务器拉取这些数据。

1.2 使用场景

  • 当目标与Prometheus不在同一子网或有防火墙限制时
  • 需要汇总不同业务数据统一收集时
  • 临时性任务(如批处理作业)需要监控时
  • 现有exporter无法满足需求,需自定义监控数据时

1.3 主要弊端

  • 单点故障影响范围大
  • Prometheus只能监控Pushgateway状态,无法直接监控各目标节点
  • 数据会持久化,需手动清理过期数据

二、Pushgateway安装部署(推荐全使用Docker Compose安装,更方便些)

2.1 二进制安装方式(可选)

# 下载最新版本
wget https://github.com/prometheus/pushgateway/releases/download/v1.0.0/pushgateway-1.0.0.linux-amd64.tar.gz

# 解压并安装
tar xzvf pushgateway-1.0.0.linux-amd64.tar.gz -C /usr/local/
mv /usr/local/pushgateway-1.0.0.linux-amd64 /usr/local/pushgateway

# 启动(带持久化)
nohup /usr/local/pushgateway/pushgateway --persistence.file="pushgateway.data" &

2.2 Docker安装方式(可选)

docker pull prom/pushgateway
docker run -d -p 9091:9091 --name pushgateway prom/pushgateway

2.3 系统服务配置(可选)

# 创建服务文件
cat > /usr/lib/systemd/system/pushgateway.service <<EOF
[Unit]
Description=pushgateway
After=network.target

[Service]
Type=simple
WorkingDirectory=/usr/local/pushgateway
ExecStart=/usr/local/pushgateway/pushgateway --persistence.file=/usr/local/pushgateway/data/pushgateway.data
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

# 启动服务
systemctl enable pushgateway
systemctl start pushgateway

三、Prometheus配置

3.1 修改prometheus.yml

- job_name: 'pushgateway'
  honor_labels: true  # 重要:保留原始job和instance标签
  static_configs:
    - targets: ['pushgateway_ip:9091']
      labels:
        instance: pushgateway

3.2 重新加载配置

curl -X POST http://localhost:9090/-/reload

四、数据推送方法

4.1 使用curl推送简单数据

# 基本格式
echo "metric_name metric_value" | curl --data-binary @- http://pushgateway_ip:9091/metrics/job/job_name

# 示例
echo "some_metric 3.14" | curl --data-binary @- http://192.168.1.144:9091/metrics/job/some_job

4.2 推送带instance的复杂数据

cat <<EOF | curl --data-binary @- http://192.168.1.144: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

4.3 使用Python SDK推送

from prometheus_client import CollectorRegistry, Gauge, push_to_gateway

registry = CollectorRegistry()
g = Gauge('job_last_success_unixtime', 
          'Last time a batch job successfully finished', 
          registry=registry)
g.set_to_current_time()
push_to_gateway('http://192.168.1.144:9091', 
                job='batchA', 
                registry=registry)

4.4 使用Shell脚本监控并推送

#!/bin/sh
FILENUM=$(ls -l /data | sed 1d | wc -l)
echo "data_file_num ${FILENUM}" | curl --data-binary @- http://192.168.1.144:9091/metrics/job/test_job/instance/test

4.5 定时任务配置

# 添加定时任务
crontab -e
*/1 * * * * /bin/sh /opt/file_num.sh >/dev/null 2>&1

五、数据管理

5.1 删除特定实例数据

curl -X DELETE http://pushgateway_ip:9091/metrics/job/some_job/instance/some_instance

5.2 删除整个job数据

curl -X DELETE http://pushgateway_ip:9091/metrics/job/some_job

六、告警规则配置

6.1 创建告警规则文件

groups:
- name: pushgateway
  rules:
  - alert: DataFileNum
    expr: data_file_num > 9
    for: 0m
    labels:
      severity: warning
    annotations:
      summary: "Data directory has too many files ({{ $value }})"

6.2 在prometheus.yml中引用规则文件

rule_files:
  - 'pushgateway.yml'

七、安全与持久化

7.1 数据持久化

启动时添加参数:

--persistence.file="pushgateway.data"  # 数据文件路径
--persistence.interval=5m             # 持久化间隔

7.2 安全配置(阿里云方案)

  • 使用JWT鉴权协议保护数据安全
  • 在请求头中添加Token:
    curl -H "Authorization: Bearer your_token" ...
    
  • 或使用Basic Auth:
    curl --user "admin:your_token" ...
    

八、验证与监控

  1. 访问Pushgateway UI:http://pushgateway_ip:9091
  2. 在Prometheus UI的Targets页面检查Pushgateway状态
  3. 在Prometheus Graph页面查询推送的指标
  4. 在Grafana中创建监控面板

九、最佳实践

  1. 为每个任务使用唯一的job名称
  2. 为每个实例添加instance标签
  3. 设置合理的持久化间隔
  4. 定期清理不再使用的指标数据
  5. 监控Pushgateway本身的状态和性能
  6. 考虑高可用部署方案

通过以上SOP,您可以完整地配置和使用Pushgateway来实现各种监控场景的需求。


网站公告

今日签到

点亮在社区的每一天
去签到