Nginx + Lua + redis implement gray release:
Gray release refers to a method for smooth transition between black and white
AB test is a gray release method that allows some users to continue using a and some users to use B. If the user has no objection to B, the scope is gradually expanded, migrate all users to B. Gray release ensures the stability of the entire system. problems can be detected and adjusted during initial gray release to ensure the impact.
Gray release can ensure the stability of application systems and reduce the scope of users affected by product upgrades. Some users can also participate in product tests in advance according to certain policies, so as to obtain user feedback early, improve application functions
Principle: Use nginx for load balancing and reverse proxy. nginx is embedded with the Lua module. parse and execute the script logic written by Lua. You can use Lua to parse cookies and access redis, some gray-scale release and shunting strategies are to associate them in redis through cookies.
Execution Process:
- When a user request arrives at the previous proxy service nginx, the embedded Lua module parses the Lua script code in the nginx configuration file.
- Obtain the Client IP address from the Lua variable and check whether the value is in the redis cache. If there is a return value, run @ client_test; otherwise, run @ client.
- Location @ client_test: the request is forwarded to the new version of the Code server. [email protected]: The request is forwarded to the server with the normal version of the Code, [email protected] forwards the request to a server with the code of the normal version. The server returns the result. Complete the process
Detailed installation and configuration process:
- Install nginx
Install the dependency package: yum-y install GCC gcc-C ++ Autoconf libjpeg-devel libpng-devel FreeType-devel libxml2 libxml2-devel zlib-devel glibc-devel glib2 glib2-devel Bzip2 bzip2-devel ncurses- devel curl-devel plugin e2fsprogs-devel krb5 krb5-devel libidn-devel OpenSSL-devel OpenLDAP openldap-devel nss_ldap openldap-clients openldap-servers make PCRE-develyum-y install GD GD2 Gd-devel gd2-devel Lua LUA-develyum-y install memcached
Download the Lua module, Lua-memcache operating library file, and nginx package
wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.18.tar.gzwget https://github.com/chaoslawful/lua-nginx-module/archive/v0.8.5.tar.gzwget https://github.com/agentzh/lua-resty-memcached/archive/v0.11.tar.gzwget http://nginx.org/download/nginx-1.4.2.tar.gztar xvf nginx-1.4.2.tar.gzcd nginx-1.4.2/./configure --prefix=/soft/nginx/ --with-http_gzip_static_module --add-module=/root/ngx_devel_kit-0.2.18/ --add-module=/root/lua-nginx-module-0.8.5/makemake install
Copy the memcached operation library file of Lua.
tar xvf v0.11.tar.gzcp -r lua-resty-memcached-0.11/lib/resty/ /usr/lib64/lua/5.1/
Configure nginx
#vim /soft/nginx/conf/nginx.confworker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; proxy_next_upstream error timeout; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $http_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 100m; client_body_buffer_size 256k; proxy_connect_timeout 180; proxy_send_timeout 180; proxy_read_timeout 180; proxy_buffer_size 8k; proxy_buffers 8 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; upstream client { server 192.168.200.29:80; } upstream client_test { server 192.168.200.29:81; } server { listen 80; server_name localhost; location / { content_by_lua ‘ clientIP = ngx.req.get_headers()["X-Real-IP"] if clientIP == nil then clientIP = ngx.req.get_headers()["x_forwarded_for"] end if clientIP == nil then clientIP = ngx.var.remote_addr end local memcached = require "resty.memcached" local memc, err = memcached:new() if not memc then ngx.say("failed to instantiate memc: ", err) return end local ok, err = memc:connect("127.0.0.1", 11211) if not ok then ngx.say("failed to connect: ", err) return end local res, flags, err = memc:get(clientIP) if err then ngx.say("failed to get clientIP ", err) return end if res == "1" then ngx.exec("@client_test") return end ngx.exec("@client") ‘; } location @client{ proxy_pass http://client; } location @client_test{ proxy_pass http://client_test; } location /hello { default_type ‘text/plain‘; content_by_lua ‘ngx.say("hello, lua")‘; } location = /50x.html { root html; } }}
Check configuration file:
/Usr/local/nginx/sbin/nginx-T
OK is displayed.
Start nginx
/Usr/local/nginx/sbin/nginx
Start memchaed Service
Chkconfig -- level 2345 memcached on
/Etc/init. d/memcached START | stop
Test whether the Lua module is running normally.
telnet localhost 11211Trying ::1...Connected to localhost.Escape character is ‘^]‘.set 192.168.68.211 0 3600 1STOREDget 192.168.68.211VALUE 192.168.68.211 9 1ENDquit
Note:
The first value after set is the key value.
192.168.68.211: the key value is the IP address that needs to be tested in gray scale;
0 indicates a custom data related to the key;
3600 indicates the effective time of the key value;
1 indicates the number of bytes of the value corresponding to the key.
The following access to nginx is as expected. My IP Address has already stored the value in memcached, so the request is forwarded to the host that executes the grayscale test code.
Access http: // test Server IP Address/Hello. If Lua is displayed, the installation is successful.
On another test machine (192.168.200.29), set two virtual hosts. One is to run the normal code using port 80, and the other is to run the grayscale test code on port 81.
In memcached, the IP address of your client is the key and the value is 1. Here, my IP address is 192.168.68.211.
Delete the IP address of my host from memcached.
Request nginx again and forward it to the host that executes the normal code.
The entire configuration is not complex, and the entire judgment process has little impact on the service. If you need to use this system, you 'd better check the Lua script by yourself.
Nginx + Lua + redis implement phased release _ Test