Prometheus06-pushgateway

概述

  • Pushgateway 是 Prometheus 生态中的一个组件,用于接收短生命周期任务主动推送的指标数据,并提供给 Prometheus 抓取。本质作用是解决 Prometheus 无法直接抓取(pull)的监控数据问
  • Pushgateway 不适合长期指标和应用监控。因为Pushgateway 不会自动删除指标。可能导致指标长期存在,数据失真。长期指标应使用exporter

适用情况

  • 在某网络环境中因为防火墙或安全策略限制,服务或应用程序仅允许访问特定端口,使得prommetheus无法直接去拉取各个目标的监控数据
  • 目标资源的生命周期太短,可能只有几秒或几十秒,prometheus来不及抓取。例如,CI/CD Job、Kubernetes Job、定时任务、批处理脚本
  • 无 HTTP 指标端点的任务例如:shell 脚本、ETL任务、数据同步任务、备份脚本。这些任务没有 exporter。可以在脚本里直接:curl push metrics

pushgateway拓扑简图

Application / Batch Job
        │
        │ push metrics
        ▼
   Pushgateway
        │
        │ pull
        ▼
   Prometheus

pushgateway工作原理

  • 工作流程
应用程序
    │
    │ HTTP POST
    ▼
Pushgateway
    │
    │ Prometheus pull
    ▼
Prometheus
  • Pushgateway是一个独立的服务组件,通过HTTP API接收指标,它位于发送指标的应用服务程序和Prometheus服务器之间。
  • 1、应用程序生成监控指标,或者运维人员通过写自己的脚本程序,抓自己想要的监控数据,转换成 K/V 的形式。2、使用 HTTP API 推送到 Pushgateway。3、Pushgateway 暂存指标。4、Prometheus 定期抓取 Pushgateway

优点

  • 实现灵活监控,自定义监控数据
  • 节约资源,exporter采集的数据过多,大部分用不到
  • 支持短生命周期任务监控

二进制安装pushgateway

下载

wget https://github.com/prometheus/pushgateway/releases/download/v1.5.1/pushgateway-1.5.1.linux-amd64.tar.gz
tar -zxf pushgateway-1.5.1.linux-amd64.tar.gz
cp pushgateway /usr/local/bin/
pushgateway --version

启动服务

# 启动。开箱即用,无需配置
pushgateway --web.listen-address="0.0.0.0:9091"

# 访问
http://127.0.0.1:9091

# metrics
http://127.0.0.1:9091/metrics

数据持久化

  • 默认情况下,数据存储在内存
  • 可以开启持久化
pushgateway \
--web.listen-address="0.0.0.0:9091" \
--persistence.file="/tmp/pushgateway.data"
  • 默认持久化间隔5分钟,可修改--persistence.interval。例如:--persistence.interval=1m

Prometheus 集成 Pushgateway

  • 修改prometheus.yml
- job_name: push_pushgateway
  honor_labels: true   # 保持Pushgateway推送时的label,不被Prometheus覆盖
  static_configs:
    - targets:
      - 192.168.24.74:9091

向pushgateway发送指标

echo "batchjob1_user_counter 2" | \
curl --data-binary @- \
http://localhost:9091/metrics/job/job_name/instance/instance_name
  • 说明:job = job_name,instance = instance_name

带 HELP / TYPE 的指标

  • 可以通过在推送中传递TYPE和HELP语句来向监控指标添加类型描述,并向监控指标组添加更多指标
cat <<EOF | curl --data-binary @- http://localhost:9091/metrics/job/some_job/instance/game_server
# TYPE some_metric counter
some_metric{label="label-1"} 52

# HELP another_metric Just an example
# TYPE another_metric gauge
another_metric 2019.113
EOF

删除 Pushgateway 指标

  • 删除整个 job:
curl -X DELETE http://localhost:9091/metrics/job/some_job
  • 删除指定实例:
curl -X DELETE \
http://localhost:9091/metrics/job/some_job/instance/game_server

基于推送的prometheus监控结构图

  • 实际生产中,服务器可能安装有多个exporter,为了减少端口的暴露,可以使用基于推送的监控架构。目的:减少exporter暴露端口
  • 在node_exporter和pushgateway之间添加了一个转发器。转发器获取监控指标并将其发送给pushgateway,Prometheus从pushgateway中获取数据,而不是直接从node_exporter中获取数据”在这里,node_exports仍然监听特定端口以公开监控指标,但只接受内部请求,这样有助于保护节点免受被外部恶意访问。
Node Exporter
      │
      │ metrics
      ▼
  转发脚本
      │
      │ push
      ▼
Pushgateway
      │
      │ pull
      ▼
Prometheus

示例脚本

  • 转发器是两个curl的操作,可以编写为脚本并加入计划任务
cat <<EOF>> /opt/trans.sh

#!/bin/bash

EXPORTER_ADDR=127.0.0.1:9100
PGW_ADDR=127.0.0.1:9091
PGW_JOB=node
PGW_INSTANCE=$(hostname)

curl -s http://$EXPORTER_ADDR/metrics | \
curl --data-binary @- \
http://$PGW_ADDR/metrics/job/$PGW_JOB/instance/$PGW_INSTANCE
EOF
  • 加入计划任务
crontab -e

*/1 * * * * /opt/trans.sh
posted @ 2024-05-10 10:15  立勋  阅读(194)  评论(0)    收藏  举报