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 是一个跨平台的开源的度量分析和可视化工具,能够经过将采集的数据查询而后可视化的展现,并及时通知。
效果图:
推荐阅读: