背景
當prometheus的server與target不在同一網段網絡不通,無法直接拉取target數據,需要使用pushgateway作為數據中轉點。
弊端
將多個節點數據匯總到 pushgateway, 如果 pushgateway 掛了,受影響比多個 target 大。
Prometheus 拉取狀態 up 只針對 pushgateway, 無法做到對每個節點有效。
Pushgateway 可以持久化推送給它的所有監控數據。即使你的監控已經下線,prometheus 還會拉取到舊的監控數據,需要手動清理 pushgateway 不要的數據。
實驗環境
以下pushgateway分別prometheus exporter互通,其他都不通
prometheus 10.2.1.11
pushgateway 10.3.1.11
exporter 10.2.1.11
部署
pushgateway
下載安裝包 pushgateway-0.8.0.linux-amd64.tar.gz (https://prometheus.io/download/#pushgateway/ )
tar -xvf pushgateway-0.8.0.linux-amd64.tar.gz -C /usr/local
mv /usr/local/pushgateway-0.8.0.linux-amd64 /usr/local/pushgateway
啟動服務
cd /usr/local/pushgateway/
./pushgateway &
prometheus
新增job pushgateway
vim /usr/local/prometheus/prometheus.yml
- job_name: 'pushgateway'
scrape_interval: 30s
honor_labels: true #加上此配置exporter節點上傳數據中的一些標簽將不會被pushgateway節點的相同標簽覆蓋
static_configs:
- targets: ['10.3.1.11:9091']
labels:
instance: pushgateway
查看target狀態:
exporter
安裝好node_exporter,此處不多介紹
傳送監控數據到pushgateway節點
對於傳過去的監控項會添加此處定義的標簽 job=test instance=10.2.1.11 hostname=ip-10-2-1-11
curl 127.0.0.1:9100/metrics|curl --data-binary @- http://10.3.1.11:9091/metrics/job/test/instance/10.2.1.11/hostname/ip-10-2-1-11
此處也可以傳送自定義的監控項值,以及自定義監控腳本抓取的值
echo "logic_cpu 5" |curl --data-binary @- http://10.3.1.11:9091/metrics/job/test/instance/10.2.1.11/hostname/ip-10-2-1-11
刪除某個實例的數據:
curl -X DELETE http://10.3.1.11:9091/metrics/job/test/instance/10.2.1.11/hostname/ip-10-2-1-11