使用prometheus+ grafana+nginx-module-vts 模块监控openresty

nginx-module-vts 是一个很不错的nginx 模块,我们可以用来,方便的分析系统的请求状态
同时支持基于prometheus 的监控, 我参考openresty 的docker镜像已经制作了一个集成模块
的镜像 dalongrong/openresty-nginx-module-vts

环境准备

  • docker-compose 文件
version: "3"
services:
  api:
   build: ./
   image: dalongrong/demo-ngx-vts
   ports:
   - "8080:80"
  g:
    image: grafana/grafana
    ports:
    - "3000:3000"
  p:
    image: prom/prometheus
    volumes:
    - "./prometheus.yml:/etc/prometheus/prometheus.yml"
    ports:
    - "9090:9090"
  • 集成nginx-module-vts的openresty 配置
    dockerfile
FROM dalongrong/openresty-nginx-module-vts
COPY nginx.conf usr/local/openresty/nginx/conf/
EXPOSE 80
EXPOSE 443
EXPOSE 88

nginx.conf

worker_processes 1;
events {
    worker_connections 1024;
}
http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;
    gzip on;
    vhost_traffic_status_zone;
    real_ip_header X-Forwarded-For;
    real_ip_recursive on;
    upstream backend-app {
       server 10.15.0.66:80 weight=20 max_fails=2 fail_timeout=30s;
     }
    server {
        listen 80;
        charset utf-8;
        location / {
            index index.html index.htm;
            proxy_set_header Host $http_host;
            proxy_set_header X-Forwarded-For $remote_addr;
            client_body_buffer_size 10M;
            client_max_body_size 10G;
            proxy_buffers 1024 4k;
            proxy_read_timeout 300;
            proxy_next_upstream error timeout http_404;
            proxy_pass http://backend-app;
        }
        location /status {
           vhost_traffic_status_display;
           vhost_traffic_status_display_format html;
        }
        location /alert {
         default_type text/html;
         content_by_lua_block{
             ngx.say([[<script>alert("error")</script>]])
         }
        }

        location /ip {
            default_type text/html;
            content_by_lua_block{
                ngx.say(ngx.var.remote_addr)
            }
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
    }
    server {
        listen 88;
        server_name localhost;
        charset utf-8;
        location / {
            root html;
            index index.html index.htm;
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
    }
}
  • prometheus监控配置
    prometheus.yml
scrape_configs:
  - job_name: nginx
    metrics_path: /status/format/prometheus
    static_configs:
      - targets: ['api:80']

启动&&集成测试

  • 启动
docker-compose up -d
  • 界面效果


  • 集成grafana
    添加prometheus datasource 配置

    添加dashboard

说明

nginx-module-vts 是一个很方便的模块,集成进openresty 中,我们可以快速的实现系统的信息监控,同时模块的文档也很全,类似的也
有一个基于opentracing 的模块nginx-opentracing

参考资料

https://github.com/rongfengliang/openresty_nginx-module-vts_prometheus_grafana
https://github.com/vozlt/nginx-module-vts
https://github.com/rongfengliang/openresty-nginx-module-vts
https://www.cnblogs.com/rongfengliang/p/9752800.html

原文地址:https://www.cnblogs.com/rongfengliang/p/10038706.html