构建一个可以统计 qps 的nginx服务的Dockerfile
Posted Shendu.cc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了构建一个可以统计 qps 的nginx服务的Dockerfile相关的知识,希望对你有一定的参考价值。
github 项目地址: https://github.com/SilentCC/nginx_lua_qps_count
nginx 是经常会用到的web 服务器,它有出色的并发性能,因此尝尝被用来当做负载均衡服务器,反向代理服务器。
nginx 的安装很简单,我通常是使用docker 安装,在编写dockerfile 时加入自己想要的功能,这样的话就可以随时随地拿来用。
统计qps 功能的nginx 服务,我只是下面的将github上两个项目的内容组合在一起。
一个是:加上lua模块的nginx
感谢上面两个作者,给我们提供了方便。我在其中也修改了上面两个项目的内容。
其中Dockerfile:
FROM ubuntu:16.04 ENV VER_NGINX_DEVEL_KIT=0.2.19 ENV VER_LUA_NGINX_MODULE=0.10.7 ENV VER_NGINX=1.10.3 ENV VER_LUAJIT=2.0.5 ENV NGINX_DEVEL_KIT ngx_devel_kit-${VER_NGINX_DEVEL_KIT} ENV LUA_NGINX_MODULE lua-nginx-module-${VER_LUA_NGINX_MODULE} ENV NGINX_ROOT=/nginx ENV WEB_DIR ${NGINX_ROOT}/html ENV LUAJIT_LIB /usr/local/lib ENV LUAJIT_INC /usr/local/include/luajit-2.0 RUN apt-get -qq update #RUN apt-get install --assume-yes apt-utils RUN apt-get -qq -y install wget # ***** BUILD DEPENDENCIES ***** # Common dependencies (Nginx and LUAJit) RUN apt-get -qq -y install make # Nginx dependencies RUN apt-get -qq -y install libpcre3 RUN apt-get -qq -y install libpcre3-dev RUN apt-get -qq -y install zlib1g-dev RUN apt-get -qq -y install libssl-dev # LUAJit dependencies RUN apt-get -qq -y install gcc RUN apt-get install libluajit-5.1-dev --assume-yes # ***** DOWNLOAD AND UNTAR ***** # Download RUN wget http://nginx.org/download/nginx-${VER_NGINX}.tar.gz RUN wget http://luajit.org/download/LuaJIT-${VER_LUAJIT}.tar.gz RUN wget https://github.com/simpl/ngx_devel_kit/archive/v${VER_NGINX_DEVEL_KIT}.tar.gz -O ${NGINX_DEVEL_KIT}.tar.gz RUN wget https://github.com/openresty/lua-nginx-module/archive/v${VER_LUA_NGINX_MODULE}.tar.gz -O ${LUA_NGINX_MODULE}.tar.gz # Untar RUN tar -xzvf nginx-${VER_NGINX}.tar.gz && rm nginx-${VER_NGINX}.tar.gz RUN tar -xzvf LuaJIT-${VER_LUAJIT}.tar.gz && rm LuaJIT-${VER_LUAJIT}.tar.gz RUN tar -xzvf ${NGINX_DEVEL_KIT}.tar.gz && rm ${NGINX_DEVEL_KIT}.tar.gz RUN tar -xzvf ${LUA_NGINX_MODULE}.tar.gz && rm ${LUA_NGINX_MODULE}.tar.gz # ***** BUILD FROM SOURCE ***** # LuaJIT WORKDIR /LuaJIT-${VER_LUAJIT} RUN make RUN make install # Nginx with LuaJIT WORKDIR /nginx-${VER_NGINX} RUN ./configure --prefix=${NGINX_ROOT} --with-ld-opt="-Wl,-rpath,${LUAJIT_LIB}" --add-module=/${NGINX_DEVEL_KIT} --add-module=/${LUA_NGINX_MODULE} RUN make -j2 RUN make install RUN ln -s ${NGINX_ROOT}/sbin/nginx /usr/local/sbin/nginx # ***** MISC ***** WORKDIR ${WEB_DIR} EXPOSE 4397 EXPOSE 80 EXPOSE 443 # ***** CLEANUP ***** RUN rm -rf /nginx-${VER_NGINX} RUN rm -rf /LuaJIT-${VER_LUAJIT} RUN rm -rf /${NGINX_DEVEL_KIT} RUN rm -rf /${LUA_NGINX_MODULE} COPY ngx_lua_reqstatus/ /nginx COPY proxy_test.conf /nginx/conf/nginx.conf # TODO: Uninstall build only dependencies? # TODO: Remove env vars used only for build? # This is the default CMD used by nginx:1.9.2 image CMD ["nginx", "-g", "daemon off;"]
proxy_test.conf 的内容如下
worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main \'$remote_addr - $remote_user [$time_local] "$request" \' # \'$status $body_bytes_sent "$http_referer" \' # \'"$http_user_agent" "$http_x_forwarded_for"\'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; map $http_upgrade $connection_upgrade { default upgrade; \'\' close; } lua_shared_dict statics_dict 1M; # 初始化变量 lua_package_path "/nginx/?.lua"; #路径 log_by_lua_file "/nginx/hook.lua"; # 添加此句 server { listen 0.0.0.0:4397; location /{ content_by_lua_file "/nginx/status.lua"; } } server { listen 80 default_server; access_log off; return 200 \'Hello, World! - nginx\\n\'; } server { listen 80; server_name recomm.cnblogs.com; access_log off; location /{ resolver 127.0.0.11; proxy_set_header Host $host; proxy_pass http://dev-recomm_web; } } }
而 ngx_lua_reqstatus 文件夹里的内容则是第一个项目中的.lua 文件。没有添加可视化。
最后的效果,就是使用curl 命令:
curl http://127.0.0.1:4397/?domain=xxx.xxxx.xxx Server Name key: xxx.xxx.xxx Seconds SinceLast: 1.0710000991821 Average Req Time Sec: 0.98439929089361 Request Count: 283 Requests Per Secs: 264.23900447452 5xx num: 0
qps 的统计原理是:每次获取qps时,会统计当前获取和上次获取之间的请求数和相差时间,从而算出qps.
最新版本镜像已经上传到dockerhub 仓库中
以上是关于构建一个可以统计 qps 的nginx服务的Dockerfile的主要内容,如果未能解决你的问题,请参考以下文章
一行 Shell 通过 Nginx access 日志实时统计单台机器QPS