简介

Openresty是一个基于NginxLua的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关

最近需要nginx暴露promethues的metrics来完成监控,所以要使用nginx-lua-promethues,就学习一下openresty的安装以及添加模块的步骤

安装并启动

1.下载源码

[deploy@huanggai /deployment]$ wget https://openresty.org/download/openresty-1.13.6.1.tar.gz

2.解压

[deploy@huanggai /deployment]$ tar -zvxf ./openresty-1.13.6.1.tar.gz

3.创建目录

我创建了一个link,指向openresty的源码,又创建了一个目录用来保存安装后的文件

[deploy@huanggai /deployment]$ ln -s openresty-1.13.6.1 openresty-source

[deploy@huanggai /deployment]$ mkdir openresty

4.configure

--prefix用来指定安装的目录,我选择的就是我刚才创建的目录

[deploy@huanggai /deployment/openresty-source]$ ./configure --prefix=/deployment/openresty

5.gmake

[deploy@huanggai /deployment/openresty-source]$ gmake

6.gmake install

[deploy@huanggai /deployment/openresty-source]$ gmake install

这时候就看到在/deployment/openresty/目录下有编译完成的文件了

[deploy@huanggai /deployment/openresty]$ ll
total 264
drwxrwxr-x  2 deploy deploy   4096 Aug  9 14:50 bin
-rw-rw-r--  1 deploy deploy  22924 Aug  9 14:50 COPYRIGHT
drwxrwxr-x  6 deploy deploy   4096 Aug  9 14:50 luajit
drwxrwxr-x  6 deploy deploy   4096 Aug  9 14:50 lualib
drwxrwxr-x  6 deploy deploy   4096 Aug  9 14:50 nginx
drwxrwxr-x 44 deploy deploy   4096 Aug  9 14:50 pod
-rw-rw-r--  1 deploy deploy 218352 Aug  9 14:50 resty.index
drwxrwxr-x  5 deploy deploy   4096 Aug  9 14:50 site

7.修改配置文件

[deploy@huanggai /deployment/openresty]$ cd /deployment/openresty/nginx/conf
[deploy@huanggai /deployment/openresty/nginx/conf]$ vim nginx.conf

...
    server {
        # 修改端口为7900
        listen       7900;
...

8.启动

[deploy@huanggai /deployment/openresty]$ cd /deployment/openresty/bin
[deploy@huanggai /deployment/openresty/bin]$ ./openresty

9.验证

打开浏览器访问http://huanggai:7900/

openresty-hello

看到这个表示启动成功

10. 添加模块

使用opm安装

[deploy@huanggai /deployment/openresty/bin]$ ./opm get knyar/nginx-lua-prometheus

11. 再次修改nginx配置文件

这里在初始化promethues的metrics的时候,我增加了一个url的label,来实现url粒度的监控

[deploy@huanggai /deployment/openresty/nginx/conf]$ vim nginx.conf
...
http {
    # 添加如下内容
    ...
    lua_shared_dict prometheus_metrics 10M;
    init_by_lua '
        prometheus = require("prometheus").init("prometheus_metrics")
        metric_requests = prometheus:counter(
        "nginx_http_requests_total", "Number of HTTP requests", {"host","url", "status"})
        metric_latency = prometheus:histogram(
        "nginx_http_request_duration_seconds", "HTTP request latency", {"host","url"})
        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.request_uri ,ngx.var.status})
        metric_latency:observe(tonumber(ngx.var.request_time), {ngx.var.server_name,ngx.var.request_uri})
    ';
    ...
    # 这里需要暴露一个端口给promethues采集
    server {
      listen 9145;
      allow 192.168.1.197;
      deny all;
      location /metrics {
          content_by_lua '
              prometheus:collect()
          ';
      }
    }
    ...
...

12. 重启nginx并验证

[deploy@huanggai /deployment/openresty/bin]$ ./openresty -s reload
[deploy@huanggai /deployment/openresty/bin]$ curl 192.168.1.197:9145/metrics
# HELP nginx_http_request_duration_seconds HTTP request latency
# TYPE nginx_http_request_duration_seconds histogram
...
nginx_http_request_duration_seconds_bucket{host="",url="/metrics",le="00.005"} 349
nginx_http_request_duration_seconds_bucket{host="",url="/metrics",le="00.010"} 349
nginx_http_request_duration_seconds_bucket{host="",url="/metrics",le="00.020"} 349
nginx_http_request_duration_seconds_bucket{host="",url="/metrics",le="00.030"} 349
nginx_http_request_duration_seconds_bucket{host="",url="/metrics",le="00.050"} 349
...
nginx_http_request_duration_seconds_bucket{host="",url="/metrics",le="+Inf"} 349
nginx_http_request_duration_seconds_bucket{host="localhost",url="/",le="00.005"} 10
nginx_http_request_duration_seconds_bucket{host="localhost",url="/",le="00.010"} 10
nginx_http_request_duration_seconds_bucket{host="localhost",url="/",le="00.020"} 10
nginx_http_request_duration_seconds_bucket{host="localhost",url="/",le="00.030"} 10
nginx_http_request_duration_seconds_bucket{host="localhost",url="/",le="00.050"} 10
nginx_http_request_duration_seconds_bucket{host="localhost",url="/",le="00.075"} 10
nginx_http_request_duration_seconds_bucket{host="localhost",url="/",le="00.100"} 10
...

验证成功!