Prometheus Pushgateway
一、Pushgateway概述
1.1、Pushgateway簡介
Pushgateway是prometheus的一個組件,prometheus server默認是通過exporter主動獲取數據(默認采取pull
拉取數據),pushgateway則是通過被動方式推送數據到prometheus server,用戶可以寫一些自定義的監控腳本把需要監控的數據發送給pushgateway, 然后pushgateway再把數據發送給Prometheus server
1.2、Pushgateway優點
Prometheus 默認采用定時pull 模式拉取targets數據,但是如果不在一個子網或者防火牆,prometheus就拉取不到targets數據,所以可以采用各個target往pushgateway上push數據,然后prometheus去pushgateway上定時pull數據
在監控業務數據的時候,需要將不同數據匯總, 匯總之后的數據可以由pushgateway統一收集,然后由 Prometheus 統一拉取。
1.3、pushgateway缺點
1)Prometheus拉取狀態只針對 pushgateway, 不能對每個節點都有效;
2)Pushgateway出現問題,整個采集到的數據都會出現問題
3)監控下線,prometheus還會拉取到舊的監控數據,需要手動清理 pushgateway不要的數據。
二、Pushgateway安裝及使用
2.1、Pushgateway安裝
1)安裝Pushgateway
# 在k8s-node節點操作
[root@k8s-node1 ~]# docker run -d --name pushgateway -p 9091:9091 prom/pushgateway
[root@k8s-node1 ~]# docker ps -a|grep pushgateway
505f7dc4a17b prom/pushgateway "/bin/pushgateway" 13 seconds ago Up 13 seconds 0.0.0.0:9091->9091/tcp, :::9091->9091/tcp pushgateway
此時pushgateway上還沒有數據
2)修改prometheus-alertmanager-cfg.yaml文件,添加job
[root@k8s-master1 prometheus]# vim prometheus-alertmanager-cfg.yaml
- job_name: 'pushgateway'
scrape_interval: 5s
static_configs:
- targets: ['192.168.40.181:9091']
honor_labels: true
# honor_labels: true可以避免targets列表中的job_name是pushgateway的job,instance 和上報到pushgateway數據的job和instance沖突
# 更新
[root@k8s-master1 prometheus]# kubectl apply -f prometheus-alertmanager-cfg.yaml
[root@k8s-master1 prometheus]# kubectl delete -f prometheus-alertmanager-deploy.yaml
[root@k8s-master1 prometheus]# kubectl apply -f prometheus-alertmanager-deploy.yaml
3)測試發送數據
# 推送指定的數據格式到pushgateway
# 向 {job="test_job"} 添加單條數據:
echo "metric 3.6" | curl --data-binary @- http://192.168.40.181:9091/metrics/job/test_job
# 添加復雜數據
cat <<EOF | curl --data-binary @- http://192.168.40.181:9091/metrics/job/test_job/instance/test_instance
node_memory_usage 36
node_memory_total 36000
EOF
# 刪除某個組下某個實例的所有數據
curl -X DELETE http://192.168.40.181:9091/metrics/job/test_job/instance/test_instance
# 刪除某個組下的所有數據:
curl -X DELETE http://192.168.40.181:9091/metrics/job/test_job
2.2、Pushgateway使用
把數據上報到pushgateway,在被監控服務所在的機器配置數據上報,想要把192.168.40.181這個機器的內存數據上報到pushgateway
1)編寫shell腳本
[root@k8s-node1 ~]# vim push.sh
node_memory_usages=$(free -m | grep Mem | awk '{print $3/$2*100}')
job_name="memory"
instance_name="192.168.40.181"
cat <<EOF | curl --data-binary @- http://192.168.40.181:9091/metrics/job/$job_name/instance/$instance_name
# TYPE node_memory_usages gauge
node_memory_usages $node_memory_usages
EOF
2)打開pushgateway web ui界面,可看到如下
3)打開prometheus ui界面,可看到如下node_memory_usages
的metrics指標
4)設置計划任務,定時上報數據
[root@k8s-node1 ~]# crontab -e
*/1 * * * * /usr/bin/bash /root/push.sh