使用 Nginx 實現灰度釋出
灰度釋出是指在黑與白之間,能夠平滑過渡的一種釋出方式。AB test就是一種灰度釋出方式,讓一部分使用者繼續用A,一部分使用者開始用B,如果使用者對B沒有什麼反對意見,那麼逐步擴大範圍,把所有使用者都遷移到B上面來。
灰度釋出可以保證整體系統的穩定,在初始灰度的時候就可以發現、調整問題,以保證其影響度。
灰度釋出常見一般有三種方式:
- Nginx+LUA方式
- 根據Cookie實現灰度釋出
- 根據來路IP實現灰度釋出
本文主要將講解根據Cookie和來路IP這兩種方式實現簡單的灰度釋出,Nginx+LUA
這種方式涉及內容太多就不再本文展開了。
A/B測試流程
Nginx根據Cookie實現灰度釋出
根據Cookie查詢Cookie鍵為version的值,如果該Cookie值為V1則轉發到hilinux_01,為V2則轉發到hilinux_02。Cookie值都不匹配的情況下預設走hilinux_01所對應的伺服器。
兩臺伺服器分別定義為:
1:hilinux_01 192.168.1.100:8080 2:hilinux_02 192.168.1.200:8080- 用if指令實
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
upstream hilinux_01 {
server192.168.1.100:8080max_fails=1fail_timeout=60;
}
upstream hilinux_02 {
server 192.168.1.200:8080 max_fails=1 fail_timeout=60;
}
upstream default {
server 192.168.1.100:8080 max_fails=1 fail_timeout=60;
}
server {
listen 80;
server_name www.hi-linux.com;
access_log logs/www.hi-linux.com.log main; #match cookie
set $group "default";
if ($http_cookie ~* "version=V1"){ set $group hilinux_01; }
if($http_cookie ~* "version=V2"){ set $group hilinux_02; }
location / {
proxy_pass http://$group;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
index index.html index.htm;
}
}
- 用map指令實現
在Nginx裡面配置一個對映,$COOKIE_version
可以解析出Cookie裡面的version欄位。$group
是一個變數,{}裡面是對映規則。
如果一個version為V1的使用者來訪問,$group
就等於hilinux_01。在server裡面使用就會代理到http://hilinux_01
上。version為V2的使用者來訪問,$group
就等於hilinux_02。在server裡面使用就會代理到http://hilinux_02
上。Cookie值都不匹配的情況下預設走hilinux_01所對應的伺服器。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
upstream hilinux_01 {
server 192.168.1.100:8080 max_fails=1 fail_timeout=60;
}
upstream hilinux_02 {
server 192.168.1.200:8080 max_fails=1 fail_timeout=60;
}
upstream default {
server 192.168.1.100:8080 max_fails=1 fail_timeout=60;
}
map $COOKIE_version $group {
~*V1$ hilinux_01; ~*V2$ hilinux_02; default default;
}
server {
listen 80;
server_name www.hi-linux.com;
access_log logs/www.hi-linux.com.log main;
location / {
proxy_pass http://$group;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
index index.html index.htm;
}
}
Nginx根據來路IP實現灰度釋出
如果是內部IP,則反向代理到hilinux_02(預釋出環境);如果不是則反向代理到hilinux_01(生產環境)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
upstream hilinux_01 {
server 192.168.1.100:8080 max_fails=1 fail_timeout=60;
}
upstream hilinux_02 {
server 192.168.1.200:8080 max_fails=1 fail_timeout=60;
}
upstream default {
server 192.168.1.100:8080 max_fails=1 fail_timeout=60;
}
server {
listen 80;
server_name www.hi-linux.com;
access_log logs/www.hi-linux.com.log main;
set $group default;
if ($remote_addr ~ "211.118.119.11") { set $group hilinux_02; }
location / {
proxy_pass http://$group;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
index index.html index.htm;
}
}
如果你只有單臺伺服器,可以根據不同的IP設定不同的網站根目錄來達到相同的目的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
server {
listen 80;
server_name www.hi-linux.com;
access_log logs/www.hi-linux.com.log main;
set $rootdir "/var/www/html";
if ($remote_addr ~ "211.118.119.11") { set $rootdir "/var/www/test"; }
location / { root $rootdir; }
}
到此最基本的實現灰度釋出方法就講解完了,如果要做更細粒度灰度釋出可參考ABTestingGateway
專案。
轉自:https://www.dnsdizhi.com/409.html
ABTestingGateway是新浪開源的一個動態路由系統。ABTestingGateway是一個可以動態設定分流策略的灰度釋出系統,工作在7層,基於nginx和ngx-lua開發,使用redis作為分流策略資料庫,可以實現動態排程功能。
ABTestingGateway:https://github.com/CNSRE/ABTestingGateway
https://www.hi-linux.com/posts/34319.html