測試環(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
下載并運行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
開始運行之前,先對它進行配置。
配置Prometheus自身監(jiān)控
Prometheus通過抓取度量HTTP端點來從目標收集指標。由于Prometheus以同樣的方式暴露自己的數(shù)據(jù),它也可以搜集和監(jiān)控自己的健康狀況。
雖然只收集自身數(shù)據(jù)的Prometheus服務器不是很有用,但它是一個很好的開始示例。保存以下Prometheus基礎(chǔ)配置到一個名為prometheus.yml的文件(安裝包自動解壓后,解壓目錄下,默認就就有一個名為prometheus.yml的文件)
global: scrape_interval: 15s # 默認,每15秒采樣一次目標 # 與其它外部系統(tǒng)(比如federation, remote storage, Alertmanager)交互時,會附加這些標簽到時序數(shù)據(jù)或者報警 external_labels: monitor: 'codelab-monitor' # 一份采樣配置僅包含一個 endpoint 來做采樣 # 下面是 Prometheus 本身的endpoint: scrape_configs: # job_name 將被被當作一個標簽 `job=`添加到該配置的任意時序采樣. - job_name: 'prometheus' # 覆蓋全局默認值,從該job每5秒對目標采樣一次 scrape_interval: 5s static_configs: # 如果需要遠程訪問, localhost 也可以替換為具體IP,比如10.118.71.170 - targets: ['localhost:9090']
有關(guān)配置選項的完整說明,請參閱配置文檔。
啟動Prometheus
使用新創(chuàng)建的配置文件來啟動 Prometheus,切換到包含 Prometheus 二進制文件的目錄并運行
# 啟動 Prometheus. # 默認地, Prometheus 在 ./data 路徑下存儲其數(shù)據(jù)庫 (flag --storage.tsdb.path). # ./prometheus --config.file=prometheus.yml
通過訪問localhost:9000來瀏覽狀態(tài)頁。等待幾秒讓他從自己的 HTTP metric endpoint 來收集數(shù)據(jù)。
還可以通過訪問到其 metrics endpoint(http://localhost:9090/metrics)來驗證 Prometheus 是否正在提供有關(guān)其自身的 metrics
開放防火墻端口
# firewall-cmd --permanent --zone=public --add-port=9090/tcp success # firewall-cmd --reload success
使用expressin browser
使用 Prometheus 內(nèi)置的expressin browser訪問localhost:9090/graph,選擇Graph導航菜單下的Tabletab頁 (Classic UI下為Consoletab頁)。
通過查看localhost:9090/metrics頁面內(nèi)容可知,Prometheus 導出了關(guān)于其自身的一個名為prometheus_target_interval_length_seconds指標(目標采樣之間的實際間隔)。將其作為搜索表達式,輸入到表達式搜索框中,點擊Execute按鈕,如下,將返回多個不同的時間序列(以及每個時間序列的最新值),所有時間序列的 metric 名稱均為prometheus_target_interval_length_seconds,但具有不同的標簽。這些標簽具有不同的延遲百分比和目標組間隔(target group intervals)。
如果我們只對第 99 個百分位延遲感興趣,則可以使用以下查詢來檢索該信息:
prometheus_target_interval_length_seconds{quantile="0.99"}
如果需要計算返回的時間序列數(shù),可以修改查詢?nèi)缦拢?/p>
count(prometheus_target_interval_length_seconds)
更多有關(guān) expression language 的更多信息,請查看 expression language 文檔。
使用繪圖界面
要繪制圖形表達式,請使用 “Graph” 選項卡。
例如,輸入以下表達式以繪制在自采樣的 Prometheus 中每秒創(chuàng)建 chunk 的速率:
rate(prometheus_tsdb_head_chunks_created_total[1m])
啟動一些采樣目標
現(xiàn)在讓我們增加一些采樣目標供 Prometheus 進行采樣。
使用Node Exporter作為采樣目標,多關(guān)于它的使用請查閱
# 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)在,應該存在監(jiān)聽http://localhost:8080/metrics, http://localhost:8081/metrics 和http://localhost:8082/metrics的示例目標
配置 Prometheus 來監(jiān)控示例目標
現(xiàn)在,我們將配置 Prometheus 來采樣這些新目標。讓我們將所有三個 endpoint 分組為一個稱為 “node” 的 job。但是,假設前兩個 endpoint 是生產(chǎn)目標,而第三個 endpoint 代表金絲雀實例。為了在 Prometheus 中對此建模,我們可以將多個端組添加到單個 job 中,并為每個目標組添加額外的標簽。在此示例中,我們將group=“ production”標簽添加到第一個目標組,同時將group=“ canary”添加到第二個目標。
為此,請將以下job定義添加到prometheus.yml中的scrape_configs部分,然后重新啟動 Prometheus 實例。修改后的prometheus.yml內(nèi)容如下
global: scrape_interval: 15s # 默認,每15秒采樣一次目標 # 與其它外部系統(tǒng)(比如federation, remote storage, Alertmanager)交互時,會附加這些標簽到時序數(shù)據(jù)或者報警 external_labels: monitor: 'codelab-monitor' # 一份采樣配置僅包含一個 endpoint 來做采樣 # 下面是 Prometheus 本身的endpoint: scrape_configs: # job_name 將被被當作一個標簽 `job=`添加到該配置的任意時序采樣. - job_name: 'prometheus' # 覆蓋全局默認值,從該job每5秒對目標采樣一次 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ù)千個時間序列中查詢時可能會變慢。為了提高效率,Prometheus 允許通過配置的記錄規(guī)則將表達式預記錄到全新的持久化的時間序列中。假設我們感興趣的是 5 分鐘的窗口內(nèi)測得的每個實例的所有cpu上平均的cpu時間(node_cpu_seconds_total,保留 Job,instance,和mode 維度))。我們可以這樣寫:
avg by (job, instance, mode) (rate(node_cpu_seconds_total[5m]))
Graph中執(zhí)行查詢,結(jié)果如下
現(xiàn)在,要將由該表達式產(chǎn)生的時間序列記錄到一個名為:job_instance_modeavg_rate5m的新指標,使用以下記錄規(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語句,以便 Prometheus 選擇此新規(guī)則?,F(xiàn)在,prometheus.yml配置應如下所示:
global: scrape_interval: 15s # 默認,每15秒采樣一次目標 # 與其它外部系統(tǒng)(比如federation, remote storage, Alertmanager)交互時,會附加這些標簽到時序數(shù)據(jù)或者報警 external_labels: monitor: 'codelab-monitor' rule_files: - 'prometheus.rules.yml' # 一份采樣配置僅包含一個 endpoint 來做采樣 # 下面是 Prometheus 本身的endpoint: scrape_configs: # job_name 將被被當作一個標簽 `job=`添加到該配置的任意時序采樣. - job_name: 'prometheus' # 覆蓋全局默認值,從該job每5秒對目標采樣一次 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'
通過新的配置重新啟動 Prometheus,并通過expression brower查詢job_instance_modeavg_rate5m,結(jié)果如下
鏈接:https://www.cnblogs.com/shouke/p/18667439
-
監(jiān)控
+關(guān)注
關(guān)注
6文章
2240瀏覽量
55399 -
Prometheus
+關(guān)注
關(guān)注
0文章
28瀏覽量
1732
原文標題:從零開始掌握Prometheus:企業(yè)級監(jiān)控與報警系統(tǒng)的最佳實踐
文章出處:【微信號:magedu-Linux,微信公眾號:馬哥Linux運維】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
Prometheus的架構(gòu)原理從“監(jiān)控”談起
![<b class='flag-5'>Prometheus</b>的架構(gòu)原理<b class='flag-5'>從</b>“<b class='flag-5'>監(jiān)控</b>”談起](https://file.elecfans.com/web1/M00/C9/CD/pIYBAF-BZXqAPcfcAAB1UYYjyhE686.png)
Prometheus的基本原理與開發(fā)指南
![<b class='flag-5'>Prometheus</b>的基本原理與開發(fā)<b class='flag-5'>指南</b>](https://file1.elecfans.com/web2/M00/AE/F8/wKgZomVMSESAWJotAAAAjgjvZ2U667.png)
SAS走進企業(yè)級存儲應用
采用nvSRAM確保企業(yè)級SSD故障時電源可靠性
prometheus做監(jiān)控服務的整個流程介紹
戴爾“企業(yè)級”視頻監(jiān)控解決方案
![戴爾“<b class='flag-5'>企業(yè)級</b>”視頻<b class='flag-5'>監(jiān)控</b>解決方案](https://file1.elecfans.com//web2/M00/A6/9A/wKgZomUMPweAGcfyAAFE7yPH06k976.jpg)
基于企業(yè)級信息集成平臺的數(shù)據(jù)存儲備份系統(tǒng)的構(gòu)建_王永平
使用Thanos+Prometheus+Grafana構(gòu)建監(jiān)控系統(tǒng)
基于kube-prometheus的大數(shù)據(jù)平臺監(jiān)控系統(tǒng)設計
構(gòu)建企業(yè)級文件管理系統(tǒng),輕松搭建 Seafile 社區(qū)版云存儲解決方案
![<b class='flag-5'>構(gòu)建</b><b class='flag-5'>企業(yè)級</b>文件管理<b class='flag-5'>系統(tǒng)</b>,輕松搭建 Seafile 社區(qū)版云存儲解決方案](https://file1.elecfans.com//web3/M00/05/BB/wKgZPGeEhP6AalNCAAEA43MSbXE394.png)
評論