一、概念

1.灰度发布是什么:

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

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

二、环境准备

模块目录全部放在/opt/下
官网:http://luajit.org/download.html

```html
#安装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 installecho "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服务是否正常

lsof -i :80
ps -ef | grep nginx
cur
cur -I 127.0.0.1

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代表没有问题,也可以在浏览器访问

四、安装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 '^]'.

五、修改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 <<EOF
local local_ip = ngx.req.get_headers()["X-Real-IP"];
if local_ip == nil thenlocal_ip = ngx.req.get_headers()["x_forwarded_for"];
end
if local_ip == nil thenlocal_ip = ngx.var.remote_addr;
endlocal function close_redis(redis_instance)if not redis_instance thenreturnendlocal ok,err = redis_instance:close();if not ok thenngx.say("close redis error : ",err);end
endlocal redis = require("resty.redis");
--local redis = require "redis"
-- 创建一个redis对象实例。在失败,返回nil和描述错误的字符串的情况下
local redis_instance = redis:new();
--设置后续操作的超时(以毫秒为单位)保护,包括connect方法
redis_instance:set_timeout(1000)
--建立连接
local ip = '10.4.82.138'
local port = 6379
--尝试连接到redis服务器正在侦听的远程主机和端口
local ok,err = redis_instance:connect(ip,port)
if not ok thenngx.say("connect redis error : ",err)return close_redis(redis_instance);
end--Redis身份验证
--local auth,err = redis_instance:auth("");
--if not auth then
--    ngx.say("failed to authenticate : ",err)
--end--调用API进行处理
--local resp,err = redis_instance:set("msg","hello world")
--if not resp then
--    ngx.say("set msg error : ",err)
--    return close_redis(redis_instance)
--end--调用API获取数据
local resp, err = redis_instance:get(local_ip)
if not resp thenngx.say("get msg error : ", err)return close_redis(redis_instance)
end--得到的数据为空处理
if resp == nil thenngx.exec("@prod1") --比如默认值(对应Nginx local下面的配置)
end
--ngx.say("msg:",resp)
if resp ==  "0"  thenngx.exec("@prod1");--return
--close_redis(redis_instance)
elsengx.exec("@prod2");
--  return  close_redis(redis_instance)
endclose_redis(redis_instance)
EOF#这里匹配了2套环境,prod01 && prod02 在nginx.conf会体现出来
#注意修改redis地址

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

启动

$ /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 808010.4.82.140 8080

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

默认访问如下图

进入到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代码了

Nginx+Lua 实现灰度发布详细步骤相关推荐

  1. nginx+lua+redis 灰度发布实现方案

    背景: 公司要把现有的某传统项目进行微服务化,拆分后要分批次预发布,实现某部分使用户使用微服务模块,其他用户使用传统项目.待微服务稳定.无bug后全部用户迁移至微服务系统. 以上为背景,实现此方案使用 ...

  2. k8s 使用Nginx Ingress实现灰度发布和蓝绿发布

    **导语:**云原生最佳实践系列,涵盖了灰度发布.弹性伸缩.集群迁移.网络通信.应用容器化改造等等场景,针对各行业面临的应用现状,提出最佳解决方案,并提供详细操作指导,希望对您有所帮助. Ingres ...

  3. Nginx访问本地静态资源详细步骤

    目录 一.Nginx简介 二.Nginx访问本地静态资源详细步骤 1.首先下载Nginx 2.将下载好的压缩包解压到合适位置 3.启动Nginx,进入localhost 界面如下图所示说明启动成功,默 ...

  4. VS2012+Win7网站发布详细步骤

    VS2012+Win7网站发布详细步骤 VS2012+Win7网站发布详细步骤 本机环境: 本文分三个部分介绍Web项目发布的常规方法,大神级别可以略过,主要是为了方便一些初学者. 第一部分:VS20 ...

  5. spring项目打包发布详细步骤

    spring项目打包发布详细步骤 1.添加pom文件配置 <packaging>jar</packaging> 2.在IDEA中打开 Run - > Edit Confi ...

  6. 微信小程序发布详细步骤

    第一步:检查代码质量 运行微信开发者工具 ==> 点击详情,选择性能分析,进行代码质量的扫描. 把未通过的部分改改. 第二步:发布体验版 点击上传 ==> 填写版本号,项目备注 ==> ...

  7. nginx的卸载与安装详细步骤

    一.nginx的安装 nginx的安装可以分为两种安装方式,一种是编译安装,另一种是yum安装,本文主要分享编译安装过程与代码.编译安装可以根据自身的情况选择功能需要,进行定制,更加节约资源,性能更好 ...

  8. Windows+Nginx+IIS做图片分布式存储详细步骤

    最近几天,一直在学习nginx在windows平台下的使用,为了寻找几种大量图片分布式存储而且有相对简单的存储方案 nginx是一种,还找到一种MongoDB GridFS 这两种方案我还是比较中意的 ...

  9. nginx部署前端项目的详细步骤

    前言 在前端开发过程中经常是需要把前端静态资源放到服务器中看效果,这时经常用到nginx来配置. 1. 什么是nginx Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/ ...

最新文章

  1. 利用mysql建立随机森林_随机森林算法实例 - osc_4imme0wh的个人空间 - OSCHINA - 中文开源技术交流社区...
  2. 25~50K|云视科技SLAM算法工程师/机器人算法软件工程师招聘(社招+实习)
  3. 设置 HTTP HEADER 字段来提高 Web 安全性
  4. 【OpenGL】六、Visual Studio 2019 配置 GitHub ( 提取和拉取简介 | 拉取远程代码 )
  5. Elasticsearch的或且非及其组合
  6. Ubuntu Server 16.04 LTS上安装Docker(使用脚本的方式)
  7. 服务器信息安全策略,信息安全工程师考试重点之定制Web服务器的安全策略和安全机制...
  8. 解决SimpleButton被移除后保持OVER状态
  9. 【script】python requests模块中cookie的使用
  10. QTP中VBS脚本下FSO、WSH的应用(二)
  11. sql2000数据类型对应的数值_Qlikview---数据类型
  12. 基于windows PE文件的恶意代码分析;使用SystemInternal工具与内核调试器研究windows用户空间与内核空间...
  13. java自学之路-day19
  14. C++ ---------------- 成员函数指针揭秘
  15. 数学一年级应用题_一年级数学下册100以内加减法应用题,给孩子下载!
  16. redis分布式锁的 5个坑
  17. 影响中国互联网的100人
  18. word脚注、尾注小技巧|怎么删除尾注的横线|怎么快速删除页眉的横线|怎么快速将尾注和脚注转为带方括号的格式
  19. linux(ubuntu)下实现鼠标侧键映射双击、鼠标中键 快捷键的方法—— 使用xbindkeys
  20. PHP - 下载/传输远程服务器上的文件到本地服务器

热门文章

  1. 英辞流——坚若金刚与穿行无碍:物质的三态
  2. なな : 动态jianmo
  3. 2022P气瓶充装考试练习题及答案
  4. mac安装Texpad:提示无法打开,因为APPLE无法检查其是否包含恶意软件解决方案
  5. MT4/MT5使用dll调用matlab
  6. 理解资产定价领域的“均衡模型”:APT、CAPM、马歇尔、瓦尔拉斯、无套利均衡
  7. 超猛tuntap虚拟网卡实现超猛UDP隧道
  8. 比尔·盖茨最新分享:ChatGPT的发展,不止于此
  9. 小文智能意图训练详解
  10. 【MD5】快速实现MD5加密解密(盐值)