Pushgateway 基本概念

Pushgateway 是采用被动推送的方式,而不是类似于prometheus server 主动连接 exporter获取监控数据

pushgateway 可以单独运行在一个节点,然后需要自定义监控脚本把需要监控的主动推送给pushgateway的API接口,然后 pushgateway 等待 prometheus server 抓取数据,即 pushgateway 本身没有任何抓取监控数据的功能,目前 pushgateway 只是被动的等待数据从客户端推送过来

–persistence.file=”” #数据保存的文件,默认只保存在内存中

–persistence.interval=5m #数据持久化的间隔时间

部署 pushgateway

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# docker 部署
docker run -d --name pushgateway -p 9091:9091 prom/pushgateway

# 二进制安装
cd /apps
wget https://github.com/prometheus/pushgateway/releases/download/v1.4.2/pushgateway-1.4.2.linux-amd64.tar.gz
tar zxf pushgateway-1.4.2.linux-amd64.tar.gz

# 编写 centos7 控制脚本

cat > /etc/systemd/system/pushgateway.service << EOF
[Unit]
Description=pushgateway
After=network.target

[Service]
Restart=on-failure
WorkingDirectory=/apps/pushgateway
ExecStart=/apps/pushgateway/pushgateway # 如果需要持久化pushgateway数据,可以加上--persistence.file="" 和--persistence.interval=5m 两个参数

[Install]
WantedBy=multi-user.target
EOF

prometheus 到 pushgateway 采集数据

验证 pushgateway

1
curl 192.168.15.100:9091/metrics

prometheus 配置数据采集

vim prometheus-cfg.yaml

1
2
3
4
5
- job_name: 'pushgateway-monitor'
scrape_interval: 5s
static_configs:
- targets: ['192.168.15.100:9091']
honor_labels: true
  • honor_labels 控制 Prometheus 如何处理已经存在于已抓取数据中的标签与 Prometheus 将附加服务器端的标签之间的冲突(”job”和”instance”标签,手动配置的目标标签以及服务发现实现生成的标签)
  • 如果 honor_labels 设置为 “true”,则通过保留已抓取数据的标签值并忽略冲突的服务器端标签来解决标签冲突
  • 如果 honor_labels 设置为 “false”,则通过将已抓取数据中的冲突标签重命名为 “exported_“ (例如 “exported_instance”, “exported_job”) 然后附加服务器端标签来解决标签冲突
1
2
3
4
5
6
kubectl apply -f prometheus-cfg.yaml
kubectl delete -f prometheus-deploy.yaml
kubectl applt -f prometheus-deploy.yaml

## 验证一下数据
查看 prometheus 在 target 中是否存在 pushgateway

测试从客户端推送单条数据

Push 数据到 Pushgateway 中,可以通过其提供的 API 标准接口来添加,默认的 URL 地址为:

http://:9091/metrics/job/{//},

其中 是必填项,为 job 标签值,后边可以跟任何数量的标签对,一般我们会添加一个 instance/ 实例名称标签,来方便区分各个指标

推送一个 job 名称为 mytest_job ,key 为 mytest_metric 值为 2022

1
2
3
echo "mytest_metric 2022" | curl --data-binary @- http://192.168.3.100:9091/metrics/job/mytest_job

echo "mytest_metric 2333" | curl --data-binary @- http://192.168.3.100:9091/metrics/job/mytest_job
pushgateway 验证数据
1
2
3
4
5
6
# 192.168.15.100/#  在 web 界面查看 

除了 mytest_metric 外,同时还新增了 push_time_seconds 和 push_failure_time_seconds 两个标签,这两个是 Pushgateway 自动生成的指标,分别用于记录指标数据的成功上传时间和失败上传时间

# 查看 192.168.15.100:9091/metrics
搜索关键字:mytest ,会发现有此指标
prometheus server 验证数据

在 Graph 页面查询 mytest_metric 可以看到图标即是有数据的

测试从客户端推送多条数据

推送多条数据
1
2
3
4
5
6
cat <<EOF | curl --date-binary @- http://192.168.15.100:9091/metrics/job/test_job/instance/192.168.15.101
#TYPE node_memory_usage gauge
node_memory_usage 4311744512
# TYPE memory_total gauge
node_memory_total 103481868288
EOF
pushgateway 验证数据
1
# 192.168.15.100/#  在 web 界面查看 
prometheus server

在 Graph 页面查询 node_memory_total 可以看到图标即是有数据的

自定义收集数据

基于自定义脚本实现数据的收集和推送

自定义脚本 vim mem_monitor.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash

total_memory=$(free|awk '/Mem/{print $2}')
used_memory=$(free |awk '/Mem/{print $3}')

job_name="custom_memory_monitor"
instance_name=`ifconfig eth0 | grep -w inet | awk '{print $2}'`
pushgateway_server="htpp://192.168.15.100:9091/metrics/job"

cat <<EOF | curl --date-binary @- ${pushgateway_server}/${job_name}/instance/${instance_name}
#TYPE custom_memory_total gauge
custom_memory_total $total_memory
#TYPE custom_memory_used gauge
custom_memory_used $used_memory
EOF

分别在不同主机执行脚本,验证指标数据收集和推送

1
bash mem_monitor.sh
Pushgateway 验证数据
1
192.168.15.100:9091/#  查看数据
prometheus 验证数据

在 Graph 页面查询 custom_memory_total 可以看到图标即是有数据的

删除数据

先对一个组写入多个 instance 的数据

1
2
3
4
5
6
7
8
9
10
11
12
13
cat <<EOF | curl --date-binary @- http://192.168.15.100:9091/metrics/job/test_job/instance/192.168.15.101
#TYPE node_memory_usage gauge
node_memory_usage 4311744512
# TYPE memory_total gauge
node_memory_total 103481868288
EOF

cat <<EOF | curl --date-binary @- http://192.168.15.100:9091/metrics/job/test_job/instance/192.168.15.102
#TYPE node_memory_usage gauge
node_memory_usage 4311744512
# TYPE memory_total gauge
node_memory_total 103481868288
EOF
通过 API 删除指定组内指定实例的数据
1
2
3
curl -X DELETE http://192.168.15.100:9091/metrics/job/test_job/instance/192.168.15.101

# web浏览 192.168.15.100:9091/#
通过 web 界面删除

直接在数据右边点 Delete Group