使用 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 {    
    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; #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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章