使用Prometheus+Grafana快速打造高逼格監控平臺

Prometheus 介紹

Prometheus 是一套開源監控系統,使用Go語言開發,是 Google BorgMon 監控系統的相似實現。html

Prometheus 的基本原理是經過HTTP協議週期性抓取被監控組件的狀態,任意組件只要提供對應的HTTP接口就能夠接入監控,是比較適合 Docker,Kubernetes 等環境的監控系統之一。輸出監控信息的HTTP接口被稱做 exporternode

Prometheus 架構

Prometheus 使用的是 Pull 模型,Prometheus Server 經過 HTTP 的 pull 方式到各個目標拉取監控數據。linux

Prometheus 架構圖

  • Retrieval:中定義了 Prometheus Server 須要從哪些地方拉取數據
    • Jobs / Exporters:Prometheus 能夠從 Jobs 或 Exporters 中拉取監控數據。Exporter 以 Web API 的形式對外暴露數據採集接口。
    • Prometheus Server:Prometheus 還能夠從其餘的 Prometheus Server 中拉取數據。
    • Pushgateway:對於一些以臨時性 Job 運行的組件,Prometheus 可能尚未來得及從中 pull 監控數據的狀況下,這些 Job 已經結束了,Job 運行時能夠在運行時將監控數據推送到 Pushgateway 中,Prometheus 從 Pushgateway 中拉取數據,防止監控數據丟失。
    • Service:是指 Prometheus 能夠動態的發現一些服務,拉取數據進行監控,如從DNS,Kubernetes,Consul 中發現。
  • Storage:即 Prometheus 的存儲,利用 Prometheus Server 的本地存儲。
  • PromQL:是 Prometheus 的查詢語句,利用 PromQL 能夠和一些 WEBUI (如 Grafana )集成
  • AlertManager:是一個獨立於 Prometheus 的外部組件,用於監控系統的告警,經過配置文件能夠配置一些告警規則,Prometheus 會把告警推送到 AlertManager。

Prometheus的特色

  • 多維度數據模型,一個時間序列由一個度量指標和多個標籤鍵值對肯定;
  • 靈活的查詢語言,對收集的時序數據進行重組;
  • 強大的數據可視化功能,除了內置的瀏覽器,也支持跟 Grafana 集成;
  • 高效的存儲,內存加本地磁盤,可經過功能分片和聯盟來擴展性能;
  • 運維簡單,只依賴本地磁盤,Go 二進制安裝包沒有任何其餘庫包依賴;
  • 精確告警;
  • 很是多的客戶端庫;
  • 提供了許多導出器來收集常見系統指標;
  • 能夠經過中間網關進行時序列數據推送;
  • 經過服務發現或者靜態配置來發現目標服務對象。

核心概念

數據模型

Prometheus 從根本上存儲的全部數據都是時間序列數據(Time Serie Data,簡稱時序數據)。時序數據是具備時間戳的數據流,該數據流屬於某個度量指標(Metric)和該度量指標下的多個標籤(Label)。nginx

  • 度量指標(Metric):描述了被監控的某個測量特徵。度量指標名稱由ASCII字母、數字、下劃線和冒號組成,須匹配正則表達式[a-zA-Z_:][a-zA-Z0-9_:]*
  • 標籤(Tag):對於同一個度量指標,不一樣標籤值組合會造成特定維度的時序。標籤開啓了 Prometheus 的多維數據模型。Prometheus 的查詢語言能夠經過度量指標和標籤對時序數據進行過濾和聚合。標籤名稱能夠包含 ASCII 字母、數字和下劃線,須匹配正則表達式 [a-zA-Z_][a-zA-Z0-9_]*,帶有 _ 下劃線的標籤名稱保留爲內部使用。標籤值能夠包含任意 Unicode 字符,包括中文。
  • 採樣值(Sample):時序數據其實就是一系列的採樣值。每一個採樣值包括:
    • 一個64位的浮點數據
    • 一個精確到毫秒的時間戳
  • 註解(Annotation):一個註解由一個度量指標和一組標籤鍵值對構成。

度量指標

Prometheus 裏的度量指標有如下幾種類型:git

  • 計數器(Counter):一種累計型的度量指標,它是一個只能遞增的數值。計數器主要用於統計相似於服務器請求數、任務完成數和錯誤出現次數這樣的數據。
  • 計量器(Gauge):表示一個既能夠增長,又能夠減小的度量指標。計量器主要用於測量相似於溫度、內存使用量這樣的瞬時數據。
  • 直方圖(Histogram):對觀察結果進行採樣(一般是請求持續時間或者響應大小這樣的數據),並在可配置的桶中進行統計。有如下幾種方式來產生直方圖(假設度量指標爲<basename>):
    • 按桶計數,至關於<basename>_bucket{le="<upper inclusive bound>"}
    • 採樣值總和,至關於<basename>_sum
    • 採樣值總數,至關於<basename>_count,也等同於把全部採樣值放到一個桶裏來計數<basename>_bucket{le="+Inf"}
  • 彙總(Summary):對觀察結果進行採樣。除了能夠統計採樣值總和和總數,還能按照分位數統計。有如下幾種方式來產生彙總(假設度量指標爲<basename>):
    • 分位數,也就是採樣值小於該分位數的個數佔總數的比例小於φ,至關於 <basename>{quantile="<φ>"}
    • 採樣值總和,至關於<basename>_sum
    • 採樣值總數,至關於<basename>_count

任務和實例

在 Prometheus 裏,能夠從中抓取採樣值的端點稱爲實例,爲了性能擴展而複製出來的多個這樣的實例造成了一個任務。github

  • 任務(Job):抓取所屬任務。
  • 實例(Instance):抓取來源實例。

Prometheus 監控實戰

Prometheus 安裝部署

# 1. 下載
wget https://github.com/prometheus/prometheus/releases/download/v2.10.0/prometheus-2.10.0.linux-amd64.tar.gz

# 2. 解壓
tar zxvf prometheus-2.10.0.linux-amd64.tar.gz

# 3. 啓動
cd prometheus-2.10.0.linux-amd64
./prometheus --config.file=prometheus.yml
複製代碼

exporter 安裝部署

node_exporter

# 1. 下載
wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.darwin-amd64.tar.gz

# 2. 解壓
tar zxvf node_exporter-0.18.1.darwin-amd64.tar.gz

# 3. 啓動
cd node_exporter-0.18.1.darwin-amd64
./node_exporter
複製代碼

nginx-vts-exporter

這裏我使用的是 openresty,若是是使用 nginx 也是同樣web

下載依賴的包正則表達式

cd /usr/local/src/openresty-1.15.8.1/bundle

# openssl
wget https://www.openssl.org/source/openssl-1.1.1c.tar.gz

# ngx_cache_purge
wget http://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.zip -O ngx_cache_purge.zip

# nginx-module-vts
wget http://github.com/vozlt/nginx-module-vts/archive/v0.1.18.zip -O nginx-module-vts.zip

# upstream 健康檢查
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip -O nginx_upstream_check_module.zip
unzip nginx_upstream_check_module.zip
cd nginx-1.15.8
patch -p1 < ../nginx_upstream_check_module-master/check_1.14.0+.patch
複製代碼

nginx 編譯安裝redis

./configure --prefix=/opt/openresty \
--with-http_auth_request_module \
--with-http_realip_module \
--with-http_v2_module \
--with-debug \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module  \
--with-http_gunzip_module \
--with-http_random_index_module \
--with-threads \
--with-pcre \
--with-luajit \
--with-mail \
--with-file-aio \
--with-http_v2_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_dav_module \
--with-http_sub_module \
--with-http_addition_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-http_secure_link_module \
--with-stream_ssl_preread_module \
--with-openssl=./bundle/openssl-1.1.1c \
--add-module=./bundle/ngx_cache_purge-2.3 \
--add-module=./bundle/nginx-module-vts-0.1.18 \
--add-module=./bundle/nginx_upstream_check_module-master \
-j2  

gmake && gmake install
複製代碼

nginx 配置json

http {
    vhost_traffic_status_zone;
    vhost_traffic_status_filter_by_host on;   #開啓此功能,會根據不一樣的server_name進行流量的統計,不然默認會把流量所有計算到第一個上。
    ...
    server {
        ...
		location /status {
    		vhost_traffic_status_display;
    		vhost_traffic_status_display_format html;
		}
        # vhost_traffic_status off;
    }
}

複製代碼

若是不想要監控這個域名,這須要在 server 模塊中配置 vhost_traffic_status off;

nginx-vts-exporter 安裝

# 1. 下載
wget https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.10.0/nginx-vts-exporter-0.10.0.linux-amd64.tar.gz
# 2. 解壓
tar zxvf nginx-vts-exporter-0.10.0.linux-amd64.tar.gz
# 3. 啓動
cd nginx-vts-exporter-0.10.0.linux-amd64
複製代碼

redis_exporter

# 1. 下載
wget https://github.com/oliver006/redis_exporter/releases/download/v1.0.3/redis_exporter-v1.0.3.linux-amd64.tar.gz
# 2. 解壓
tar zxvf redis_exporter-v1.0.3.linux-amd64.tar.gz
# 3. 啓動
cd redis_exporter-v1.0.3.linux-amd64
./redis_exporter -redis.addr 192.168.102.55:7000 -redis.password test --web.listen-address 0.0.0.0:9121
複製代碼

Prometheus 添加 Exporter 配置

安裝完了 exporter 以後,須要將 exporter 添加到 prometheus 的配置中,簡單配置以下:

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']
    
 - job_name: "node"
 static_configs:
 - targets: 
 - '192.168.26.18:9100'
 - '192.168.102.51:9100'
 - '192.168.102.58:9100'
 - '192.168.102.59:9100'
      #labels:
      # instance: "192.168.26.18:9100"
      # env: "pro"
      # name: "192.168.26.18"
    
 - job_name: 'nginx'
 static_configs:
 - targets: 
 - '192.168.102.51:9913'
 - '192.168.102.58:9913'
 - '192.168.102.59:9913'
    
 - job_name: 'redis-exporter'
 file_sd_configs:
 - files: ['./redis.json']

複製代碼

redis.json 配置文件以下:

[{
        "targets": [
            "192.168.102.53:9121",
            "192.168.102.53:9122",
            "192.168.102.54:9121",
            "192.168.102.54:9122",
            "192.168.102.55:9121",
            "192.168.102.55:9122",
            "192.168.102.70:9121",
            "192.168.102.70:9122",
            "192.168.102.71:9121",
            "192.168.102.71:9122",
            "192.168.102.72:9121",
            "192.168.102.72:9122"
        ],
        "labels": {
            "service": "redis"
        }
    }
]
複製代碼

重啓 prometheus 便可。最後就是配置 Grafana 可視化了。

Grafana 安裝配置

Grafana 是一個跨平臺的開源的度量分析和可視化工具,能夠經過將採集的數據查詢而後可視化的展現,並及時通知。

  1. Grafana 安裝指南

  2. Grafana 添加 Prometheus 數據源

  3. 導入 Dashboard

效果圖:

  • Nginx 監控

Nginx 監控

  • Redis 監控

Redis 監控

  • Node 監控

Node 監控

-- END --
歡迎長按下圖關注公衆號 DigNew

歡迎長按下圖關注公衆號 DigNew


推薦閱讀:

相關文章
相關標籤/搜索