Prometheus+Grafana 搭建屬於自己的性能監控平臺(上)

第一步:需求分析

根據業務需求,原Psutil採集工具不滿足實際工作需求,決定更換採集工具。 聽朋友介紹就決定選擇使用 Prometheus+Grafana  來搭建性能測試使用的監控平臺,而且經過上級領導同意,效果滿意,則擴大到全環境監控。

首先我們需要了解一下

Prometheus 是什麼?

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

Prometheus 的優點

  • 非常少的外部依賴,安裝使用超簡單
  • 已經有非常多的系統集成 例如:docker HAProxy Nginx JMX等等
  • 服務自動化發現
  • 直接集成到代碼
  • 設計思想是按照分佈式、微服務架構來實現的

Prometheus 的特性

  • 自定義多維度的數據模型
  • 非常高效的存儲 平均一個採樣數據佔 ~3.5 bytes左右,320萬的時間序列,每30秒採樣,保持60天,消耗磁盤大概228G。
  • 強大的查詢語句
  • 輕鬆實現數據可視化

等等

相對於Graphite這種產品,還是有不少優點的。最讓我覺得不錯的是非常優秀的寫性能和讀取性能,它數據結構實現和OpenTSDB是有相似之處,有興趣可以看看這個文檔。解密OpenTSDB的表存儲優,這裏只簡單描述一下Prometheus的特性,感興趣的同學可以自己百科。

Prometheus 的系統架構

第二步:技術設計

由於我們公司是spring 後端,集成多個微服務,所以在進行性能測試過程中,需要監控所有微服務的資源使用情況,來定位具體性能瓶頸。另外由於是多多節點多服務,所以在現在性能監控平臺的時候,需要選擇一款部署塊,監控數據多,而且需要指出跨平臺以及支持快速擴充的一款產品,因此就決定使用Prometheus來做這個事情。

首先我們需要技術設計,來決定具體實現效果,請參考下圖。

框架圖

 

第三步:環境準備

首先每臺linux 機器均需要安裝 golang ,Prometheus基於go語言來進行數據統計的。

其次分別安裝上圖所描述的工具,另外如果是多節點監控,只需要一臺機器安裝grafana及Prometheus 就可以了,採集節點需要安裝 node_exporter 、process-exporter即可。

一般都是從官網下載二進制壓縮包,自行解壓完成。

https://prometheus.io/download/

 

 

 

https://github.com/ncabatoff/process-exporter/releases

 

下載後 使用rz 命令上傳至linux  使用解壓命令 進行解壓。

首先是 prometheus  然後是 node_exporter 最後是 *.exporter:      

tar -zxvf prometheus-2.7.1.linux-amd64.tar.gz 
  
tar -zxvf node_exporter-0.18.0.linux-amd64.tar.gz 

tar -zxvf process-exporter-0.4.0.linux-amd64.tar.gz

node_exporter 

   首先啓動node_exporter,node_exporter解壓完成後不需要任何操作,可以直接啓動。

建議使用 nohup 方式啓動。

nohup ./node_exporter >/dev/null &

驗證node_exporter : 瀏覽器輸入     http://ip:9100/metrics,如果打開如下圖則證明啓動成功。

process_exporter

然後是process_exporter,進入process_exporter目錄文件,設置配置文件,process_exporter解壓完後默認是沒有配置文件的,需要我們自己寫一個,啓動時通過-config.path 指定配置文件。讀配置文件的本質就是,讀裏面配置的服務名,通過服務名稱去進行pid的查找和分析,以下爲範例<這裏使用{{.Matches}}模板>匹配 指定系統微服務 :如下       

$ vim process.yaml

process_names:
  - name: "{{.Matches}}"
    cmdline:
    - '*.jar'                   #需要監控的進程名稱
  - name: "{{.Matches}}"
    cmdline:
    - '*.jar'                    #需要監控的進程名稱
  

啓動方式:nohup ./process-exporter -config.path process.yaml >/dev/null &

驗證process_exporter: 瀏覽器輸入 http://ip:9256/metrics    ,如果打開如下圖則證明啓動成功。

prometheus

進行Prometheus配置,以下是基本的配置,完整文檔介紹可以看 > 官方文檔:https://prometheus.io/docs/prometheus/2.13/configuration/configuration/

# my global config
global:
scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
 - targets:
   # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'

 # metrics_path defaults to '/metrics'
 # scheme defaults to 'http'.

 static_configs:
 - targets: ['localhost:9090']

 

如果後面配置了exporter組件,可以在scrape_configs裏添加job,例如:

scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:

    - targets: ['localhost:9090']            #博主的prometheus 安裝在本地所以直接用localhost

  - job_name: 'node'                     #腳本名稱,隨意看心情,自己能認識就行

    static_configs:
    - targets: ['localhost:9100']

  - job_name: 'process'

    static_configs:

    - targets: ['localhost:9256']

  - job_name: 'process-150'

    static_configs:
    - targets: ['192.168.64.150:9256']

  - job_name: 'node-150'

    static_configs:
    - targets: ['192.168.64.150:9100']


  - job_name: 'process-152'

    static_configs:
    - targets: ['192.168.64.152:9256']

  - job_name: 'node-152'
    static_configs:
                          

   啓動:

# 默認情況下,Prometheus會將其數據庫存儲在./data (flag—storage.tsdb.path)中
$ cd prometheus-2.13.1.linux-amd64/ 
$ nohup ./prometheus --config.file=prometheus.yml >/dev/null &

驗證:可以通過http://ip:9090訪問瀏覽器,或者http://ip:9090/metrics看是否能提供關於自身的各項指標

至此前期環境準備已經完成,下一篇將介紹如何進行Prometheus和Grafana 集成。

 

技術交流羣:1045153571

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章