V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
wsgzao
V2EX  ›  程序员

Prometheus 监控 Nginx

  •  
  •   wsgzao ·
    wsgzao · 2019-03-26 20:11:56 +08:00 · 2375 次点击
    这是一个创建于 1830 天前的主题,其中的信息可能已经有所发展或是发生改变。

    前言

    Nginx 官网有介绍各种监控方案,以前我们常用 stub_status 和 Log 日志来实现 Nginx 监控。本文主要介绍基于 Prometheus 的 2 种监控方案 nginx-lua-prometheus 和 nginx-vts-exporter,在真实的生产环境中你可能需要研究和选择哪种方法才是适合你的,F5 收购 Nginx 后的未来让我们一起拭目以待。

    Prometheus 监控 Nginx

    更新历史

    2019 年 03 月 25 日 - 初稿

    阅读原文 - https://wsgzao.github.io/post/prometheus-nginx/

    扩展阅读

    Monitoring NGINX - https://www.nginx.com/blog/monitoring-nginx/


    Nginx 监控

    官网介绍的监控方案 - https://www.nginx.com/blog/monitoring-nginx/

    Prometheus 集成的 HTTP exporter 方案 - https://prometheus.io/docs/instrumenting/exporters/#http

    聊聊 Nginx 的监控 - https://zhuanlan.zhihu.com/p/34050716

    使用 rpmbuild 制作 Nginx 的 RPM 包 - https://wsgzao.github.io/post/rpmbuild/

    Prometheus 监控 Nginx

    nginx-lua-prometheus

    Nginx 需要添加 Lua 扩展

    https://github.com/knyar/nginx-lua-prometheus

    # 下载 redis_exporter
    https://github.com/knyar/nginx-lua-prometheus/releases
    wget https://github.com/knyar/nginx-lua-prometheus/archive/0.20181120.tar.gz
    tar xf 0.20181120.tar.gz
    cd nginx-lua-prometheus-0.20181120
    
    # 创建 prometheus.lua 目录
    mkdir -p /etc/nginx/lua/
    cp prometheus.lua /etc/nginx/lua/
    
    # 编辑 nginx 配置文件修改,注意修改 lua_package_path "/etc/nginx/lua/prometheus.lua";
    vim /etc/nginx/nginx.conf
    
    lua_shared_dict prometheus_metrics 10M;
    lua_package_path "/etc/nginx/lua/prometheus.lua";
    init_by_lua '
      prometheus = require("prometheus").init("prometheus_metrics")
      metric_requests = prometheus:counter(
        "nginx_http_requests_total", "Number of HTTP requests", {"host", "status"})
      metric_latency = prometheus:histogram(
        "nginx_http_request_duration_seconds", "HTTP request latency", {"host"})
      metric_connections = prometheus:gauge(
        "nginx_http_connections", "Number of HTTP connections", {"state"})
    ';
    log_by_lua '
      metric_requests:inc(1, {ngx.var.server_name, ngx.var.status})
      metric_latency:observe(tonumber(ngx.var.request_time), {ngx.var.server_name})
    ';
    
    # 创建 nginx-lua-prometheus
    vim /etc/nginx/sites-available/nginx-lua-prometheus
    
    server {
      listen 9145;
      location /metrics {
        content_by_lua '
          metric_connections:set(ngx.var.connections_reading, {"reading"})
          metric_connections:set(ngx.var.connections_waiting, {"waiting"})
          metric_connections:set(ngx.var.connections_writing, {"writing"})
          prometheus:collect()
        ';
      }
    }
    
    # 创建软链接
    cd /etc/nginx/sites-enabled/
    ln -s ../sites-available/prometheus
    
    # 测试 Nginx 语法并 reload 测试 metrics
    nginx -t
    nginx -s reload
    curl http://127.0.0.1:9145/metrics
    
    # iptables rule for Prometheus Nginx 
    -A INPUT -s xxx -p tcp --dport 9145 -j ACCEPT
    
    

    nginx-vts-exporter

    https://github.com/hnlq715/nginx-vts-exporter

    对方正在输入中

    Grafana

    nginx-lua-prometheus

    https://grafana.com/dashboards/462

    nginx-vts-exporter

    https://grafana.com/dashboards/2949

    参考文献

    https://prometheus.io/docs/instrumenting/exporters/#http

    6 条回复    2019-03-27 15:16:52 +08:00
    mritd
        1
    mritd  
       2019-03-26 20:15:12 +08:00
    好熟悉的域名啊,访问过很多次
    wsgzao
        2
    wsgzao  
    OP
       2019-03-26 22:52:54 +08:00   ❤️ 1
    @mritd #1 欢迎来新加坡折腾 K8S 哈
    mritd
        3
    mritd  
       2019-03-26 22:55:05 +08:00
    @wsgzao #2 k8s 还没折腾明白 哈哈
    funky
        4
    funky  
       2019-03-27 00:30:13 +08:00
    +1 前端时间 prometheus + grafana ,集成各种监控很方便
    szkoda
        5
    szkoda  
       2019-03-27 15:09:07 +08:00
    Sea 用的不是 mesos 吗
    wsgzao
        6
    wsgzao  
    OP
       2019-03-27 15:16:52 +08:00
    @szkoda #5 Mesos Swarm Kubernetes 都有,不同团队之前的技术选型不同,目前大趋势是逐步统一至 K8S
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2780 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 12:32 · PVG 20:32 · LAX 05:32 · JFK 08:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.