Prometheus 是一套開源監控系統,使用Go語言開發,是 Google BorgMon 監控系統的相似實現。html
Prometheus 的基本原理是經過HTTP協議週期性抓取被監控組件的狀態,任意組件只要提供對應的HTTP接口就能夠接入監控,是比較適合 Docker,Kubernetes 等環境的監控系統之一。輸出監控信息的HTTP接口被稱做 exporter。node
Prometheus 使用的是 Pull
模型,Prometheus Server 經過 HTTP 的 pull 方式到各個目標拉取監控數據。linux
Prometheus 從根本上存儲的全部數據都是時間序列數據
(Time Serie Data,簡稱時序數據)。時序數據是具備時間戳的數據流,該數據流屬於某個度量指標(Metric)和該度量指標下的多個標籤(Label)。nginx
[a-zA-Z_:][a-zA-Z0-9_:]*
。[a-zA-Z_][a-zA-Z0-9_]*
,帶有 _ 下劃線的標籤名稱保留爲內部使用。標籤值能夠包含任意 Unicode 字符,包括中文。Prometheus 裏的度量指標有如下幾種類型:git
<basename>
):
<basename>_bucket{le="<upper inclusive bound>"}
<basename>_sum
<basename>_count
,也等同於把全部採樣值放到一個桶裏來計數<basename>_bucket{le="+Inf"}
<basename>
):
φ
,至關於 <basename>{quantile="<φ>"}
<basename>_sum
<basename>_count
在 Prometheus 裏,能夠從中抓取採樣值的端點稱爲實例,爲了性能擴展而複製出來的多個這樣的實例造成了一個任務。github
# 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
複製代碼
# 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
複製代碼
這裏我使用的是 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
複製代碼
# 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
複製代碼
安裝完了 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 是一個跨平臺的開源的度量分析和可視化工具,能夠經過將採集的數據查詢而後可視化的展現,並及時通知。
效果圖:
推薦閱讀: