一、Pushgateway介紹
Pushgateway是一個獨立的服務,Pushgateway位於應用程序發送指標和Prometheus服務器之間。Pushgateway接收指標,然后將其作為目標被Prometheus服務器拉取。可以將其看作代理服務,或者與blackbox exporter的行為相反, 它接收度量,而不是探測它們。
Pushgateway 是 Prometheus 生態中一個重要工具,使用它的原因主要是:
- Prometheus 采用 pull 模式,可能因為網絡限制,導致 Prometheus 無法直接拉取各個 target 數據。
- 在監控業務數據的時候,需要將不同數據匯總, 由 Prometheus 統一收集。
由於以上原因,不得不使用 pushgateway,但它存在一些弊端:
- 將多個節點數據匯總到 pushgateway, 如果 pushgateway 掛了,受影響比多個 target 大。
- Prometheus 拉取狀態 up 只針對 pushgateway, 無法做到對每個節點有效。
- Pushgateway 可以持久化推送給它的所有監控數據。默認是存放在內存中,可以通過配置持久化。
因此,即使你的監控已經下線,prometheus 還會拉取到舊的監控數據,需要手動清理 pushgateway 不要的數據。
二、部署配置
1、主機安裝:
[root@master soft]# wget https://github.com/prometheus/pushgateway/releases/download/v1.1.0/pushgateway-1.1.0.linux-amd64.tar.gz [root@master soft]# tar xf pushgateway-1.1.0.linux-amd64.tar.gz [root@master soft]# mv pushgateway-1.1.0.linux-amd64 /opt/prometheus/pushgateway [root@master pushgateway]# ./pushgateway --help [root@master pushgateway]# nohup ./pushgateway --persistence.file="pushgateway.data" & #持久化存儲,默認監聽端口9091
安裝完成后,瀏覽器訪問: http://192.168.42.128:9091/#
2、docker安裝:
[root@node1 ~]# docker pull prom/pushgateway [root@node1 ~]# docker run -d --name="pushgateway" -p 9091:9091 prom/pushgateway
3、修改prometheus配置,添加pushgateway
[root@node1 prometheus]# vim prometheus.yml ..... - job_name: pushgateway honor_labels: true #避免收集數據本身的 job 和 instance被pushgateway實例信息覆蓋 static_configs: - targets: ['192.168.42.128:9091'] labels: instance: pushgateway
4、測試:
(1)查看prometheus web界面,target里pushgateway狀態是否為UP。
(2)metric推送與刪除
[root@node1 prometheus]# echo "test_metric 999" | curl --data-binary @- http://192.168.42.128:9091/metrics/job/test_job #可在pushgateway或prometheus web頁面上查看 注:--data-binary 表示發送二進制數據,它是使用POST方式發送的!

[root@node1 prometheus]# cat <<EOF | curl --data-binary @- http://192.168.42.128:9091/metrics/job/test_job/instance/some_instance # TYPE some_metric counter some_metric{label="val1"} 999 # TYPE another_metric gauge # HELP another_metric Just an example. another_metric 2398.283 EOF

刪除某個組下的所有數據:
[root@node1 prometheus]# curl -X DELETE http://192.168.42.128:9091/metrics/job/test_job
刪除某個組下某個實例的所有數據:
[root@node1 prometheus]# curl -X DELETE http://192.168.42.128:9091/metrics/job/test_job/instance/some_instance
注:
可以發現 pushgateway 中的數據我們通常按照 job 和 instance 分組分類,所以這兩個參數不可缺少。
因為 Prometheus 配置 pushgateway 的時候,也會指定 job 和 instance, 但是它只表示 pushgateway 實例,不能真正表達收集數據的含義。所以在 prometheus 中配置 pushgateway 的時候,需要添加 honor_labels: true 參數, 從而避免收集數據本身的 job 和 instance 被覆蓋。
注意,為了防止 pushgateway 重啟或意外掛掉,導致數據丟失,我們可以通過 -persistence.file 和 -persistence.interval 參數將數據持久化下來。