由于业务需要,node_exporter 无法满足全部功能,故调用 Prometheus 的自带组件 Pushgateway 进行数据的汇总,将原先的 pull 改为 push 进行数据的上报。
- 下载 Pushgateway (https://prometheus.io/download/#pushgateway)
tar -xvf pushgateway-0.7.0.linux-amd64.tar.gz
- 在 prometheus.yml 中添加新job
- job_name: 'push-metrics'
static_configs:
- targets: ['localhost:9999']
(targets的目标地址根据实际部署填写)
- 启动 Pushgateway
./pushgateway
- 启动 Prometheus
./prometheus
- 编写规范的上报规则数据
正常情况我们会使用 Client SDK 推送数据到 pushgateway, 但是我们还可以通过 API 来管理, 例如:
向 {job="some_job"} 添加单条数据:
echo "some_metric 3.14" | curl --data-binary @- http://pushgateway.example.org:9091/metrics/job/some_job
添加更多更复杂数据,通常数据会带上 instance, 表示来源位置:
cat <<EOF | curl --data-binary @- http://pushgateway.example.org: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
删除某个组下的某实例的所有数据:
curl -X DELETE http://pushgateway.example.org:9091/metrics/job/some_job/instance/some_instance
删除某个组下的所有数据:
curl -X DELETE http://pushgateway.example.org:9091/metrics/job/some_job
可以发现 pushgateway 中的数据我们通常按照 job 和 instance 分组分类,所以这两个参数不可缺少。
因为 Prometheus 配置 pushgateway 的时候,也会指定 job 和 instance, 但是它只表示 pushgateway 实例,不能真正表达收集数据的含义。所以在 prometheus 中配置 pushgateway 的时候,需要添加 honor_labels: true 参数, 从而避免收集数据本身的 job 和 instance 被覆盖。
注意,为了防止 pushgateway 重启或意外挂掉,导致数据丢失,我们可以通过 -persistence.file 和 -persistence.interval 参数将数据持久化下来。
参考 https://github.com/prometheus/pushgateway/blob/master/README.md
网友评论