Nginx+Lua 实现灰度发布

灰度发布,灰度发布(又名金丝雀发布)是指在黑与白之间,能够平滑过渡的一种发布方式。在其上可以进行A/B testing,即让一部分用户继续用产品特性A,一部分用户开始用产品特性B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B上面来。灰度发布可以保证整体系统的稳定,在初始灰度的时候就可以发现、调整问题,以保证其影响度。
Nginx+Lua 实现灰度发布 Nginx 博客地址:https://i4t.com lua脚本由人人网运维组长王李明提供

一、概念

灰度发布概念 按照一定的关系区分,分不分的代码进行上线,使代码的发布能平滑过渡上线 △使用用户的信息cookie等信息区别 △根据用户的ip地址区分 (本次使用ip地址区分) 灰度发布百度解释 灰度发布(又名金丝雀发布)是指在黑与白之间,能够平滑过渡的一种发布方式。在其上可以进行A/B testing,即让一部分用户继续用产品特性A,一部分用户开始用产品特性B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B上面来。灰度发布可以保证整体系统的稳定,在初始灰度的时候就可以发现、调整问题,以保证其影响度。 灰度期:灰度发布开始到结束期间的这一段时间,称为灰度期。 这里用于WEB系统新代码的测试发布,让一部分(IP)用户访问新版本,一部分用户仍然访问正常版本,其原理如图:

image_1dcgv2k5j15l7vd01p1h1n0a1jc03v.png-112kB

二、环境准备

2.1 安装LUA环境及相关库 模块目录全部放在/opt/下

#安装LuaJIT
wget -P /opt/ http://down.i4t.com/LuaJIT-2.0.5.tar.gz
tar xf LuaJIT-2.0.5.tar.gz
cd LuaJIT-2.0.5
make && make install

echo "export LUAJIT_LIB=/usr/local/lib" >>/etc/profile
echo "export LUAJIT_INC=/usr/local/include/luajit-2.0" >>/etc/profile
source /etc/profile

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

#下载ngx_devel_kit模块
wget -P /opt/ http://down.i4t.com/ngx_devel_kit-0.3.0.tar.gz
tar 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.gz
tar xf lua-nginx-module-0.10.13.tar.gz

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

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

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

#下载nginx

wget -P /opt/ http://down.i4t.com/nginx-1.14.2.tar.gz
tar xf  nginx-1.14.2.tar.gz

现在进行编译安装nginx

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-devel

2.创建用户
useradd -s /sbin/nologin nginx -M

3.编译安装nginx
cd /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

三、Nginx配置Lua

在nginx编译之后,我们需要先检查一下lua是否安装成功 1.首先检查nginx服务是否正常

3333333.png-315.4kB

2.验证lua模块是否成功

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

3.reload nginx检查是否正常

[root@abcdocker ~]# vim /usr/local/nginx/conf/nginx.conf
[root@abcdocker ~]# /usr/local/nginx/sbin/nginx  -t
nginx: the configuration file /usr/local/nginx-1.14.2/conf/nginx.conf syntax is ok
nginx: 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/test
test

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

image_1dcgodv4u1h7r82p4d1o4i1j6iu.png-25.6kB

四、安装redis

#yum安装
yum install epel-release
yum repolist
yum install redis -y

#修改redis ip
sed -i 's/127.0.0.1/0.0.0.0/g' /etc/redis.conf

#启动服务
service redis start

[root@abcdocker logs]# ps -ef|grep redis
redis    24334     1  1 05:04 ?        00:00:00 /usr/bin/redis-server 10.4.82.138:6379
root     24349 14935  0 05:04 pts/0    00:00:00 grep --color=auto redis

#检查Telnet是否正常
[root@abcdocker logs]# telnet 10.4.82.138 6379
Trying 10.4.82.138...
Connected to 10.4.82.138.
Escape character is '^]'.

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

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

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

cd /opt
git clone https://github.com/openresty/lua-resty-redis.git
cd lua-resty-redis/
make && make install

#当make完毕之后会生成我们的路径,复制相关路径就可以
ll /usr/local/lib/lua/resty/redis.lua

2.创建lua脚本

#创建lua目录
mkdir /usr/local/nginx/conf/lua

#脚本内容如下
cat > /usr/local/nginx/conf/lua/abcdocker.lua <

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

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

启动

$ /usr/local/nginx/sbin/nginx -t
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/nginx-1.14.2/conf/nginx.conf:22
nginx: the configuration file /usr/local/nginx-1.14.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.14.2/conf/nginx.conf test is successful

#这里的警告可以忽略,是由于lua_code_cache为off影响的

接下来就是部署2台tomcat 10.4.82.138 8080 10.4.82.140 8080 我这里就不写安装了,不会的可以参考下面文档 企业必会tomcat https://i4t.com/2514.html

1111.png-215.7kB

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

444.png-63.9kB

接下来我们进入到redis里面,让我们这个ip访问成prod1环境 这里的脚本逻辑解释如下 redis Key 为0 访问Pord1 redis Key 为空 访问Pord2 redis Key 为1 访问Pord2

image_1dcgu3tqq1mej1ieho181fp34vc35.png-124.7kB

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

image_1dcgu8ti51dqimjg1bked2jndg3i.png-206.9kB

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

相关文章:

  1. Kubernetes 1.14 二进制集群安装
  2. 搭建分布式文件系统FastDFS集群
  3. Kubenetes 1.13.5 集群二进制安装
  4. ngx_http_substitutions_filter_module 模块替换正文内容和URL