【云原生 | Kubernetes 系列】---Prometheus监控Nginx

Prometheus监控Nginx

步骤:

  1. nginx 添加扩展模块nginx-module-vts,获取Nginx状态数据
  2. 通过Nginx_exporter将Nginx状态转换为Prometheus可读数据
  3. 将输入导入Prometheus监控
  4. 通过Grafana展示数据

1. 扩展Nginx状态数据

1.1 安装Nginx

root@zookeeper-1:/opt# tar xf nginx-1.20.2.tar.gz 
root@zookeeper-1:/opt# cd nginx-1.20.2/

1.2 克隆nginx-module-vts源码

root@zookeeper-1:/opt/nginx-1.20.2# git clone https://github.com/vozlt/nginx-module-vts.git
Cloning into 'nginx-module-vts'...
remote: Enumerating objects: 903, done.
remote: Counting objects: 100% (96/96), done.
remote: Compressing objects: 100% (52/52), done.
remote: Total 903 (delta 56), reused 64 (delta 44), pack-reused 807
Receiving objects: 100% (903/903), 1.18 MiB | 742.00 KiB/s, done.
Resolving deltas: 100% (606/606), done.

1.3 编译安装

编译时除了常用的模块,需要加入nginx-module-vts模块
尽量每一步都用echo $?确认是否成功

root@zookeeper-1:/opt/nginx-1.20.2# ./configure --prefix=/apps/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-file-aio --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=/opt/nginx-1.20.2/nginx-module-vts/
root@zookeeper-1:/opt/nginx-1.20.2# echo $?
0
root@zookeeper-1:/opt/nginx-1.20.2# make -j 4
root@zookeeper-1:/opt/nginx-1.20.2# echo $?
0
root@zookeeper-1:/opt/nginx-1.20.2# make install 
root@zookeeper-1:/opt/nginx-1.20.2# echo $?
0

确认nginx已经加入了nginx-module-vts

root@zookeeper-1:/opt/nginx-1.20.2# ln -sf /apps/nginx/sbin/nginx /usr/bin/nginx
root@zookeeper-1:/opt/nginx-1.20.2# nginx -V
nginx version: nginx/1.20.2
built by gcc 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1) 
built with OpenSSL 1.1.1f  31 Mar 2020
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-file-aio --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=/opt/nginx-1.20.2/nginx-module-vts/

1.4 打开nginx状态页

编辑/apps/nginx/conf/nginx.conf,加入以下配置打开状态页

http {
    vhost_traffic_status_zone;

    ...

    server {

        ...

        location /status {
            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
        }
    }
}

启动Nginx服务后,通过/status访问到状态页

【云原生 | Kubernetes 系列】---Prometheus监控Nginx_第1张图片

2. 基于Nginx_exporter收集数据

2.1 下载nginx_exporter

root@zookeeper-1:/opt/nginx-1.20.2# cd /apps/
root@zookeeper-1:/apps# wget https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.10.0/nginx-vts-exporter-0.10.0.linux-amd64.tar.gz
root@zookeeper-1:/apps# tar xf nginx-vts-exporter-0.10.0.linux-amd64.tar.gz
root@zookeeper-1:/apps# ln -sf /apps/nginx-vts-exporter-0.10.0.linux-amd64/nginx-vts-exporter /usr/bin/

2.2 配置service

/etc/systemd/system/nginx-vts-exporter.service

[Unit]
Description=Nginx
After=network.target

[Service]
ExecStart=/usr/bin/nginx-vts-exporter -nginx.scrape_uri=http://192.168.31.121/status/format/json

[Install]
WantedBy=multi-user.target

开启nginx_exporter,正常开启后9913端口被打开

systemctl enable --now  nginx-vts-exporter.service
root@zookeeper-1:/apps# ss -tnl|grep 9913
LISTEN  0       4096                    *:9913                 *:*  

尝试访问/metrics页,可以看到nginx开头的键值,如果没有,检查service文件

【云原生 | Kubernetes 系列】---Prometheus监控Nginx_第2张图片
【云原生 | Kubernetes 系列】---Prometheus监控Nginx_第3张图片

3. Prometheus 配置

prometheus.yaml

  - job_name: "Nginx-exporter-9913"
    metrics_path: /metrics
    static_configs:
      - targets: ["192.168.31.121:9913"]

启动之后

【云原生 | Kubernetes 系列】---Prometheus监控Nginx_第4张图片
【云原生 | Kubernetes 系列】---Prometheus监控Nginx_第5张图片

4. Grafana配置

2949

【云原生 | Kubernetes 系列】---Prometheus监控Nginx_第6张图片

你可能感兴趣的:(prometheus,Docker,K8s,nginx,云原生,kubernetes)