prometheus之Pushgateway安裝和使用方法
一、Pushgateway概述
1.1 Pushgateway簡(jiǎn)介
Pushgateway是prometheus的一個(gè)組件,prometheus server默認(rèn)是通過exporter主動(dòng)獲取數(shù)據(jù)(默認(rèn)采取pull拉取數(shù)據(jù)),pushgateway則是通過被動(dòng)方式推送數(shù)據(jù)到prometheus server,用戶可以寫一些自定義的監(jiān)控腳本把需要監(jiān)控的數(shù)據(jù)發(fā)送給pushgateway, 然后pushgateway再把數(shù)據(jù)發(fā)送給Prometheus server。

1.2 Pushgateway優(yōu)點(diǎn)
- Prometheus 默認(rèn)采用定時(shí)pull 模式拉取targets數(shù)據(jù),但是如果不在一個(gè)子網(wǎng)或者防火墻,prometheus就拉取不到targets數(shù)據(jù),所以可以采用各個(gè)target往pushgateway上push數(shù)據(jù),然后prometheus去pushgateway上定時(shí)pull數(shù)據(jù)
- 在監(jiān)控業(yè)務(wù)數(shù)據(jù)的時(shí)候,需要將不同數(shù)據(jù)匯總, 匯總之后的數(shù)據(jù)可以由pushgateway統(tǒng)一收集,然后由 Prometheus 統(tǒng)一拉取。
1.3 pushgateway缺點(diǎn)
- Prometheus拉取狀態(tài)只針對(duì) pushgateway, 不能對(duì)每個(gè)節(jié)點(diǎn)都有效;
- Pushgateway出現(xiàn)問題,整個(gè)采集到的數(shù)據(jù)都會(huì)出現(xiàn)問題
- 監(jiān)控下線,prometheus還會(huì)拉取到舊的監(jiān)控?cái)?shù)據(jù),需要手動(dòng)清理 pushgateway不要的數(shù)據(jù)。
二、測(cè)試環(huán)境
IP | 主機(jī)名 |
192.168.2.139 | master1 |
192.168.40.140 | node1 |
三、安裝測(cè)試
3.1 pushgateway安裝
在node1節(jié)點(diǎn)操作
docker pull prom/pushgateway docker run -d --name pushgateway -p 9091:9091 prom/pushgateway
在瀏覽器訪問192.168.2.140:9091出現(xiàn)如下ui界面

3.2 prometheus添加pushgateway
修改prometheus-cfg.yaml文件
- job_name: 'pushgateway'
scrape_interval: 5s
static_configs:
- targets: ['192.168.2.140:9091']
honor_labels: true更新
kubectl apply -f prometheus-alertmanager-cfg.yaml kubectl delete -f prometheus-alertmanager-deploy.yaml kubectl apply -f prometheus-alertmanager-deploy.yaml
登錄prometheus http://192.168.2.139:30242/targets

3.3 推送指定的數(shù)據(jù)格式到pushgateway
1.添加單條數(shù)據(jù)
# 向 {job="test_job"} 添加單條數(shù)據(jù):
echo " metric 3.6" | curl --data-binary @- http://192.168.2.140:9091/metrics/job/test_job這里需要注意的是將<key & value>推送給pushgateway,curl --data-binary是將HTTP POST請(qǐng)求中的數(shù)據(jù)發(fā)送給HTTP服務(wù)器(pushgateway),和用戶提交THML表單時(shí)瀏覽器的行為是一樣的,HTTP POST請(qǐng)求中的數(shù)據(jù)為純二進(jìn)制數(shù)據(jù)。

prometheus web中查詢

2.添加復(fù)雜數(shù)據(jù)
# 添加復(fù)雜數(shù)據(jù) cat <<EOF | curl --data-binary @- http://192.168.2.140:9091/metrics/job/test_job/instance/test_instance # TYPE node_memory_usage gauge node_memory_usage 26 # TYPE memory_total gauge node_memory_total 26000 EOF
- http://192.168.2.143:9091/metrics/job/test_job:這是URL的主location,發(fā)送到哪個(gè)URL
- job/test_job:表示是推送到哪個(gè)prometheus定義的job里面,上面我們定義的job_name為pushgateway
- instance/test_instance:表示推送后顯示的主機(jī)名稱是什么,從上面pushgateway圖也可以看出

如下是刪除某個(gè)實(shí)例
# 刪除某個(gè)組下某個(gè)實(shí)例的所有數(shù)據(jù) curl -X DELETE http://192.168.2.140:9091/metrics/job/test_job/instance/test_instance # 刪除某個(gè)組下的所有數(shù)據(jù): curl -X DELETE http://192.168.2.140:9091/metrics/job/test_job
3.SDk-prometheus-client使用
python安裝 prometheus_client
使用 pip 工具可以非常方便地安裝 prometheus_client:

測(cè)試腳本
# -*- coding: utf-8 -*-
# 導(dǎo)入所需的庫(kù)
from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
if __name__ == '__main__':
# 定義和注冊(cè)指標(biāo)
registry = CollectorRegistry()
labels = ['req_status', 'req_method', 'req_url']
g_one = Gauge('requests_total', 'url請(qǐng)求次數(shù)', labels, registry=registry)
g_two = Gauge('avg_response_time_seconds', '1分鐘內(nèi)的URL平均響應(yīng)時(shí)間', labels, registry=registry)
# 收集和記錄指標(biāo)數(shù)據(jù)
g_one.labels('200','GET', '/test/url').set(1) #set設(shè)定值
g_two.labels('200','GET', '/test/api/url/').set(10) #set設(shè)定值
# 推送指標(biāo)數(shù)據(jù)到Pushgateway
push_to_gateway('http://192.168.2.140:9091', job='SampleURLMetrics', registry=registry)在這個(gè)示例中,我們定義了一個(gè)名為requests_total的指標(biāo),記錄了一個(gè)值為1和10的示例數(shù)據(jù),并將指標(biāo)數(shù)據(jù)推送到了名為example_job的job中。

到此這篇關(guān)于prometheus之Pushgateway安裝和使用的文章就介紹到這了,更多相關(guān)Pushgateway安裝和使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
BurpSuite超詳細(xì)安裝和基礎(chǔ)使用教程(已破解)
Burp?Suite?是用于攻擊web?應(yīng)用程序的集成平臺(tái)包含了許多Burp工具,它主要用來做安全性滲透測(cè)試,可以實(shí)現(xiàn)攔截請(qǐng)求、Burp?Spider爬蟲、漏洞掃描(付費(fèi))等類似Fiddler和Postman但比其更強(qiáng)大的功能,今天給大家介紹下BurpSuite安裝破解使用教程,感興趣的朋友一起看看吧2022-10-10
使用roolup構(gòu)建你的lib(實(shí)現(xiàn)步驟)
大家都知道Rollup更加適合用于構(gòu)建lib 而 Webpack, Precel 更加適合開發(fā)應(yīng)用。本文,將結(jié)合一個(gè)簡(jiǎn)單的例子說說如何使用Rollup構(gòu)建自己的lib,感興趣的朋友一起看看吧2021-08-08
火車頭采集正文多圖片如何采集下載(只下載一張圖片的情況該如何處理)
文章采集是站長(zhǎng)常常會(huì)遇到的事,很多人使用火車頭采集器軟件,采集正文中有多個(gè)圖片的時(shí)候如果設(shè)置錯(cuò)誤,會(huì)遇到只下載一張圖片、采集后的正文多張圖片同一個(gè)文件名的情況2023-03-03
計(jì)算機(jī)程序設(shè)計(jì)并行計(jì)算概念及定義全面詳解
最近項(xiàng)目需要實(shí)現(xiàn)程序的并行化,剛好借著翻譯這篇帖子的機(jī)會(huì),了解和熟悉并行計(jì)算的基本概念和程序設(shè)計(jì),有需要的朋友可以借鑒參考下2021-11-11
kali2021.4a使用virtualenv安裝angr的詳細(xì)過程
在Linux中安裝各種依賴python的軟件時(shí),最頭疼的問題之一就是各個(gè)軟件的python版本不匹配的問題,angr依賴python3,因此考慮使用virtualenv來安裝angr,需要的朋友可以參考下2022-11-11

