运维笔记

openresty+Lua+Redis灰度发布

Einic Yeo · 3月29日 · 2019年 · · · · · ·

灰度发布,简单来说,就是根据各种条件,让一部分用户使用旧版本,另一部分用户使用新版本。百度百科中解释:灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式。A版权声明:本文遵循 CC 4.0 BY-SA 版权协议,若要转载请务必附上原文出处链接及本声明,谢谢合作!B test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B上面 来。灰度发布可以保证整体系统的稳定,在初始灰度的时候就可以发现、调整问题,以保证其影响度。上述描述的灰度方案A和B需要等量的服务器,这里我们所做的灰度发布稍作改变:用1-2台机器作为B,B测试成功再部署A。用于WEB系统新代码的测试发布,让一部分(IP)用户访问新版本,一部分用户仍然访问正常版本,原理如图:

执行过程:版权声明:本文遵循 CC 4.0 BY-SA 版权协议,若要转载请务必附上原文出处链接及本声明,谢谢合作!
1、当用户请求到达前端web(代理)服务器Openresty,内嵌的lua模块解析Nginx配置文件中的lua脚本代码;
2、Lua获取客户端IP地址,去查询Redis中是否有该键值,如果有返回值执行@clien2,否则执行@clien版权声明:本文遵循 CC 4.0 BY-SA 版权协议,若要转载请务必附上原文出处链接及本声明,谢谢合作!t1。
3、Location @client2把请求转发给预发布服务器,location @client1把请求转发给生产服务器,服务器返回结果,整个过程完成。

Openresty部分配置如下:

upstream client1{ 
        server 192.168.0.225:85;  #模拟生产服务器
    }
upstream client2{
        server 192.168.0.225:86;  #模拟预发布服务器
    }

lua_package_path "/usr/local/openresty/lualib/lua-resty-redis/lib/?.lua;;";

server {

        listen       90;
        server_name  192.168.0.225;
        proxy_ignore_client_abort on;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_redirect off;
 
        location / {
            content_by_lua_file /usr/local/openresty/luasrc/huidu.lua;
        }
        
        location @client1{
                proxy_pass http://client1;
        }
        location @client2{
                proxy_pass http://client2;
        }
        error_log /data/wwwlogs/lua_err.log debug;
}

Lua脚本内容如下:

local redis = require "resty.redis" 
local cache = redis.new() 
cache:set_timeout(60000)

local ok, err = cache.connect(cache, '127.0.0.1', 6379) 
if not ok then 
    ngx.say("failed to connect:", err) 
    return 
end 

local red, err = cache:auth("foobared")
--配置Redis auth 密码为foobared
if not red then
    ngx.say("failed to authenticate: ", err)
    return
end

local local_ip = ngx.req.get_headers()["X-Real-IP"]
if local_ip == nil then
    local_ip = ngx.req.get_headers()["x_forwarded_for"]
end

if local_ip == nil then
    local_ip = ngx.var.remote_addr
end

--ngx.say("local_ip is : ", local_ip)
--ngx.exec前不能放ngx.say

local intercept = cache:get(local_ip) 

if intercept == local_ip then
        ngx.exec("@client2")
        return 
     end

     ngx.exec("@client1")

     local ok, err = cache:close() 
         
     if not ok then 
         ngx.say("failed to close:", err) 
         return 
     end

验证:
url:http://192.168.0.225:90 (模拟生产环境)
客户端IP:192.168.0.142(模拟公司办公网IP)

1、访问 http://192.168.0.225:90

2、 在Redis存入客户端IP(亦指定IP访问预版权声明:本文遵循 CC 4.0 BY-SA 版权协议,若要转载请务必附上原文出处链接及本声明,谢谢合作!发布环境)

[root@infvie luasrc]# redis-cli 
127.0.0.1:6379> auth foobared
OK
127.0.0.1:6379> set 192.168.0.142 192.168.0.142
OK
127.0.0.1:6379> keys *
1) "192.168.0.142"

然后刷新浏览器 ,再次访问:

3、在Redis中删除客户端IP:

127.0.0.1:6379> del 192.168.0.142
(integer) 1
127.0.0.1:6379> keys *
(empty list or set)

然后刷新浏览器 ,再版权声明:本文遵循 CC 4.0 BY-SA 版权协议,若要转载请务必附上原文出处链接及本声明,谢谢合作!次访问:

3 条回应
  1. daleyzou2019-8-10 · 11:16

    咋感觉大家所理解的灰度发布都是基于ip的,现在想基于requestParam和URL做灰度转发。发现大家都没这样的需求呀

    • Einic Yeo2019-8-10 · 23:58

      IP相对简单,但你可以找属于自己的业务的最优解

  2. marsman2020-7-9 · 10:17

    请问博主,用的是哪个博客程序啊,感觉这样界面样式、字体都特别好看! 希望得到回复~