2013年8月6日星期二

Use nginx + lua + memcache Grayscale post

 

one, gray publish Rationale

 

gray published in Baidu Encyclopedia explains:

 

release refers to the gray between black and white, a smooth transition can be distributed. AB test is a gray-scale release, which allows users to continue to use part A, some users started using B, if the user is no objection to the B, then gradually expanding to all users to migrate to B above. Grayscale release can guarantee the stability of the overall system, the initial gray when you can find adjustment to ensure that its degree of impact.

 

 

here for the WEB system testing new code release, let some (IP) users access to the new version, some users still access the normal version, and its principle is shown:

 

 

 

implementation process:

 

1, when a user requests reach the front-end proxy service Nginx, embedded lua module Nginx configuration file parsing lua script code;

 

2, Lua variables to get the client IP address, go to memcached caching query whether there is the key, if you have a return value is executed @ client_test, or perform @ client.

 

3, Location @ client_test forwards the request to deploy a new version of the code for the server, location @ client requests to deploy the normal version of the code server, the server returns the result. The process is complete.

 

following detailed description of the installation and configuration process.

 

 

Second, the installation and configuration process detailed

 

1, install nginx

 

install dependencies

 
  
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers make pcre-devel 

yum -y install gd gd2 gd-devel gd2-devel lua lua-devel

yum –y install memcached
 
 

 

download lua module, lua-memcache manipulation library files and nginx package

 
  
wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.18.tar.gz 

wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.8.5.tar.gz

wget https://github.com/agentzh/lua-resty-memcached/archive/v0.11.tar.gz

wget http://nginx.org/download/nginx-1.4.2.tar.gz

tar xvf nginx-1.4.2.tar.gz

cd 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/

make

make install
 
 

 

copy of memcached operations lua library file

 
  
tar xvf v0.11.tar.gz 

cp -r lua-resty-memcached-0.11/lib/resty/ /usr/lib64/lua/5.1/
 
 


 

configure nginx

 
  
#vim /soft/nginx/conf/nginx.conf 

worker_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;

}

}

}
 
 

 

detect configuration file.

 
  
#/soft/nginx/sbin/nginx -t 

nginx: the configuration
file /soft/nginx/conf/nginx.conf syntax is ok

nginx: configuration
file /soft/nginx/conf/nginx.conf test is successful
 
 

 

start nginx

 
  
/soft/nginx/sbin/nginx
 
 

 

start the memcached service

 
  
memcached -u nobody -m 1024 -c 2048 -p 11211 –d
 
 

 

 

three, test verification

 

test lua module is operating normally

 

visit http:// test server ip address / hello. If the display: hello, lua installation was successful.

 

 

 

 

on another test machine (here 192.168.200.29) set up two virtual hosts, one with 80 ports is normal code execution, a port is 81 performs the gradation test code.

 

 

in memcached in to your client IP address is key, value is 1. Here my IP is 192.168.68.211.

 
  
telnet localhost 11211 

Trying ::
1...

Connected to localhost.

Escape character is
'^]'.

set
192.168.68.211 0 0 1

1

STORED

get
192.168.68.211

VALUE
192.168.68.211 9 1

1

END

quit
 
 

 

 

Note:

 

set value after a key value.

 

192.168.68.211 key value is needed which is the IP address of the gradation test;

 

0 indicates a key associated with the custom data;

 

3600 indicates that the key value is valid;

 

1 represents the value of the corresponding key value in bytes.

 

 

following access Nginx, results in line with expectations, my IP has the value stored in memcached, so forwards the request to perform a host grayscale test code.

 

 

memcached Remove from my host IP values.

 

 

requested again Nginx, forwards the request to the content host to perform normal code.

 

 

 

entire configuration is not complicated, the entire judgment process on the service impact is very small. If you need to use this system to see for yourself the best lua script.

 

没有评论:

发表评论