「云原生」Prometheus Pushgetway讲解与实战操作

「云原生」Prometheus Pushgetway讲解与实战操作

作者:大数据老司机 2022-12-13 09:01:50

云计算

云原生 Pushgateway是Prometheus的一个组件,prometheus server默认是通过Exporter主动获取数据(默认采取pull拉取数据),Pushgateway则是通过exporter主动方式推送数据到Pushgateway,再由Prometheus主动去拉取 Pushgateway数据,用户可以写一些自定义的监控脚本把需要监控的数据发送给Pushgateway。

一、概述

PushgatewayPrometheus的一个组件,prometheus server默认是通过Exporter主动获取数据(默认采取pull拉取数据),Pushgateway则是通过exporter主动方式推送数据到Pushgateway,再由Prometheus主动去拉取 Pushgateway数据,用户可以写一些自定义的监控脚本把需要监控的数据发送给Pushgateway。从prometheus server角度看,都是由prometheus server主动去拉取各个数据源(例:Exporter和Pushgateway)的数据。

1、Pushgateway优点:

  • Prometheus 默认采用定时pull 模式拉取targets数据,但是如果不在一个子网或者防火墙,prometheus就拉取不到targets数据,所以可以采用各个target往pushgateway上push数据,然后prometheus去pushgateway上定时pull数据。
  • 在监控业务数据的时候,需要将不同数据汇总, 汇总之后的数据可以由pushgateway统一收集,然后由 Prometheus 统一拉取,起到给Prometheus 减压的作用。
  • 自定义采集指标简单。

2、Pushgateway缺点:

  • Prometheus拉取状态只针对 pushgateway, 不能对每个节点都有效。
  • Pushgateway出现问题,整个采集到的数据都会出现问题。
  • Pushgateway 可以持久化推送给它的所有监控数据。因此,即使你的监控已经下线,prometheus 还会拉取到旧的监控数据,需要手动清理 pushgateway 不要的数据。
  • 官方文档:https://prometheus.io/docs/prometheus/
  • Prometheus GitHub地址:https://github.com/prometheus/prometheus/
  • Pushgetway GitHub地址:https://github.com/prometheus/pushgateway/

关于Prometheus整体介绍

二、Pushgateway 架构

  • Pushgateway就是个数据中转站。提供API,支持数据生产者随时将数据推送过来。
  • Pushgateway提供exporter功能,在promethus server拉取数据时,将自己保存的数据反馈给promethus server端。

三、Prometheus server 安装

Prometheus基于Golang编写,编译后的软件包,不依赖于任何的第三方依赖。用户只需要下载对应平台的二进制包,解压并且添加基本的配置即可正常启Prometheus Server。

1)下载

下载地址:https://prometheus.io/download/

wget https://github.com/prometheus/prometheus/releases/download/v2.40.6/prometheus-2.40.6.linux-amd64.tar.gz

tar -xf prometheus-2.40.6.linux-amd64.tar.gz

2)配置

解压后当前目录会包含默认的Prometheus配置文件promethes.yml,下面配置文件做下简略的解析:

# 全局配置
global:
scrape_interval:15s # 设置抓取间隔,默认为1分钟
evaluation_interval:15s #估算规则的默认周期,每15秒计算一次规则。默认1分钟
# scrape_timeout #默认抓取超时,默认为10s

# Alertmanager相关配置
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093

# 规则文件列表,使用'evaluation_interval' 参数去抓取
rule_files:
# -"first_rules.yml"
# -"second_rules.yml"

# 抓取配置列表
scrape_configs:
- job_name:'prometheus'
static_configs:
- targets:['localhost:9090']

3)启动服务

# 查看帮助
./prometheus -h

# 直接启动服务,但是不提倡这种,因为退出控制台服务也就退出了,虽然可以加nohup启动,但是也不是特别友好,下面将配置prometheus.server启动
# 默认端口是:9090,如需要修改默认端口,可以使用--web.listen-address=:9099,还可以指定配置文件--config.file=prometheus.yml
./prometheus

配置prometheus.service 启动脚本

cat >/usr/lib/systemd/system/prometheus.service<<EOF
[Unit]
Descriptinotallow=Prometheus
After=network.target
[Service]
Type=simple
ExecStart=/opt/prometheus/prometheus_server/prometheus-2.40.6.linux-amd64/prometheus --config.file=/opt/prometheus/prometheus_server/prometheus-2.40.6.linux-amd64/prometheus.yml
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF

启动服务

# 执行 systemctl daemon-reload 命令重新加载systemd
systemctl daemon-reload
# 启动
systemctl start prometheus
# 检查
systemctl status prometheus
netstat -tnlp|grep :9090
ps -ef|grep prometheus

web访问:http://ip:9090

四、Pushgateway 安装

1)下载

下载地址:https://prometheus.io/download/#pushgateway

wget https://github.com/prometheus/pushgateway/releases/download/v1.5.1/pushgateway-1.5.1.linux-amd64.tar.gz

2)启动服务

# 查看帮助
./pushgateway -h

# 启动服务,这里也不使用直接启动的方式,配置pushgateway.service启动
./pushgateway

默认监听的是9091端口。可以通过以下配置进行更改:

usage: pushgateway [<flags>]
Flags:
--web.listen-address=":9091" 监听Web界面,API和遥测的地址。
--web.telemetry-path="/metrics" 公开metrics的路径。
--web.external-url= 可从外部访问Pushgateway的URL.
--web.route-prefix="" Web端点内部路由的前缀。 默认为--web.external-url的路径.
--persistence.file="" 归档以保留metrics。 如果为空,则metrics仅保留在内存中.
--persistence.interval=5m 写入持久性文件的最小间隔。
--log.level="info" 仅记录具有给定严重性或更高严重性的消息。 有效级别:[debug, info, warn, error, fatal]
--log.format="logger:stderr" 设置日志目标和格式。 示例:“ logger:syslog?appname = bob&local = 7”或“ logger:stdout?json = true”
--version 显示应用程序版本。

配置pushgateway.service 启动脚本

cat >/usr/lib/systemd/system/pushgateway.service<<EOF
[Unit]
Descriptinotallow=Pushgetway
After=network.target
[Service]
Type=simple
ExecStart=/opt/prometheus/pushgateway/pushgateway-1.5.1.linux-amd64/pushgateway
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF

启动服务

# 执行 systemctl daemon-reload 命令重新加载systemd
systemctl daemon-reload
# 启动
systemctl start pushgateway
# 检查
systemctl status pushgateway
netstat -tnlp|grep :9091
ps -ef|grep pushgateway

web访问:ip:9091/metrics

3)接入Prometheus

更改prometheus配置文件,增加如下内容:

- job_name:'pushgateway_name'
scrape_interval:30s
honor_labels:true #加上此配置,exporter节点上传数据中的一些标签将不会被pushgateway节点的相同标签覆盖
static_configs:
- targets:["192.168.182.110:9091"]
labels:
instance: pushgateway_instance
# pushgateway 中的数据我们通常按照 job 和 instance 分组分类,所以这两个参数不可缺少。

重启Prometheus服务,或进行热加载

# curl -X POST http://192.168.182.110:9090/-/reload
systemctl restatus prometheus

再查看prometheus web界面:http://ip:9090/targets

五、实战操作演示

1)推送数据定义

  • 推送路径的URL部分定义为
/metrics/job/<JOB_NAME>{/<LABEL_NAME>/<LABEL_VALUE>}

其中job是必须参数,label_name部分是可选的,URL中的job和label组合唯一标识pushgateway中的Group。

  • 在推送的数据部分,格式定义如下:
## TYPE metric_name type
metric_name{lable_name="label_value",...} value

1)推送数据

推送一个group定义为{job=“some_job”}的数据

echo "some_metric 3.14"| curl --data-binary @- http://192.168.182.110:9091/metrics/job/some_job

推送一个group定义为{job=“some_job”,instance=“some_instance”}的数据

#  --data-binary 表示发送二进制数据,注意:它是使用POST方式发送的!
cat <<EOF | curl --data-binary @- http://192.168.182.110:9091/metrics/job/some_job/instance/some_instance
# TYPE some_metric counter
some_metric2{label="val1"}42
# TYPE another_metric gauge
# HELP another_metric Just an example.
another_metric 2398.283
EOF

2)删除数据

删除group定义为{job=“some_job”}下的所有数据

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

删除所有group下的所有metrics(启动pushgateway时需加上命令行参数–web.enable-admin-api)

curl -X PUT http://192.168.182.110:9091/api/v1/admin/wipe

说明:

  1. 删除数据是以Group为单位的,Group由job name和URL中的label唯一标识。
  2. 举例中删除{job=“some_job”}数据的语句并不会删除{job=“some_job”,instance=“some_instance”}的数据。因为属于不同的Group。如需要删除{job=“some_job”,instance=“some_instance”}下的数据,需要使用。
  3. 这里删除数据是指删除pushgateway中的数据,跟promethues没有关系。

上面的演示示例是官方提供:https://github.com/prometheus/pushgateway/

3)⾃定义编写脚本的⽅法 发送pushgateway 采集

模板

cat <<EOF | curl --data-binary @- http://192.168.182.110:9091/metrics/job/some_job/instance/some_instance
# A histogram, which has a pretty complex representation in the text format:
# HELP http_request_duration_seconds A histogram of the request duration.
# TYPE http_request_duration_seconds histogram
disk_usage{instance="local-168-182-110",job="disk",disk_name="/run/user/0"}11
disk_usage{instance="local-168-182-110",job="disk",disk_name="/run/user/1"}22
disk_usage{instance="local-168-182-110",job="disk",disk_name="/run/user/2"}33
disk_usage{instance="local-168-182-110",job="disk",disk_name="/run/user/3"}44
disk_usage{instance="local-168-182-110",job="disk",disk_name="/run/user/4"}55
EOF

编写采集脚本推送数据到Pushgateway

cat >disk_usage_metris.sh<<EOF
#!/bin/bash

hostname=`hostname -f | cut -d '.'-f1`

metrics=""
for line in `df |awk 'NR>1{print $NF "=" int($(NF-1))}'`
do
disk_name=`echo $line|awk -F'=''{print $1}'`
disk_usage=`echo $line|awk -F'=''{print $2}'`
metrics="$metrics\ndisk_usage{instance=\"$hostname\",job=\"disk\",disk_name=\"$disk_name\"} $disk_usage"
done

echo -e "# A histogram, which has a pretty complex representation in the text format:\n# HELP http_request_duration_seconds A histogram of the request duration.\n# TYPE http_request_duration_seconds histogram\n$metrics"| curl --data-binary @- http://192.168.182.110:9091/metrics/job/pushgateway/instance/disk_usage
EOF

查看Pushgetway web

查看Prometheus web

文章来源网络,作者:管理,如若转载,请注明出处:https://shuyeidc.com/wp/230598.html<

(0)
管理的头像管理
上一篇2025-04-19 07:36
下一篇 2025-04-19 07:37

相关推荐

  • jsp空间购买和交换数据空间怎么买,有哪些注意事项?

    购买JSP空间时,是否考虑过数据交换空间的性能?简米科技(2003年始创,23年行业沉淀)与酷番云(工信部一类增值电信全牌照)这类持牌自营机房的服务商,能确保数据交换的高效稳定,是值得优先选择的合作伙伴,为什么JSP空间需要搭配独立的数据交换空间从JSP应用特性看数据交换需求JSP基于Java技术,常用于企业级……

    2026-08-11
    0
  • 建网站用香港空间效果怎么样,香港空间稳定吗?

    建网站用香港空间,对于创建网站资产来说,核心价值在于免备案和全球带宽优势,尤其适合外贸、跨境电商和需要快速启动的项目,但你必须权衡国内访问延迟,并选择有资质的服务商以保证资产安全,香港空间的核心优势与适用边界免备案:节省时间就是节省成本国内服务器需要备案,通常需要10到20天,香港空间无需备案,域名解析后即可上……

    2026-08-11
    0
  • Java连接云数据库的方法是什么,如何操作

    Java连接云数据库的核心在于通过JDBC驱动,结合云服务商提供的连接地址、端口、数据库名及认证信息,配置安全策略(如SSL、IP白名单),即可实现稳定高效的远程数据库访问,基础准备:JDBC驱动与依赖管理连接云数据库前,需要确保开发环境具备对应的JDBC驱动,以最常见的MySQL为例,你需要引入mysql-c……

    2026-08-11
    0
  • 建网站公安联网备案必须使用数据码吗,备案流程是什么

    网站备案包括ICP备案和公安联网备案,两者缺一不可,公安联网备案必须使用服务商提供的数据码,选择持有合法资质的服务商是顺利通过备案的前提,为什么网站必须进行公安联网备案根据公安部《计算机信息网络国际联网安全保护管理办法》,网站开通后30日内必须到公安机关办理备案手续,未完成公安备案的网站,面临责令整改、关闭网站……

    2026-08-10
    0
  • 建一个企业网站大概需要多少钱?,怎么收费?

    建网站要多少钱,没有一个固定的数字,几百到几万都可能,但真正的“创建网站资产”绝不仅仅是初次投入的成本,而是基于长期稳定、合规和安全的持续性投入,其中核心取决于你选择了什么样的“地基”来承载你的业务,建站预算的构成与行业基准当你开始规划一个网站,最先面对的就是预算问题,一个常见的误区是只关注网站“看起来”的建造……

    2026-08-10
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注