• 【云原生 | 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

    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    确认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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    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;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

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

    请添加图片描述

    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/
    
    • 1
    • 2
    • 3
    • 4

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

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

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

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

    请添加图片描述
    请添加图片描述

    3. Prometheus 配置

    prometheus.yaml

      - job_name: "Nginx-exporter-9913"
        metrics_path: /metrics
        static_configs:
          - targets: ["192.168.31.121:9913"]
    
    • 1
    • 2
    • 3
    • 4

    启动之后

    请添加图片描述
    请添加图片描述

    4. Grafana配置

    2949

    请添加图片描述

  • 相关阅读:
    axios的原理及实现一个简易版axios
    FPGA学习笔记——知识点总结
    一款超实用的AI漫画生成器,支持9种漫画风格,无限免费使用
    【UniApp】-uni-app-CompositionAPI传递数据
    一文搞懂泛型——泛型简介/优点/上界/通配符
    任意文件读取
    arm gcc 编译器在eclipse中修改堆栈大小
    Nacos -- 集群部署
    【锂离子电池】
    An动画基础之元件的影片剪辑动画与传统补间
  • 原文地址:https://blog.csdn.net/qq_29974229/article/details/126700676