Nginx

推荐列表 站点导航

当前位置:首页 > 服务器技术 > Nginx >

使用Nginx实现灰度发布的使用

来源:网络整理  作者:fen  发布时间:2020-12-25 06:12
这篇文章主要介绍了使用Nginx实现灰度发布的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一...

灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式。AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B上面来。

灰度发布可以保证整体系统的稳定,在初始灰度的时候就可以发现、调整问题,以保证其影响度。

灰度发布常见一般有三种方式:

Nginx+LUA方式

根据Cookie实现灰度发布

根据来路IP实现灰度发布

本文主要将讲解根据Cookie和来路IP这两种方式实现简单的灰度发布,Nginx+LUA这种方式涉及内容太多就不再本文展开了。

A/B测试流程

使用Nginx实现灰度发布的使用

使用Nginx实现灰度发布的使用

Nginx根据Cookie实现灰度发布

根据Cookie查询Cookie键为version的值,如果该Cookie值为V1则转发到hilinux_01,为V2则转发到hilinux_02。Cookie值都不匹配的情况下默认走hilinux_01所对应的服务器。

两台服务器分别定义为:

hilinux_01  192.168.1.100:8080

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 ;

 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里面使用就会代理到上。version为V2的用户来访问,$group就等于hilinux_02。在server里面使用就会代理到上。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

 

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 ;

 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

 

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 ;

 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 ;

 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项目。

ABTestingGateway是新浪开源的一个动态路由系统。ABTestingGateway是一个可以动态设置分流策略的灰度发布系统,工作在7层,基于nginx和ngx-lua开发,使用redis作为分流策略数据库,可以实现动态调度功能。

ABTestingGateway:https://github.com/CNSRE/ABTestingGateway

参考文档


到此这篇关于使用Nginx实现灰度发布的使用的文章就介绍到这了,更多相关Nginx 灰度发布内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/weifeng1463/p/7353710.html

相关热词:

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://www.juheyunku.com/server/nginx/8805.shtml

最新文章
Nginx环境下WordPress的多站点 Nginx环境下WordPress的多站点

时间:2021-01-05

nginx keepalive的具体使用 nginx keepalive的具体使用

时间:2021-01-05

Nginx的信号控制 Nginx的信号控制

时间:2021-01-05

win10上安装nginx的方法步骤 win10上安装nginx的方法步骤

时间:2020-12-29

linux下 nginx监控问题 linux下 nginx监控问题

时间:2020-12-29

Nginx配置基于多域名、端口 Nginx配置基于多域名、端口

时间:2020-12-28

详解Nginx之Location配置(Lo 详解Nginx之Location配置(Lo

时间:2020-12-28

详解基于centos7搭建Nginx网 详解基于centos7搭建Nginx网

时间:2020-12-28

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

使用Nginx实现灰度发布的使用

2020-12-25 编辑:fen

灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式。AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B上面来。

灰度发布可以保证整体系统的稳定,在初始灰度的时候就可以发现、调整问题,以保证其影响度。

灰度发布常见一般有三种方式:

Nginx+LUA方式

根据Cookie实现灰度发布

根据来路IP实现灰度发布

本文主要将讲解根据Cookie和来路IP这两种方式实现简单的灰度发布,Nginx+LUA这种方式涉及内容太多就不再本文展开了。

A/B测试流程

使用Nginx实现灰度发布的使用

使用Nginx实现灰度发布的使用

Nginx根据Cookie实现灰度发布

根据Cookie查询Cookie键为version的值,如果该Cookie值为V1则转发到hilinux_01,为V2则转发到hilinux_02。Cookie值都不匹配的情况下默认走hilinux_01所对应的服务器。

两台服务器分别定义为:

hilinux_01  192.168.1.100:8080

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 ;

 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里面使用就会代理到上。version为V2的用户来访问,$group就等于hilinux_02。在server里面使用就会代理到上。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

 

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 ;

 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

 

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 ;

 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 ;

 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项目。

ABTestingGateway是新浪开源的一个动态路由系统。ABTestingGateway是一个可以动态设置分流策略的灰度发布系统,工作在7层,基于nginx和ngx-lua开发,使用redis作为分流策略数据库,可以实现动态调度功能。

ABTestingGateway:https://github.com/CNSRE/ABTestingGateway

参考文档


到此这篇关于使用Nginx实现灰度发布的使用的文章就介绍到这了,更多相关Nginx 灰度发布内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/weifeng1463/p/7353710.html

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://www.juheyunku.com/server/nginx/8805.shtml

相关文章

风云图片

推荐阅读

返回Nginx频道首页