Nginx+Lua 实现灰度发布

灰度发布,灰度发布(又名金丝雀发布)是指在黑与白之间,能够平滑过渡的一种发布方式。在其上可以进行A/B testing,即让一部分用户继续用产品特性A,一部分用户开始用产品特性B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B上面来。灰度发布可以保证整体系统的稳定,在初始灰度的时候就可以发现、调整问题,以保证其影响度。

Nginx+Lua 实现灰度发布


一、概念

灰度发布概念

按照一定的关系区分,分不分的代码进行上线,使代码的发布能平滑过渡上线

△使用用户的信息cookie等信息区别

△根据用户的ip地址区分 (本次使用ip地址区分)

灰度发布(又名金丝雀发布)是指在黑与白之间,能够平滑过渡的一种发布方式。在其上可以进行A/B testing,即让一部分用户继续用产品特性A,一部分用户开始用产品特性B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B上面来。灰度发布可以保证整体系统的稳定,在初始灰度的时候就可以发现、调整问题,以保证其影响度。

灰度期:灰度发布开始到结束期间的这一段时间,称为灰度期。

这里用于WEB系统新代码的测试发布,让一部分(IP)用户访问新版本,一部分用户仍然访问正常版本,其原理如图:

Nginx+Lua 实现灰度发布

二、环境准备

2.1 安装LUA环境及相关库

模块目录全部放在/opt/下

<code>#安装LuaJITwget -P /opt/ http://down.i4t.com/LuaJIT-2.0.5.tar.gztar xf LuaJIT-2.0.5.tar.gzcd LuaJIT-2.0.5make && make installecho "export LUAJIT_LIB=/usr/local/lib" >>/etc/profileecho "export LUAJIT_INC=/usr/local/include/luajit-2.0" >>/etc/profilesource /etc/profile/<code>

ngx_devel_kit和lua-nginx-module都是lua需要的模块

<code>#下载ngx_devel_kit模块wget -P /opt/ http://down.i4t.com/ngx_devel_kit-0.3.0.tar.gztar xf ngx_devel_kit-0.3.0.tar.gz#下载lua-nginx-module模块wget -P /opt/ http://down.i4t.com/lua-nginx-module-0.10.13.tar.gztar xf lua-nginx-module-0.10.13.tar.gz/<code>

还需要安装redis2-nginx-module模块

redis2-nginx-module 是一个支持 Redis 2.0 协议的 Nginx upstream 模块,它可以让 Nginx 以非阻塞方式直接防问远方的 Redis 服务,同时支持 TCP 协议和 Unix Domain Socket 模式,并且可以启用强大的 Redis 连接池功能

<code>wget -P /opt/ http://down.i4t.com/redis2-nginx-module-0.15.tar.gztar xf redis2-nginx-module-0.15.tar.gz/<code>

接下来就是安装Nginx了,版本我们采用目前稳定版1.14

<code>#下载nginxwget -P /opt/ http://down.i4t.com/nginx-1.14.2.tar.gztar xf  nginx-1.14.2.tar.gz/<code>

现在进行编译安装nginx

<code>1.安装依赖包yum install -y gcc glibc gcc-c++ prce-devel openssl-devel pcre-devel lua-devel libxml2 libxml2-devel libxslt-devel  perl-ExtUtils-Embed   GeoIP GeoIP-devel GeoIP-data zlib zlib-devel openssl  pcre pcre-devel gcc g++ gcc-c++ gd-devel2.创建用户useradd -s /sbin/nologin nginx -M3.编译安装nginxcd /opt/nginx-1.14.2./configure --prefix=/usr/local/nginx-1.14.2 \\--user=nginx --group=nginx --with-http_ssl_module \\--with-http_stub_status_module \\--add-module=/opt/lua-nginx-module-0.10.13 \\--add-module=/opt/ngx_devel_kit-0.3.0 \\--add-module=/opt/redis2-nginx-module-0.15#必须按照我的版本来,否则会出现问题make && make install#设置软连ln -s /usr/local/nginx-1.14.2 /usr/local/nginx#设置模块,否则nginx -t报错ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2错误提示如下:/usr/local/nginx/sbin/nginx -t/usr/local/nginx/sbin/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory/<code>

三、Nginx配置Lua

在nginx编译之后,我们需要先检查一下lua是否安装成功

1.首先检查nginx服务是否正常

Nginx+Lua 实现灰度发布

2.验证lua模块是否成功

<code>location /test {       default_type 'text/plain';       content_by_lua 'ngx.say("test")'; }/<code>

3.reload nginx检查是否正常

<code>[root@abcdocker ~]# vim /usr/local/nginx/conf/nginx.conf[root@abcdocker ~]# /usr/local/nginx/sbin/nginx  -tnginx: the configuration file /usr/local/nginx-1.14.2/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx-1.14.2/conf/nginx.conf test is successful[root@abcdocker ~]# /usr/local/nginx/sbin/nginx  -s reload[root@abcdocker ~]# curl 127.0.0.1/testtest/<code>

当访问/test时返回值也为test代表没有问题,也可以在浏览器访问

Nginx+Lua 实现灰度发布

四、安装redis

<code>#yum安装yum install epel-releaseyum repolistyum install redis -y#修改redis ipsed -i 's/127.0.0.1/0.0.0.0/g' /etc/redis.conf#启动服务service redis start[root@abcdocker logs]# ps -ef|grep redisredis    24334     1  1 05:04 ?        00:00:00 /usr/bin/redis-server 10.4.82.138:6379root     24349 14935  0 05:04 pts/0    00:00:00 grep --color=auto redis#检查Telnet是否正常[root@abcdocker logs]# telnet 10.4.82.138 6379Trying 10.4.82.138...Connected to 10.4.82.138.Escape character is '^]'./<code> 

编译安装:https://i4t.com/2796.html

五、接下来修改Nginx配置文件,引用lua脚本

1.下载加载lua库的redis脚本文件

<code>cd /optgit clone https://github.com/openresty/lua-resty-redis.gitcd lua-resty-redis/make && make install#当make完毕之后会生成我们的路径,复制相关路径就可以ll /usr/local/lib/lua/resty/redis.lua/<code>

2.创建lua脚本

<code>#创建lua目录mkdir /usr/local/nginx/conf/lua#脚本内容如下cat > /usr/local/nginx/conf/lua/abcdocker.lua <<eoflocal>/<code>

2.修改nginx.conf,引用redis.lua脚本

<code>cat /usr/local/nginx/conf/nginx.conf user  nginx;worker_processes  1;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;    lua_package_path  "/usr/local/lib/lua/resty/redis.lua";    lua_shared_dict ip_blacklist 1m;    server {        listen       80;        server_name  localhost;        location / {        lua_code_cache off;        proxy_set_header   Host             $host;        proxy_set_header   X-Real-IP        $remote_addr;        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;        content_by_lua_file "/usr/local/nginx/conf/lua/script/redis.lua";        }        location @prod1 {                 proxy_pass  http://10.4.82.140:8080;               }        location @prod2 {                 proxy_pass  http://10.4.82.138:8080;             }  }}#在http标签添加lua变量及i4t.conf#lua_package_path  "/usr/local/lib/lua/resty/redis.lua";#lua脚本路径#lua_shared_dict ip_blacklist 1m; 共享内存区域始终由当前nginx服务器实例中的所有nginx工作进程共享#content_by_lua_file 自定义lua脚本路径#lua_code_cache  nginx配置中将lua_code_cache配置成on/off来控制是否关闭lua 的cache缓存,如果设置为off.则每次修改lua脚本都会重新加载新的lua代码,从而实现快速调试响应。同时状态为off时启动或重启nginx都会提示:nginx: [alert] lua_code_cache is off; this will hurt performance in /path/to/nginx.conf。因为这会影响nginx性能表现。一般开发调试的时候使用off, 线上运行时设置为on。#localtion @prod1代表环境1#localtion @prod2代表环境2#更多变量地址:https://github.com/openresty/lua-nginx-module/<code>

启动

<code>$ /usr/local/nginx/sbin/nginx -tnginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/nginx-1.14.2/conf/nginx.conf:22nginx: the configuration file /usr/local/nginx-1.14.2/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx-1.14.2/conf/nginx.conf test is successful#这里的警告可以忽略,是由于lua_code_cache为off影响的/<code>

接下来就是部署2台tomcat

10.4.82.138 8080

10.4.82.140 8080

我这里就不写安装了,不会的可以参考下面文档

企业必会tomcat https://i4t.com/2514.html

Nginx+Lua 实现灰度发布

当我们默认访问的时候,不修改redis参数,不加任何变量访问的是prod2环境

默认访问如下图

Nginx+Lua 实现灰度发布

接下来我们进入到redis里面,让我们这个ip访问成prod1环境

这里的脚本逻辑解释如下

redis Key 为0 访问Pord1

redis Key 为空 访问Pord2

redis Key 为1 访问Pord2

Nginx+Lua 实现灰度发布

我们修改过nginx之后再次访问10.4.82.138 项目就变为tomcat代码了

Nginx+Lua 实现灰度发布

Lua脚本可以进行自定义,我这里只是简单的实现,后期会考虑使用cookie实现~



分享到:


相關文章: