用Prometheus和Grafana監控你的服務器

本文介紹使用Prometheus和Grafana搭建服務器監控的步驟,並簡單介紹其中會涉及到的概念。

基礎

術語

  • 時序數據庫:Time Series Database(TSDB),顧名思義,就是存放時序數據的數據庫,每條數據有時間戳,支持對該類數據的快速讀寫、持久化、聚合查詢等操作。由於有了時間,就可以根據數據回溯,可用於監控、大數據分析、機器學習等。

    常見的TSDB有OpenTSDB、InfluxDBm、Prometheus等

  • Metrics:度量,是Prometheus中的核心概念。直接來看,度量就是一串標識符,例如http_requests_total表示所有http請求的總數

  • Tags:標籤,一個metric可能會記錄多種類型數據,比如http_requests_total,可能同時記錄了請求的uri,此時就需要標籤進行區隔,一個metric可以對應多個標籤,此時它就是一個多維數據。

    http_request_total{uri="ergedd/hello"}
    

簡介

Prometheus

Prometheus是一個開源的監控報警軟件,整體結構如下

在這裏插入圖片描述
主要組件如下

  • Prometheus Server:服務端,包含數據接收模塊、時序數據庫模塊、Http服務模塊等

    數據接收模塊用於連接監控目標,監控目標可以是靜態配置的單個服務,也可以是基於服務發現的得到的服務。

    時序數據庫模塊用於存儲接收到的時序數據,並存儲於硬盤

    Http服務暴露API,用於PromQL查詢

    服務端工作在pull模式,即監控目標需要暴露接口,服務端通過該接口主動拉取數據。

  • Push Gateway:推送網關。用於接收監控目標主動推送的數據,同時接受服務端的數據拉取

  • Alter Manager:報警管理器。接收來自服務端的報警推送,並將報警消息發送出去

  • UI:通過PromQL查詢服務端存儲的數據,並通過Web頁面的形式展示。一般我們不用Prometheus自帶的UI模塊,而是將數據接入到圖形展示功能更加強大的Grafana

此外,完整的Prometheus還應包含針對特定監控目標所編寫的Exporter,用於暴露監控數據,對於常規需求,會有開源公共的Exporter,對於特殊需求,可實現自定義的Exporter。

Grafana

Grafana是一個開源的數據分析和展示系統,有兩個主要優點:

  • 支持各種數據庫:Elasticsearch、Graphite、influxDB、Prometheus
  • 豐富的展示功能,可以圖表、文字等各種方式展示數據,全憑使用者的想象力。

搭建

這裏僅搭建一個最簡單的系統,打通從exporter到grafana數據流,push網關和報警管理器都暫時忽略。

Prometheus Exporter

監控Linux系統狀態,需要用到Node_Exporter,安裝它有兩種方式,一種是通過源碼編譯,另一種是直接下載運行,在這裏
在這裏插入圖片描述
下載合適的版本,運行,如下

$ ./node_exporter 
INFO[0000] Starting node_exporter (version=0.18.1, branch=HEAD, revision=3db77732e925c08f675d7404a8c46466b2ece83e)  source="node_exporter.go:156"
INFO[0000] Build context (go=go1.12.5, user=root@b50852a1acba, date=20190604-16:41:18)  source="node_exporter.go:157"
INFO[0000] Enabled collectors:                           source="node_exporter.go:97"
INFO[0000]  - arp                                        source="node_exporter.go:104"
INFO[0000]  - bcache                                     source="node_exporter.go:104"
. . . . . .
INFO[0000]  - zfs                                        source="node_exporter.go:104"
INFO[0000] Listening on :9100                            source="node_exporter.go:170"

此時在本地9100端口暴露了metrics數據,訪問http://localhost:9100/metrics可以獲取到所有數據,數據格式是直接可讀的:metric name + tag: value

$ curl http://localhost:9100/metrics

# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 1.8648e-05
go_gc_duration_seconds{quantile="0.25"} 1.8648e-05
go_gc_duration_seconds{quantile="0.5"} 4.6304e-05
go_gc_duration_seconds{quantile="0.75"} 4.6304e-05
go_gc_duration_seconds{quantile="1"} 4.6304e-05
go_gc_duration_seconds_sum 6.4952e-05
go_gc_duration_seconds_count 2
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 7
# HELP go_info Information about the Go environment.
# TYPE go_info gauge
go_info{version="go1.12.5"} 1
. . . . . .

Prometheus Server

Prometheus在github開源,因此可直接下載最新版,這裏就不再貼圖了,假設已經下載好了,我們從解壓後開始,在解壓後的目錄下可觀察到有如下幾個文件

$ ls
console_libraries  consoles  data  LICENSE  NOTICE  prometheus  prometheus.yml  promtool  tsdb

關注prometheus和prometheus.yml,前者是可執行文件,後者是配置文件。

在配置文件中加上exporter數據源,設置每5秒抓取一次數據,目標地址爲localhost:9100,抓取路勁默認爲/metrics,因此不用再指明。

  scrape_configs:
    - job_name: 'floyd_T490'
      scrape_interval: 5s
      static_configs:
      - targets: ['localhost:9100']

啓動,如下輸出代表成功。

./prometheus --config.file=prometheus.yml
level=info ts=2020-03-21T14:18:55.601Z caller=main.go:295 msg="no time or size retention was set so using the default time retention" duration=15d
. . . . . .
level=info ts=2020-03-21T14:18:55.914Z caller=main.go:775 msg="Completed loading of configuration file" filename=prometheus.yml
level=info ts=2020-03-21T14:18:55.914Z caller=main.go:630 msg="Server is ready to receive web requests."

Prometheus Server默認監聽9090端口,訪問localhost:9090來到server界面,可以進行簡單的查詢操作。
在這裏插入圖片描述

Grafana

Grafana可以安裝啓動,也可以用docker啓動,方便起見,我們使用Docker。

$ sudo docker run -p 3000:3000 grafana/grafana
t=2020-03-21T14:25:09+0000 lvl=info msg="Starting Grafana" logger=server version=6.6.2 commit=3fa63cfc34 branch=HEAD compiled=2020-02-20T12:03:49+0000
t=2020-03-21T14:25:09+0000 lvl=info msg="Config loaded from" logger=settings file=/usr/share/grafana/conf/defaults.ini
. . . . . .
t=2020-03-21T14:25:10+0000 lvl=info msg="Backend rendering via phantomJS" logger=rendering renderer=phantomJS
t=2020-03-21T14:25:10+0000 lvl=warn msg="phantomJS is deprecated and will be removed in a future release. You should consider migrating from phantomJS to grafana-image-renderer plugin." logger=rendering renderer=phantomJS
t=2020-03-21T14:25:10+0000 lvl=info msg="Initializing Stream Manager"
t=2020-03-21T14:25:10+0000 lvl=info msg="HTTP Server Listen" logger=http.server address=[::]:3000 protocol=http subUrl= socket=

安裝成功,訪問localhost:3000,可以看到登錄界面,默認用戶名密碼爲 admin / admin
在這裏插入圖片描述

添加Prometheus數據源

點擊設置 - Data Sources - Add data source - Prometheus

來到數據源設置界面,設置URL爲Prometheus Server地址http://localhost:9090,Access爲Browser,即通過瀏覽器訪問,點擊Save & Test,出現下圖所示的Data source is working提示即添加成功。
在這裏插入圖片描述

添加Dashboard

Grafana通過Dashboard展示數據,一個Dashboard上可以有多個圖表。可以自己創建,也可以將現有的導入,在Grafana Labs中,有很多分享的Dashboard,我們找一個合適的。
在這裏插入圖片描述
點進去,複製它的ID
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-r3P31UiC-1584803378870)(/home/floyd/PersonalCode/blog/source/_posts/用Prometheus和Grafana監控你的服務器/image-20200321223832988.png)]
回到Grafana,點擊 創建 - Import ,輸入上面的ID8919,加載成功可顯示如下界面
在這裏插入圖片描述
點擊導入,Awwwwwwwwwwwwwwwwwwwsome!!! 這裏詳細地展示了你的設備信息。
在這裏插入圖片描述

輕微探索

Dashboard中每個圖表上方都有菜單按鈕,可供我們修改
在這裏插入圖片描述
點擊View,全屏顯示該圖表;點擊Edit,進入編輯模式。我們點編輯看看
在這裏插入圖片描述
可以看到圖表中的每個數據來自於prometheus提供的metrics,並使用PromQL進行查詢的結果。自己添加圖標也是如此:寫PromQL - 配置圖表

至此,最簡單的監控服務搭建完成。

總結

本文簡單搭建了Linux的監控系統,主要目的在於展示Prometheus和Grafana的工作方式,算是科普文。也介紹了基本的工作原理,給擴展留下了空間。但其它重要部分需要讀者自己去探索,比如

  • Alert Manager
  • Push Gateway
  • Prometheus和其它TSDB的比較

瞭解更多

想要更深入瞭解Prometheus?

想要了解Vertx、Kubernetes如何集成Prometheus?

參考文檔

  1. TSDB維基百科

  2. 時間序列數據庫漫談

  3. Awesome time series database

  4. Prometheus Blog Series (Part 1): Metrics and Labels

  5. MONITORING LINUX HOST METRICS WITH THE NODE EXPORTER

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