prometheus學習系列十一: Prometheus pushgateway的使用


由於網絡問題或者安全問題,可能我們的數據無法直接暴露出一個entrypoint 給prometheus采集。 這個時候可能就需要一個pushgateway來作為中間者完成中轉工作。  prometheus還是采用pull方式來采集pushgateway的數據,我們的采集端通過push方式把數據push給pushgateway,來完成數據的上報。

pushgateway的安裝

[root@node01 src]# wget https://github.com/prometheus/pushgateway/releases/download/v0.10.0/pushgateway-0.10.0.linux-amd64.tar.gz
[root@node01 src]# tar xf pushgateway-0.10.0.linux-amd64.tar.gz 
[root@node01 src]# ll
total 8732
drwxr-xr-x. 2 root root       6 Nov  5  2016 debug
drwxr-xr-x. 2 root root       6 Nov  5  2016 kernels
drwxr-xr-x  2 3434 3434      54 Oct 10 19:29 pushgateway-0.10.0.linux-amd64
-rw-r--r--  1 root root 8940709 Oct 10 19:30 pushgateway-0.10.0.linux-amd64.tar.gz
[root@node01 src]# mv pushgateway-0.10.0.linux-amd64 /usr/local/^C
[root@node01 src]# mkdir /usr/local/prometheus
[root@node01 src]# mv pushgateway-0.10.0.linux-amd64 /usr/local/prometheus/
[root@node01 src]# cd  /usr/local/prometheus/
[root@node01 prometheus]# ls
pushgateway-0.10.0.linux-amd64
[root@node01 prometheus]# ln -s pushgateway-0.10.0.linux-amd64/ pushgateway
[root@node01 prometheus]# ll
total 0
lrwxrwxrwx 1 root root 31 Oct 11 04:00 pushgateway -> pushgateway-0.10.0.linux-amd64/
drwxr-xr-x 2 3434 3434 54 Oct 10 19:29 pushgateway-0.10.0.linux-amd64

pushgateway的配置

[root@node01 system]#  cd /usr/lib/systemd/system
[root@node01 system]#  vim pushgateway.service
[root@node01 system]# cat pushgateway.service 
[Unit]
Description=prometheus
After=network.target 

[Service]
User=prometheus
Group=prometheus
WorkingDirectory=/usr/local/prometheus/pushgateway
ExecStart=/usr/local/prometheus/pushgateway/pushgateway \
                --web.enable-admin-api  \
                --persistence.file="pushfile.txt" \
                --persistence.interval=10m 
[Install]
WantedBy=multi-user.target
[root@node01 system]# systemctl enable pushgateway 
Created symlink from /etc/systemd/system/multi-user.target.wants/pushgateway.service to /usr/lib/systemd/system/pushgateway.service.
[root@node01 system]# systemctl start pushgateway
[root@node01 system]# systemctl status pushgateway 

注意: 上面的持久文件如果存儲量大,需要考慮配置單獨的磁盤來存儲。

測試web頁面

 

配置采集push端

添加一個數據,查看結果

[root@node02 ~]# !vim
vim push_memory.sh
#!/bin/bash
# desc push memory info

total_memory=$(free  |awk '/Mem/{print $2}')
used_memory=$(free  |awk '/Mem/{print $3}')

job_name="custom_memory"
instance_name="192.168.100.12"

cat <<EOF | curl --data-binary @- http://192.168.100.11:9091/metrics/job/$job_name/instance/$instance_name
#TYPE custom_memory_total  gauge
custom_memory_total $total_memory
#TYPE custom_memory_used  gauge
custom_memory_used $used_memory
EOF

# 執行導入
bash push_memory.sh

插入數據后效果圖

集成prometheus

添加pushgateway的采集

# 修改prometheus.yml 加入如下片段 
 - job_name: "custom-memory-pushgateway"
    #honor_labels: true
    static_configs:
    - targets: ["192.168.100.11:9091"]

持續生成數據

上面執行的 push_memory.sh腳本也就是只是插入一次數據, 我們這里使用計划任務來周期push數據到pushgateway中。

 

[root@node02 ~]# crontab  -e 
no crontab for root - using an empty one
1 * * * * /root/push_memory.sh
[root@node02 ~]# chmod a+x push_memory.sh 

 

效果圖

 

可以發現instance和job標簽有點問題, 這是pushgateway填充的, 我們可以加入honor配置使用我們自定義的。

修改配置如下

  - job_name: "custom-memory-pushgateway"
    honor_labels: true
    static_configs:
    - targets: ["192.168.100.11:9091"]

效果圖

 總結

我們可以通過pushgateway來輔助采集。 此場景中,我們假定的192.168.100.10這個prometheus server服務器是到192.168.100.12網絡是不通的, 但是192.168.100.11 這個ip地址是可以和2個ip是通的, 這里就可以在192.168.100.11 這個服務器上面部署pushgateway來作為橋梁, 采集到192.168.100.12的監控數據。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM