系統性能監控:Prometheus + Grafana 監控服務器性能


Prometheus 是一個開源的服務監控系統和時間序列數據庫,是一款開源系統監控和警報工具,在測試領域中,我們可以使用Promethues來監控壓力測試時服務端的性能。

Prometheus簡介

Prometheus使用Go語言開發,是最初在SoundCloud上構建的開源系統監控和警報工具,在2016年加入了Cloud Native Computing Foundation(CNCF)基金會,是繼Kubernetes之后該基金會的第二個托管項目。

主要特性

  • 多維數據模型,由指標名稱和鍵值對標識的時間序列數據度量

  • PromQL查詢語言

  • 不依賴分布式存儲;單個服務器節點是自治的

  • 通過HTTP使用pull模式收集時間序列數據

  • 支持通過中間網關推送時間序列數據

  • 通過服務發現或靜態配置發現目標對象

  • 支持多種圖形和儀表盤

組成

Prometheus由多個組件組成:

  • Prometheus主服務器:用於抓取並存儲時間序列數據
  • 客戶端庫:用於檢測應用程序代碼
  • 推送網關:支持短生命周期
  • 各種exporter:HAProxy,StatsD,Graphite等服務收集服務器性能數據
  • 警告管理器
  • 各種支持工具

架構

Prometheus + Grafana 監控系統性能

主要用到了Prometheus,node exporter和Grafana,Prometheus和node exporter收集保存服務器性能數據,Grafana用於圖形化展示數據。

圖片來源:https://www.ansible.com/blog/red-hat-ansible-tower-monitoring-using-prometheus-node-exporter-grafana

docker安裝Prometheus

docker hub地址:https://registry.hub.docker.com/r/prom/prometheus

docker安裝:

$ docker pull prom/prometheus

配置prometheus.yml文件

參考:https://github.com/prometheus/prometheus/blob/main/documentation/examples/prometheus.yml

新建/root/prometheus/prometheus.yml文件:

# 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']

啟動prometheus

我們要用Grafana展示prometheus監控數據,先啟動Grafana,Grafana安裝配置方法可參考:JMeter性能監控系統:Jmeter + InfluxDB + Grafana

啟動命令:

$ docker run -d -p 3000:3000 --name=grafana --network=grafana grafana/grafana:latest

啟動prometheus:

$ docker run -d --name prometheus --network grafana -p 9090:9090 -v /root/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus:latest --config.file=/etc/prometheus/prometheus.yml

啟動后使用docker ps查看是否啟動成功

[root@server prometheus]# docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED              STATUS              PORTS                    NAMES
0587156618c5   prom/prometheus:latest   "/bin/prometheus --c…"   About a minute ago   Up About a minute   0.0.0.0:9090->9090/tcp   prometheus
[root@server prometheus]# 

瀏覽器訪問:http://192.168.30.8:9090/
其中192.168.30.8是我的服務器主機ip地址

metrics為本地主機的數據,訪問http://192.168.30.8:9090/metrics 可以查看采集的數據。

安裝啟動node exporter

node exporter用於收集系統數據,下面介紹它的使用方法。

node exporter github地址:https://github.com/prometheus/node_exporter

下載node_exporter-1.1.2.linux-amd64.tar.gz,安裝到另一台Linux系統上

[root@Server2 exporter]# tar -xvzf node_exporter-1.1.2.linux-amd64.tar.gz
node_exporter-1.1.2.linux-amd64/
node_exporter-1.1.2.linux-amd64/LICENSE
node_exporter-1.1.2.linux-amd64/NOTICE
node_exporter-1.1.2.linux-amd64/node_exporter
[root@Server2 exporter]# cd node_exporter-1.1.2.linux-amd64/
[root@Server2 node_exporter-1.1.2.linux-amd64]# ls
LICENSE  node_exporter  NOTICE
[root@Server2 node_exporter-1.1.2.linux-amd64]# 

運行node exporter,端口號為9100(默認):

[root@Server2 node_exporter-1.1.2.linux-amd64]# nohup ./node_exporter --web.listen-address=":9100" &

啟動后,瀏覽器輸入http://192.168.30.9:9100/ 訪問Node Exporter采集的數據,192.168.30.9為安裝node exporter服務器的IP地址。

這時候在Prometheus是看不到這個節點的,因為沒有建立連接,接下來配置prometheus.yml文件

配置Prometheus

配置前面創建的prometheus.yml文件(啟動prometheus的那個服務器),在scrape_configs下添加:

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']

  - job_name: 'centos server2'
    static_configs:
    - targets: ['192.168.30.9:9100']

重啟Prometheus:

$ docker restart prometheus
prometheus

重啟成功后,刷新Prometheus頁面,發現上線成功

配置Grafana

點擊Configuration -> Data Sources -> Add data source 選擇Prometheus
然后配置URL:http://prometheus:9090
修改抓取時間,查詢超時時間等參數,設置完成后點擊Save & Test

接下來配置Dashboard,在https://grafana.com/grafana/dashboards 中搜索選擇別人開發好的面板,推薦https://grafana.com/grafana/dashboards/8919

點擊Dashboards -> Manage -> Import,輸入選擇的dashboard地址,點擊load,選擇prometheus數據源,配置完成后點擊Import。

監控界面顯示:

總結

Prometheus提供了各種exporter,用於收集各種數據庫、系統、中間件等性能數據,可參考https://prometheus.io/docs/instrumenting/exporters/ 查看所有官方或者第三方提供的exporter。

--THE END--

歡迎關注公眾號:「測試開發小記」及時接收最新技術文章!


免責聲明!

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



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