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)文章
聊聊Druid register mbean error的問題
這篇文章主要介紹了Druid register mbean error的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11鴻蒙開發(fā)Hvigor插件動(dòng)態(tài)生成代碼的操作方法
Hvigor主要提供了兩種方式來實(shí)現(xiàn)插件:基于hvigorfile腳本開發(fā)插件、基于typescript項(xiàng)目開發(fā),下面以基于hvigorfile腳本開發(fā)插件進(jìn)行介紹,感興趣的朋友跟隨小編一起看看吧2024-11-11基于Python和Java實(shí)現(xiàn)單詞計(jì)數(shù)(Word Count)
Spark框架也是MapReduce-like模型,采用“分治-聚合”策略來對(duì)數(shù)據(jù)分布進(jìn)行分布并行處理,本文就來利用Spark實(shí)現(xiàn)單詞統(tǒng)計(jì)的功能,需要的可以參考一下2023-05-05gliffy-confluence-plugin-9.1.2插件教程詳解
這篇文章主要介紹了gliffy-confluence-plugin-9.1.2破解教程詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-0210分鐘搞定讓你困惑的 Jenkins 環(huán)境變量過程詳解
這篇文章主要介紹了10分鐘搞定讓你困惑的 Jenkins 環(huán)境變量過程詳解,本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01