一、灰度發布原理說明

灰度發布在百度百科中解釋:
灰度發布是指在黑與白之間,能夠平滑過渡的一種發布方式。AB test就是一種灰度發布方式,讓一部分用戶繼續用A,一部分用戶開始用B,如果用戶對B沒有什么反對意見,那么逐步擴大范圍,把所有用戶都遷移到B上面 來。灰度發布可以保證整體系統的穩定,在初始灰度的時候就可以發現、調整問題,以保證其影響度。
這里的用于WEB系統新代碼的測試發布,讓一部分(IP)用戶訪問新版本,一部分用戶仍然訪問正常版本,其原理如圖:
執行過程:
1、 當用戶請求到達前端代理服務Nginx,內嵌的lua模塊解析Nginx配置文件中的lua腳本代碼;
2、 Lua變量獲得客戶端IP地址,去查詢memcached緩存內是否有該鍵值,如果有返回值執行@client_test,否則執行@client。
3、 Location @client_test把請求轉發給部署了new版代碼的服務器,location @client把請求轉發給部署了normal版代碼的服務器,服務器返回結果。整個過程完成。
下面把安裝配置過程詳細說明。
二、安裝配置過程詳解
1、安裝nginx
安裝依賴包
yum-yinstallgccgcc-c++autoconflibjpeglibjpeg-devellibpnglibpng-develfreetypefreetype-devellibxml2libxml2-develzlibzlib-develglibcglibc-develglib2glib2-develbzip2bzip2-develncursesncurses-develcurlcurl-devele2fsprogse2fsprogs-develkrb5krb5-devellibidnlibidn-developensslopenssl-developenldapopenldap-develnss_ldapopenldap-clientsopenldap-serversmakepcre-develyum-yinstallgdgd2gd-develgd2-devellualua-develyum–yinstallmemcached
下載lua模塊、lua-memcache操作庫文件和nginx包
wgethttps://github.com/simpl/ngx_devel_kit/archive/v0.2.18.tar.gzwgethttps://github.com/chaoslawful/lua-nginx-module/archive/v0.8.5.tar.gzwgethttps://github.com/agentzh/lua-resty-memcached/archive/v0.11.tar.gzwgethttp://nginx.org/download/nginx-1.4.2.tar.gztarxvfnginx-1.4.2.tar.gz cdnginx-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/makemakeinstall
拷貝lua的memcached操作庫文件
tarxvfv0.11.tar.gzcp-rlua-resty-memcached-0.11/lib/resty//usr/lib64/lua/5.1/
配置nginx
#vim/soft/nginx/conf/nginx.conf worker_processes1; events{ worker_connections1024; } http{ includemime.types; default_typeapplication/octet-stream; sendfileon; keepalive_timeout65; proxy_next_upstreamerrortimeout; proxy_redirectoff; proxy_set_headerHost$host; proxy_set_headerX-Real-IP$http_x_forwarded_for; proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for; client_max_body_size100m; client_body_buffer_size256k; proxy_connect_timeout180; proxy_send_timeout180; proxy_read_timeout180; proxy_buffer_size8k; proxy_buffers864k; proxy_busy_buffers_size128k; proxy_temp_file_write_size128k; upstreamclient{ server192.168.200.29:80; } upstreamclient_test{ server192.168.200.29:81; } server{ listen80; server_namelocalhost; location/{ content_by_lua\' clientIP=ngx.req.get_headers()["X-Real-IP"]ifclientIP==nilthen clientIP=ngx.req.get_headers()["x_forwarded_for"] endifclientIP==nilthen clientIP=ngx.var.remote_addr end localmemcached=require"resty.memcached" localmemc,err=memcached:new()ifnotmemcthen ngx.say("failedtoinstantiatememc:",err) return end localok,err=memc:connect("127.0.0.1",11211)ifnotokthen ngx.say("failedtoconnect:",err) return end localres,flags,err=memc:get(clientIP)iferrthen ngx.say("failedtogetclientIP",err) return endifres=="1"then ngx.exec("@client_test") return end ngx.exec("@client") \'; } location@client{ proxy_passhttp://client; } location@client_test{ proxy_passhttp://client_test; } location/hello{ default_type\'text/plain\'; content_by_lua\'ngx.say("hello,lua")\'; } location=/50x.html{ roothtml; } } }
檢測配置文件。
#/soft/nginx/sbin/nginx-t nginx:theconfigurationfile/soft/nginx/conf/nginx.confsyntaxisok nginx:configurationfile/soft/nginx/conf/nginx.conftestissuccessful
啟動nginx
/soft/nginx/sbin/nginx
啟動memcached服務
memcached-unobody-m1024-c2048-p11211–d
三、測試驗證
測試lua模塊是否運行正常
訪問http://測試服務器ip地址/hello。如果顯示:hello,lua 表示安裝成功。
在另一臺測試機(這里是192.168.200.29)設置兩個虛擬主機,一個用80端口是執行正常代碼,一個是81端口執行灰度測試代碼。
在memcached中以你的客戶機IP地址為key,value值為1。這里我的IP是192.168.68.211.
telnetlocalhost11211Trying::1... Connectedtolocalhost. Escapecharacteris\'^]\'. set192.168.68.2110011STORED get192.168.68.211VALUE192.168.68.211911END quit
注意:
set后第一個值為key值。
192.168.68.211這是key值是需要灰度測試的IP地址;
0 表示一個跟該key有關的自定義數據;
3600 表示該key值的有效時間;
1 表示key所對應的value值的字節數。
下面訪問Nginx,效果符合預期,我的IP已經在memcached中存儲值,所以請求轉發給執行灰度測試代碼的主機。
從memcached刪除我的主機IP值。
再次請求Nginx,請求轉發給執行正常代碼內容的主機。
整個配置并不復雜,整個判斷過程對服務的影響非常小。如果需要使用這個系統最好自己看看lua腳本。
分享標題:利用nginx+lua+memcache實現灰度發布
分享URL:http://www.yijiale78.com/article0/cppsio.html
成都網站建設公司_創新互聯,為您提供網站內鏈、用戶體驗、商城網站、網站維護、面包屑導航、品牌網站建設
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯