深圳羅湖住房和建設(shè)局網(wǎng)站鄭州外語(yǔ)網(wǎng)站建站優(yōu)化
Pushgateway簡(jiǎn)介
Pushgateway是Prometheus監(jiān)控系統(tǒng)中的一個(gè)重要組件,它采用被動(dòng)push的方式獲取數(shù)據(jù),由應(yīng)用主動(dòng)將數(shù)據(jù)推送到pushgateway,然后Prometheus再?gòu)腜ushgateway抓取數(shù)據(jù)。使用Pushgateway的主要原因是:
- Prometheus和target由于某些原因網(wǎng)絡(luò)不能互通,需要經(jīng)由Pushgateway代理
- 某些作業(yè)生命周期較短,沒(méi)有足夠的時(shí)間等待Prometheus抓取數(shù)據(jù)。所以可以先把數(shù)據(jù)推送到Pushgateway,再讓Prometheus抓取
但是PushGateway也存在一些弊端:
- 通過(guò)單個(gè) Pushgateway 監(jiān)控多個(gè)實(shí)例時(shí), Pushgateway 將會(huì)成為單點(diǎn)故障和潛在瓶頸
- Pushgateway 可以持久化推送給它的所有監(jiān)控?cái)?shù)據(jù)。
因此,即使監(jiān)控目標(biāo)以下線,prometheus 還會(huì)拉取到舊的監(jiān)控?cái)?shù)據(jù),需要手動(dòng)清理 pushgateway 不要的數(shù)據(jù)。
部署Pushgateway
下載安裝包
wget https://github.com/prometheus/pushgateway/releases/download/v1.5.1/pushgateway-1.5.1.linux-amd64.tar.gz
tar xvf pushgateway-1.5.1.linux-amd64.tar.gz
pushgateway-1.5.1.linux-amd64/pushgateway /usr/bin/
pushgateway -h #查看幫助
準(zhǔn)備service文件
root@prometheus-server-01:~# cat /lib/systemd/system/pushgateway.service
[Unit]
Description=Prometheus Pushgateway
After=network.target[Service]
Type=simple
User=root
Group=root
ExecStart=pushgateway --web.listen-address=:9091
Restart=on-failure[Install]
WantedBy=multi-user.target
啟動(dòng)服務(wù)
systemctl daemon-reload
systemctl start pushgateway
systemctl status pushgateway
systemctl enable pushgateway
訪問(wèn)pushgateway界面
配置Prometheus抓取數(shù)據(jù)
在prometheus配置中添加job,抓取pushgateway數(shù)據(jù),內(nèi)容如下:
- job_name: pushgatewayhonor_labels: truestatic_configs:- targets: ["192.168.122.21:9091"]
配置修改完成后重啟Prometheus。然后在Prometheus界面查看target狀態(tài)
測(cè)試
要推送數(shù)據(jù)到Pushgateway中,可以其提供的API接口來(lái)添加,默認(rèn)URL地址為http://<ip>:9091/metrics/job/<job-name>/<label-name>/<label-value>
其中job-name是必填項(xiàng),是job標(biāo)簽的值,后邊可以跟任意數(shù)量的標(biāo)簽&標(biāo)簽值對(duì),一般會(huì)添加一個(gè)instance/<instance-name>標(biāo)簽來(lái)區(qū)分指標(biāo)數(shù)據(jù)來(lái)源
測(cè)試向Pushgateway推送單條數(shù)據(jù)
執(zhí)行如下命令進(jìn)行單條數(shù)據(jù)推送
#my_metric表示指標(biāo)名稱,2022表示指標(biāo)值
echo "my_metric 2022"| curl --data-binary @- http://192.168.122.21:9091/metrics/job/test-job/instance/192.168.122.22
#再執(zhí)行一次推送,將值改為2023
echo "my_metric 2023"| curl --data-binary @- http://192.168.122.21:9091/metrics/job/test-job/instance/192.168.122.22
在Pushgateway界面查看數(shù)據(jù),如下圖,可以看到只顯示了最新值。
在Prometheus中查詢my_metric,驗(yàn)證是否獲取到數(shù)據(jù)
測(cè)試向Pushgateway推送多條數(shù)據(jù)
cat <<EOF | curl --data-binary @- http://192.168.122.21:9091/metrics/job/test-job/instance/192.168.122.22
#TYPE node_memory_total gauge
node_memory_total 10240000000
#TYPE node_memory_usage gauge
node_memory_usuge 1024000000
EOF
在Pushgateway界面查看數(shù)據(jù)
在Prometheus中查詢,驗(yàn)證是否獲取到數(shù)據(jù)
測(cè)試刪除數(shù)據(jù)
curl -X DELETE http://192.168.122.21:9091/metrics/job/test-job/instance/192.168.122.22
在Pushgateway界面驗(yàn)證