Prometheus+Grafana監控


系統架構

系統: centos 7.9 

配置: CPU 16核

網絡:千兆網絡

角色 主機名 IP地址

Prometheus

Grafana

grafana 10.121.118.50
監控客戶端node-export client01  
監控客戶端node-export client02  
監控客戶端node-export client03  

 

一、介紹Prometheus

Prometheus(普羅米修斯)是一套開源的監控&報警&時間序列數據庫的組合,起始是由SoundCloud公司開發的。隨着發展,越來越多公司和組織接受采用Prometheus,社會也十分活躍,他們便將它獨立成開源項目,並且有公司來運作。Google SRE的書內也曾提到跟他們BorgMon監控系統相似的實現是Prometheus。現在最常見的Kubernetes容器管理系統中,通常會搭配Prometheus進行監控。

Prometheus基本原理是通過HTTP協議周期性抓取被監控組件的狀態,這樣做的好處是任意組件只要提供HTTP接口就可以接入監控系統,不需要任何SDK或者其他的集成過程。這樣做非常適合虛擬化環境比如VM或者Docker 。

Prometheus應該是為數不多的適合Docker、Mesos、Kubernetes環境的監控系統之一。

輸出被監控組件信息的HTTP接口被叫做exporter 。目前互聯網公司常用的組件大部分都有exporter可以直接使用,比如Varnish、Haproxy、Nginx、MySQL、Linux 系統信息 (包括磁盤、內存、CPU、網絡等等),具體支持的源看:https://github.com/prometheus

與其他監控系統相比,Prometheus的主要特點是:

  • 一個多維數據模型(時間序列由指標名稱定義和設置鍵/值尺寸)。
  • 非常高效的存儲,平均一個采樣數據占~3.5bytes左右,320萬的時間序列,每30秒采樣,保持60天,消耗磁盤大概228G。
  • 一種靈活的查詢語言。
  • 不依賴分布式存儲,單個服務器節點。
  • 時間集合通過HTTP上的PULL模型進行。
  • 通過中間網關支持推送時間。
  • 通過服務發現或靜態配置發現目標。
  • 多種模式的圖形和儀表板支持。

二、Prometheus架構概覽

該圖說明了普羅米修斯(Prometheus)及其一些生態系統組件的整體架構:

 

它的服務過程是這樣的Prometheus daemon負責定時去目標上抓取metrics(指標) 數據,每個抓取目標需要暴露一個http服務的接口給它定時抓取。

Prometheus

支持通過配置文件、文本文件、zookeeper、Consul、DNS SRV lookup等方式指定抓取目標。支持很多方式的圖表可視化,例如十分精美的Grafana,自帶的Promdash,以及自身提供的模版引擎等等,還提供HTTP API的查詢方式,自定義所需要的輸出。

Alertmanager

是獨立於Prometheus的一個組件,可以支持Prometheus的查詢語句,提供十分靈活的報警方式。

PushGateway

這個組件是支持Client主動推送metrics到PushGateway,而Prometheus只是定時去Gateway上抓取數據。

如果有使用過statsd的用戶,則會覺得這十分相似,只是statsd是直接發送給服務器端,而Prometheus主要還是靠進程主動去抓取。

大多數Prometheus組件都是用Go編寫的,它們可以輕松地構建和部署為靜態二進制文件。訪問prometheus.io以獲取完整的文檔,示例和指南。

 

三、Prometheus的數據模型

Prometheus從根本上所有的存儲都是按時間序列去實現的,相同的metrics(指標名稱) 和label(一個或多個標簽) 組成一條時間序列,不同的label表示不同的時間序列。為了支持一些查詢,有時還會臨時產生一些時間序列存儲。

metrics name&label指標名稱和標簽

每條時間序列是由唯一的”指標名稱”和一組”標簽(key=value)”的形式組成。

指標名稱:一般是給監測對像起一名字,例如http_requests_total這樣,它有一些命名規則,可以包字母數字_之類的的。通常是以應用名稱開頭_監測對像_數值類型_單位這樣。例如:push_total、userlogin_mysql_duration_seconds、app_memory_usage_bytes。

標簽:就是對一條時間序列不同維度的識別了,例如一個http請求用的是POST還是GET,它的endpoint是什么,這時候就要用標簽去標記了。最終形成的標識便是這樣了:http_requests_total{method=”POST”,endpoint=”/api/tracks”}。

記住,針對http_requests_total這個metrics name無論是增加標簽還是刪除標簽都會形成一條新的時間序列。

查詢語句就可以跟據上面標簽的組合來查詢聚合結果了。

如果以傳統數據庫的理解來看這條語句,則可以考慮http_requests_total是表名,標簽是字段,而timestamp是主鍵,還有一個float64字段是值了。(Prometheus里面所有值都是按float64存儲)。

四、Prometheus四種數據類型

Counter

  Counter用於累計值,例如記錄請求次數、任務完成數、錯誤發生次數。一直增加,不會減少。重啟進程后,會被重置。

  例如:http_response_total{method=”GET”,endpoint=”/api/tracks”} 100,10秒后抓取http_response_total{method=”GET”,endpoint=”/api/tracks”} 100。

Gauge

  Gauge常規數值,例如 溫度變化、內存使用變化。可變大,可變小。重啟進程后,會被重置。

  例如: memory_usage_bytes{host=”master-01″} 100 < 抓取值、memory_usage_bytes{host=”master-01″} 30、memory_usage_bytes{host=”master-01″} 50、memory_usage_bytes{host=”master-01″} 80 < 抓取值。

Histogram

  Histogram(直方圖)可以理解為柱狀圖的意思,常用於跟蹤事件發生的規模,例如:請求耗時、響應大小。它特別之處是可以對記錄的內容進行分組,提供count和sum全部值的功能。

  例如:{小於10=5次,小於20=1次,小於30=2次},count=7次,sum=7次的求和值。

Summary

  Summary和Histogram十分相似,常用於跟蹤事件發生的規模,例如:請求耗時、響應大小。同樣提供 count 和 sum 全部值的功能。

  例如:count=7次,sum=7次的值求值。

  它提供一個quantiles的功能,可以按%比划分跟蹤的結果。例如:quantile取值0.95,表示取采樣值里面的95%數據。

五、安裝運行Prometheus(二進制版)

下面介紹如何使用Prometheus和Grafana對MySQL服務器性能進行監控。

我們用到了以下兩個exporter:

  • node_exporter – 用於機器系統數據收集
  • mysqld_exporter – 用於MySQL服務器數據收集

Grafana是一個開源的功能豐富的數據可視化平台,通常用於時序數據的可視化。它內置了以下數據源的支持:

下面是我們安裝時用到的架構圖:

使用Prometheus+Grafana監控MySQL實踐

 

 

下載安裝Prometheus(https://prometheus.io/download/)

[root@grafana]#wget https://github.com/prometheus/prometheus/releases/download/v1.6.2/prometheus-1.6.2.linux-amd64.tar.gz
[root@grafana]#tar xvf prometheus-1.6.2.linux-amd64.tar.gz -C /usr/local/
[root@grafana]#ln -sv /usr/local/prometheus-1.6.2.linux-amd64/ /usr/local/prometheus
[root@grafana]#cd /usr/local/prometheus

首先,修改Prometheus配置文件prometheus.yml (替換你要監控的IP地址):

[root@grafana ~]# cd /usr/local/prometheus-1.6.2/
[root@grafana prometheus-1.6.2]# cat prometheus.yml
global:
  scrape_interval:     60s
  evaluation_interval: 60s

scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ['localhost:9090']
        labels:
          instance: prometheus
 #靜態配置文件 
  - job_name: linux
    static_configs:
      - targets: ['client01:9100']
        labels:
          instance: myhosts
  #動態的配置文件
  - job_name: Development
    file_sd_configs:
      - files:
        - targets/Development/*.yaml
        refresh_interval: 5m

  - job_name: Production
    file_sd_configs:
      - files:
        - targets/Production/*.yaml
        refresh_interval: 5m

  啟動Prometheus

[root@grafana prometheus-1.6.2]# ./prometheus -config.file=prometheus.yml

  

Prometheus內置了一個web界面,我們可通過http://10.121.118.50:9090進行訪問:

 

 

 

 在Status->Targets頁面下,我們可以看到我們配置的兩個Target,它們的StateDOWN

 

 下一步我們需要安裝並運行exporter,下載exporters並解壓到被監控端服務器:

wget https://github.com/prometheus/node_exporter/releases/download/v1.2.2/node_exporter-1.2.2.linux-amd64.tar.gz

tar zxvf node_exporter-1.2.2.linux-amd64.tar.gz

cd node_exporter-1.2.2.linux-amd64

./node_exporter

  

六、安裝運行Grafana

Grafana安裝配置介紹

wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-4.2.0-1.x86_64.rpm

sudo yum localinstall grafana-4.2.0-1.x86_64.rpm

編輯配置文件/etc/grafana/grafana.ini,修改dashboards.json段落下兩個參數的值:

[dashboards.json]
enabled = true
path = /var/lib/grafana/dashboards

  

安裝儀表盤(Percona提供)

git clone https://github.com/percona/grafana-dashboards.git
cp -r grafana-dashboards/dashboards /var/lib/grafana

運行以下命令為Grafana打個補丁,不然圖表不能正常顯示:

sed -i 's/expr=\(.\)\.replace(\(.\)\.expr,\(.\)\.scopedVars\(.*\)var \(.\)=\(.\)\.interval/expr=\1.replace(\2.expr,\3.scopedVars\4var \5=\1.replace(\6.interval, \3.scopedVars)/' /usr/share/grafana/public/app/plugins/datasource/prometheus/datasource.js

sed -i 's/,range_input/.replace(\/"{\/g,"\\"").replace(\/}"\/g,"\\""),range_input/; s/step_input:""/step_input:this.target.step/' /usr/share/grafana/public/app/plugins/datasource/prometheus/query_ctrl.js

最后我們運行Grafana服務

systemctl daemon-reload
systemctl start grafana-server
systemctl status grafana-server

 我們可通過http://10.121.118.50:3000訪問Grafana網頁界面(缺省的帳號/密碼為admin/admin): 

 

 

然后我們到Data Sources頁面添加數據源:

  

 

prometheus 監控的指標請參考以下的文檔

 https://www.kancloud.cn/pshizhsysu/prometheus/1804341  

  

當前做了一些監控指標

 

統計在線主機數創建dashboard

 

 

sum(up ==1)

 

CPU 負載

Query:

node_load15

Legend format #顯示主機hostname 通過 lable 的key 獲取

{{instance}}

  

文件系統可用空間  

Query:

node_filesystem_free_bytes{mountpoint="/"}

Legend format #顯示主機hostname 通過 lable 的key 獲取

 

{{instance}}

 

 

 

 

 


免責聲明!

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



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