首页 > 网站 > Nginx > 正文

使用nginx模拟进行金丝雀发布的方式

2024-08-30 12:30:08
字体:
来源:转载
供稿:网友

这篇文章介绍一下蓝绿部署以及使用nginx如何最简单地模拟一下金丝雀发布的方式

金丝雀发布/灰度发布

金丝雀发布的重点在于:试错。金丝雀发布的来历本身就是自然界的美丽生物在人类工业发展过程中的一个悲惨的故事。金丝雀就是用它的生命来为矿工的安全来试错的。用很小的成本来换取整体的安全,在持续部署的实践中,金丝雀就是流量控制,用很少的流量比如百分之一或者十分之一用于检证某个版本是否正常,如果不正常则就用最低的成本实现了其作用,降低了风险。如果正常,则可以逐渐加大权重直至百分之百,将所有的流量都平稳地切换至新的版本。灰度发布,一般来说也是类似的概念。灰色是介于黑和白之前的一个过渡,区别于蓝绿部署的非蓝即绿,灰度发布/金丝雀发布会有一个两者同时存在的时间段,只是两者对应的流量不同,金丝雀发布如果说和灰度发布有所不同的话,其不同点应该是目的性的不同,金丝雀发布目的在于试错,而灰度发布在于平稳发布,而在金丝雀发布没有问题的状况下进行的平稳过渡则正是灰度发布。

模拟金丝雀发布

接下来我们使用nginx的upstream来简单模拟一下金丝雀发布的场景。具体场景如下, 当前活跃的是主版本,通过调整nginx设定,通过不断的调节金丝雀版本的权重,最终实现平稳地发布。

nginx,模拟,金丝雀,发布

事前准备

事前在7001/7002两个端口分别启动两个服务,用于显示不同信息,为了演示方便,使用tornado做了一个镜像,通过docker容器启动时传递的参数不同用于显示服务的不同。

docker run -d -p 7001:8080 liumiaocn/tornado:latest python /usr/local/bin/daemon.py "Hello main service: v1 in 7001"docker run -d -p 7002:8080 liumiaocn/tornado:latest python /usr/local/bin/daemon.py "Hello canary deploy service: v2 in 7002"

执行日志

[root@kong ~]# docker run -d -p 7001:8080 liumiaocn/tornado:latest python /usr/local/bin/daemon.py "Hello main service: v1 in 7001"28f42bbd21146c520b05ff2226514e62445b4cdd5d82f372b3791fdd47cd602a[root@kong ~]# docker run -d -p 7002:8080 liumiaocn/tornado:latest python /usr/local/bin/daemon.py "Hello canary deploy service: v2 in 7002"b86c4b83048d782fadc3edbacc19b73af20dc87f5f4cf37cf348d17c45f0215d[root@kong ~]# curl http://192.168.163.117:7001Hello, Service :Hello main service: v1 in 7001[root@kong ~]# curl http://192.168.163.117:7002Hello, Service :Hello canary deploy service: v2 in 7002[root@kong ~]#

启动nginx

[root@kong ~]# docker run -p 9080:80 --name nginx-canary -d nginx659f15c4d006df6fcd1fab1efe39e25a85c31f3cab1cda67838ddd282669195c[root@kong ~]# docker ps |grep nginx-canary659f15c4d006    nginx           "nginx -g 'daemon ..."  7 seconds ago    Up 7 seconds    0.0.0.0:9080->80/tcp   nginx-canary[root@kong ~]#

nginx代码段

准备如下nginx代码段将其添加到nginx的/etc/nginx/conf.d/default.conf中, 模拟方式很简单,通过down来表示流量为零(nginx中无法将weight设置为零),开始的时候100%的流量都发到主版本。

http {upstream nginx_canary {  server 192.168.163.117:7001 weight=100;  server 192.168.163.117:7002 down;}server {  listen    80;  server_name www.liumiao.cn 192.168.163.117;  location / {    proxy_pass http://nginx_canary;  }}

修改default.conf的方法

可以通过在容器中安装vim达到效果,也可以在本地修改然后通过docker cp传入,或者直接sed修改都可。如果在容器中安装vim,使用如下方式即可

[root@kong ~]# docker exec -it nginx-lb sh# apt-get update...省略# apt-get install vim...省略

修改前

# cat default.confserver {  listen    80;  server_name localhost;  #charset koi8-r;  #access_log /var/log/nginx/host.access.log main;  location / {    root  /usr/share/nginx/html;    index index.html index.htm;  }  #error_page 404       /404.html;  # redirect server error pages to the static page /50x.html  #  error_page  500 502 503 504 /50x.html;  location = /50x.html {    root  /usr/share/nginx/html;  }  # proxy the PHP scripts to Apache listening on 127.0.0.1:80  #  #location ~ /.php$ {  #  proxy_pass  http://127.0.0.1;  #}  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  #  #location ~ /.php$ {  #  root      html;  #  fastcgi_pass  127.0.0.1:9000;  #  fastcgi_index index.php;  #  fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;  #  include    fastcgi_params;  #}  # deny access to .htaccess files, if Apache's document root  # concurs with nginx's one  #  #location ~ //.ht {  #  deny all;  #}}#

修改后

# cat default.confupstream nginx_canary {  server 192.168.163.117:7001 weight=100;  server 192.168.163.117:7002 down;}server {  listen    80;  server_name www.liumiao.cn 192.168.163.117;  #charset koi8-r;  #access_log /var/log/nginx/host.access.log main;  location / {    #root  /usr/share/nginx/html;    #index index.html index.htm;    proxy_pass http://nginx_canary;  }  #error_page 404       /404.html;  # redirect server error pages to the static page /50x.html  #  error_page  500 502 503 504 /50x.html;  location = /50x.html {    root  /usr/share/nginx/html;  }  # proxy the PHP scripts to Apache listening on 127.0.0.1:80  #  #location ~ /.php$ {  #  proxy_pass  http://127.0.0.1;  #}  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  #  #location ~ /.php$ {  #  root      html;  #  fastcgi_pass  127.0.0.1:9000;  #  fastcgi_index index.php;  #  fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;  #  include    fastcgi_params;  #}  # deny access to .htaccess files, if Apache's document root  # concurs with nginx's one  #  #location ~ //.ht {  #  deny all;  #}}#

重新加载nginx设定

# nginx -s reload2018/05/28 05:16:20 [notice] 319#319: signal process started#

确认结果

10次调用全部输出的都是v1 in 7001

[root@kong ~]# cnt=0; while [ $cnt -lt 10 ]; do curl http://localhost:9080; let cnt++; done
Hello, Service :Hello main service: v1 in 7001
Hello, Service :Hello main service: v1 in 7001
Hello, Service :Hello main service: v1 in 7001
Hello, Service :Hello main service: v1 in 7001
Hello, Service :Hello main service: v1 in 7001
Hello, Service :Hello main service: v1 in 7001
Hello, Service :Hello main service: v1 in 7001
Hello, Service :Hello main service: v1 in 7001
Hello, Service :Hello main service: v1 in 7001
Hello, Service :Hello main service: v1 in 7001
[root@kong ~]#

金丝雀发布: 金丝雀版本流量权重10%

通过调整default.conf的weight,然后执行nginx -s reload的方式,调节金丝雀版本的权重为10%,流量的10%会执行新的服务

修改default.conf的方法

只需要将upstream中的server的权重做如下调整:

upstream nginx_canary {  server 192.168.163.117:7001 weight=10;  server 192.168.163.117:7002 weight=90;}

重新加载nginx设定

# nginx -s reload2018/05/28 05:20:14 [notice] 330#330: signal process started#

确认结果

[root@kong ~]# cnt=0; while [ $cnt -lt 10 ]; do curl http://localhost:9080; let cnt++; done
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello main service: v1 in 7001
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello canary deploy service: v2 in 7002
[root@kong ~]#

金丝雀发布: 金丝雀版本流量权重50%

通过调整default.conf的weight,然后执行nginx -s reload的方式,调节金丝雀版本的权重为50%,流量的50%会执行新的服务

修改default.conf的方法

只需要将upstream中的server的权重做如下调整:

upstream nginx_canary {  server 192.168.163.117:7001 weight=50;  server 192.168.163.117:7002 weight=50;}

重新加载nginx设定

# nginx -s reload2018/05/28 05:22:26 [notice] 339#339: signal process started#

确认结果

[root@kong ~]# cnt=0; while [ $cnt -lt 10 ]; do curl http://localhost:9080; let cnt++; done
Hello, Service :Hello main service: v1 in 7001
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello main service: v1 in 7001
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello main service: v1 in 7001
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello main service: v1 in 7001
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello main service: v1 in 7001
Hello, Service :Hello canary deploy service: v2 in 7002
[root@kong ~]#

金丝雀发布: 金丝雀版本流量权重90%

通过调整default.conf的weight,然后执行nginx -s reload的方式,调节金丝雀版本的权重为90%,流量的90%会执行新的服务

修改default.conf的方法

只需要将upstream中的server的权重做如下调整:

upstream nginx_canary {  server 192.168.163.117:7001 weight=10;  server 192.168.163.117:7002 weight=90;}

重新加载nginx设定

# nginx -s reload2018/05/28 05:24:29 [notice] 346#346: signal process started#

确认结果

[root@kong ~]# cnt=0; while [ $cnt -lt 10 ]; do curl http://localhost:9080; let cnt++; done
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello main service: v1 in 7001
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello canary deploy service: v2 in 7002
[root@kong ~]#

金丝雀发布: 金丝雀版本流量权重100%

通过调整default.conf的weight,然后执行nginx -s reload的方式,调节金丝雀版本的权重为100%,流量的100%会执行新的服务

修改default.conf的方法

只需要将upstream中的server的权重做如下调整:

upstream nginx_canary {  server 192.168.163.117:7001 down;  server 192.168.163.117:7002 weight=100;}

重新加载nginx设定

# nginx -s reload2018/05/28 05:26:37 [notice] 353#353: signal process started

确认结果

[root@kong ~]# cnt=0; while [ $cnt -lt 10 ]; do curl http://localhost:9080; let cnt++; done
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello canary deploy service: v2 in 7002
Hello, Service :Hello canary deploy service: v2 in 7002
[root@kong ~]#

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对VEVB武林网的支持。


注:相关教程知识阅读请移步到服务器教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表