測(cè)試環(huán)境
prometheus-2.26.0.linux-amd64.tar.gz 下載地址:https://github.com/prometheus/prometheus/releases/download/v2.26.0/prometheus-2.26.0.linux-amd64.tar.gz prometheus-2.54.1.linux-amd64.tar.gz 下載地址:https://github.com/prometheus/prometheus/releases/download/v2.26.0/prometheus-2.26.0.linux-amd64.tar.gz CentOS 7.9
下載并運(yùn)行Prometheus
# wget https://github.com/prometheus/prometheus/releases/download/v2.26.0/prometheus-2.26.0.linux-amd64.tar.gz # tar xvzf prometheus-2.26.0.linux-amd64.tar.gz # cd prometheus-2.26.0.linux-amd64 # ls console_libraries consoles LICENSE NOTICE prometheus prometheus.yml promtool
開(kāi)始運(yùn)行之前,先對(duì)它進(jìn)行配置。
配置Prometheus自身監(jiān)控
Prometheus通過(guò)抓取度量HTTP端點(diǎn)來(lái)從目標(biāo)收集指標(biāo)。由于Prometheus以同樣的方式暴露自己的數(shù)據(jù),它也可以搜集和監(jiān)控自己的健康狀況。
雖然只收集自身數(shù)據(jù)的Prometheus服務(wù)器不是很有用,但它是一個(gè)很好的開(kāi)始示例。保存以下Prometheus基礎(chǔ)配置到一個(gè)名為prometheus.yml的文件(安裝包自動(dòng)解壓后,解壓目錄下,默認(rèn)就就有一個(gè)名為prometheus.yml的文件)
global: scrape_interval: 15s # 默認(rèn),每15秒采樣一次目標(biāo) # 與其它外部系統(tǒng)(比如federation, remote storage, Alertmanager)交互時(shí),會(huì)附加這些標(biāo)簽到時(shí)序數(shù)據(jù)或者報(bào)警 external_labels: monitor: 'codelab-monitor' # 一份采樣配置僅包含一個(gè) endpoint 來(lái)做采樣 # 下面是 Prometheus 本身的endpoint: scrape_configs: # job_name 將被被當(dāng)作一個(gè)標(biāo)簽 `job=`添加到該配置的任意時(shí)序采樣. - job_name: 'prometheus' # 覆蓋全局默認(rèn)值,從該job每5秒對(duì)目標(biāo)采樣一次 scrape_interval: 5s static_configs: # 如果需要遠(yuǎn)程訪問(wèn), localhost 也可以替換為具體IP,比如10.118.71.170 - targets: ['localhost:9090']
有關(guān)配置選項(xiàng)的完整說(shuō)明,請(qǐng)參閱配置文檔。
啟動(dòng)Prometheus
使用新創(chuàng)建的配置文件來(lái)啟動(dòng) Prometheus,切換到包含 Prometheus 二進(jìn)制文件的目錄并運(yùn)行
# 啟動(dòng) Prometheus. # 默認(rèn)地, Prometheus 在 ./data 路徑下存儲(chǔ)其數(shù)據(jù)庫(kù) (flag --storage.tsdb.path). # ./prometheus --config.file=prometheus.yml
通過(guò)訪問(wèn)localhost:9000來(lái)瀏覽狀態(tài)頁(yè)。等待幾秒讓他從自己的 HTTP metric endpoint 來(lái)收集數(shù)據(jù)。
還可以通過(guò)訪問(wèn)到其 metrics endpoint(http://localhost:9090/metrics)來(lái)驗(yàn)證 Prometheus 是否正在提供有關(guān)其自身的 metrics
開(kāi)放防火墻端口
# firewall-cmd --permanent --zone=public --add-port=9090/tcp success # firewall-cmd --reload success
使用expressin browser
使用 Prometheus 內(nèi)置的expressin browser訪問(wèn)localhost:9090/graph,選擇Graph導(dǎo)航菜單下的Tabletab頁(yè) (Classic UI下為Consoletab頁(yè))。
通過(guò)查看localhost:9090/metrics頁(yè)面內(nèi)容可知,Prometheus 導(dǎo)出了關(guān)于其自身的一個(gè)名為prometheus_target_interval_length_seconds指標(biāo)(目標(biāo)采樣之間的實(shí)際間隔)。將其作為搜索表達(dá)式,輸入到表達(dá)式搜索框中,點(diǎn)擊Execute按鈕,如下,將返回多個(gè)不同的時(shí)間序列(以及每個(gè)時(shí)間序列的最新值),所有時(shí)間序列的 metric 名稱均為prometheus_target_interval_length_seconds,但具有不同的標(biāo)簽。這些標(biāo)簽具有不同的延遲百分比和目標(biāo)組間隔(target group intervals)。
如果我們只對(duì)第 99 個(gè)百分位延遲感興趣,則可以使用以下查詢來(lái)檢索該信息:
prometheus_target_interval_length_seconds{quantile="0.99"}
如果需要計(jì)算返回的時(shí)間序列數(shù),可以修改查詢?nèi)缦拢?/p>
count(prometheus_target_interval_length_seconds)
更多有關(guān) expression language 的更多信息,請(qǐng)查看 expression language 文檔。
使用繪圖界面
要繪制圖形表達(dá)式,請(qǐng)使用 “Graph” 選項(xiàng)卡。
例如,輸入以下表達(dá)式以繪制在自采樣的 Prometheus 中每秒創(chuàng)建 chunk 的速率:
rate(prometheus_tsdb_head_chunks_created_total[1m])
啟動(dòng)一些采樣目標(biāo)
現(xiàn)在讓我們?cè)黾右恍┎蓸幽繕?biāo)供 Prometheus 進(jìn)行采樣。
使用Node Exporter作為采樣目標(biāo),多關(guān)于它的使用請(qǐng)查閱
# wget https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz # tar -xvzf node_exporter-1.1.2.linux-amd64.tar.gz # ./node_exporter --web.listen-address 127.0.0.1:8001 # ./node_exporter --web.listen-address 127.0.0.1:8002 # ./node_exporter --web.listen-address 127.0.0.1:8003
現(xiàn)在,應(yīng)該存在監(jiān)聽(tīng)http://localhost:8080/metrics, http://localhost:8081/metrics 和http://localhost:8082/metrics的示例目標(biāo)
配置 Prometheus 來(lái)監(jiān)控示例目標(biāo)
現(xiàn)在,我們將配置 Prometheus 來(lái)采樣這些新目標(biāo)。讓我們將所有三個(gè) endpoint 分組為一個(gè)稱為 “node” 的 job。但是,假設(shè)前兩個(gè) endpoint 是生產(chǎn)目標(biāo),而第三個(gè) endpoint 代表金絲雀實(shí)例。為了在 Prometheus 中對(duì)此建模,我們可以將多個(gè)端組添加到單個(gè) job 中,并為每個(gè)目標(biāo)組添加額外的標(biāo)簽。在此示例中,我們將group=“ production”標(biāo)簽添加到第一個(gè)目標(biāo)組,同時(shí)將group=“ canary”添加到第二個(gè)目標(biāo)。
為此,請(qǐng)將以下job定義添加到prometheus.yml中的scrape_configs部分,然后重新啟動(dòng) Prometheus 實(shí)例。修改后的prometheus.yml內(nèi)容如下
global: scrape_interval: 15s # 默認(rèn),每15秒采樣一次目標(biāo) # 與其它外部系統(tǒng)(比如federation, remote storage, Alertmanager)交互時(shí),會(huì)附加這些標(biāo)簽到時(shí)序數(shù)據(jù)或者報(bào)警 external_labels: monitor: 'codelab-monitor' # 一份采樣配置僅包含一個(gè) endpoint 來(lái)做采樣 # 下面是 Prometheus 本身的endpoint: scrape_configs: # job_name 將被被當(dāng)作一個(gè)標(biāo)簽 `job=`添加到該配置的任意時(shí)序采樣. - job_name: 'prometheus' # 覆蓋全局默認(rèn)值,從該job每5秒對(duì)目標(biāo)采樣一次 scrape_interval: 5s static_configs: - targets: ['10.118.71.170:9090'] - job_name: 'node' # Override the global default and scrape targets from this job every 5 seconds. scrape_interval: 5s static_configs: - targets: ['localhost:8001', 'localhost:8002'] labels: group: 'production' - targets: ['localhost:8003'] labels: group: 'canary'
查看Targets(Status->Targets)
Graph查詢
配置規(guī)則以將采樣的數(shù)據(jù)聚合到新的時(shí)間序列
盡管在我們的示例中并不會(huì)有問(wèn)題,但是在聚集了數(shù)千個(gè)時(shí)間序列中查詢時(shí)可能會(huì)變慢。為了提高效率,Prometheus 允許通過(guò)配置的記錄規(guī)則將表達(dá)式預(yù)記錄到全新的持久化的時(shí)間序列中。假設(shè)我們感興趣的是 5 分鐘的窗口內(nèi)測(cè)得的每個(gè)實(shí)例的所有cpu上平均的cpu時(shí)間(node_cpu_seconds_total,保留 Job,instance,和mode 維度))。我們可以這樣寫:
avg by (job, instance, mode) (rate(node_cpu_seconds_total[5m]))
Graph中執(zhí)行查詢,結(jié)果如下
現(xiàn)在,要將由該表達(dá)式產(chǎn)生的時(shí)間序列記錄到一個(gè)名為:job_instance_modeavg_rate5m的新指標(biāo),使用以下記錄規(guī)則創(chuàng)建文件并將其保存prometheus.rules.yml
groups: - name: cpu-node rules: - record: job_instance_modeavg_rate5m expr: avg by (job, instance, mode) (rate(node_cpu_seconds_total[5m]))
在prometheus.yml中添加rule_files語(yǔ)句,以便 Prometheus 選擇此新規(guī)則?,F(xiàn)在,prometheus.yml配置應(yīng)如下所示:
global: scrape_interval: 15s # 默認(rèn),每15秒采樣一次目標(biāo) # 與其它外部系統(tǒng)(比如federation, remote storage, Alertmanager)交互時(shí),會(huì)附加這些標(biāo)簽到時(shí)序數(shù)據(jù)或者報(bào)警 external_labels: monitor: 'codelab-monitor' rule_files: - 'prometheus.rules.yml' # 一份采樣配置僅包含一個(gè) endpoint 來(lái)做采樣 # 下面是 Prometheus 本身的endpoint: scrape_configs: # job_name 將被被當(dāng)作一個(gè)標(biāo)簽 `job=`添加到該配置的任意時(shí)序采樣. - job_name: 'prometheus' # 覆蓋全局默認(rèn)值,從該job每5秒對(duì)目標(biāo)采樣一次 scrape_interval: 5s static_configs: - targets: ['10.118.71.170:9090'] - job_name: 'node' # Override the global default and scrape targets from this job every 5 seconds. scrape_interval: 5s static_configs: - targets: ['localhost:8001', 'localhost:8002'] labels: group: 'production' - targets: ['localhost:8003'] labels: group: 'canary'
通過(guò)新的配置重新啟動(dòng) Prometheus,并通過(guò)expression brower查詢job_instance_modeavg_rate5m,結(jié)果如下
鏈接:https://www.cnblogs.com/shouke/p/18667439
-
監(jiān)控
+關(guān)注
關(guān)注
6文章
2320瀏覽量
57636 -
Prometheus
+關(guān)注
關(guān)注
0文章
30瀏覽量
1900
原文標(biāo)題:從零開(kāi)始掌握Prometheus:企業(yè)級(jí)監(jiān)控與報(bào)警系統(tǒng)的最佳實(shí)踐
文章出處:【微信號(hào):magedu-Linux,微信公眾號(hào):馬哥Linux運(yùn)維】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
Prometheus的架構(gòu)原理從“監(jiān)控”談起

Prometheus的基本原理與開(kāi)發(fā)指南

SAS走進(jìn)企業(yè)級(jí)存儲(chǔ)應(yīng)用
采用nvSRAM確保企業(yè)級(jí)SSD故障時(shí)電源可靠性
prometheus做監(jiān)控服務(wù)的整個(gè)流程介紹
戴爾“企業(yè)級(jí)”視頻監(jiān)控解決方案

評(píng)論