54

使用Prometheus+Grafana快速打造高逼格监控平台

 4 years ago
source link: https://www.tuicool.com/articles/26jYzeE
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
M3Mv2qr.png!web

Prometheus 介绍

Prometheus是一套开源监控系统,使用Go语言开发,是 Google BorgMon 监控系统的类似实现。

Prometheus 的基本原理是通过HTTP协议周期性抓取被监控组件的状态,任意组件只要提供对应的HTTP接口就可以接入监控,是比较适合 Docker,Kubernetes 等环境的监控系统之一。输出监控信息的HTTP接口被称作 exporter

Prometheus 架构

Prometheus 使用的是 Pull 模型,Prometheus Server 通过 HTTP 的 pull 方式到各个目标拉取监控数据。

Znq6rqU.png!web 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)。

  • 度量指标(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 里的度量指标有以下几种类型:

  • 计数器(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 里,可以从中抓取采样值的端点称为实例,为了性能扩展而复制出来的多个这样的实例形成了一个任务。

  • 任务(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 也是一样

下载依赖的包

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 编译安装

./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 配置

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 VTS Stats Dashboard

  • Redis Dashboard

  • Node Exporter Dashboard

效果图:

  • Nginx 监控

VJnUFjJ.jpg!web Nginx 监控
  • Redis 监控

IN3QRn3.jpg!web Redis 监控
  • Node 监控

vaqMfaZ.jpg!web Node 监控
-- END --
欢迎长按下图关注公众号 DigNew
mqEzuyV.png!web 欢迎长按下图关注公众号 DigNew

推荐阅读:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK