Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Use nginx to simulate the way you deploy blue and green

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

This article describes the blue-green deployment and how to simulate it most easily using nginx

Blue and green deployment

The focus of blue and green deployment is on the following characteristics

1. Both the blue and green versions exist at the same time. The actual operating environment is blue or green, which can only be one of them, which can be controlled by switch.

Analysis of advantages and disadvantages: the advantage lies in its speed and rollback. And the shortcomings are obvious. You can roll back quickly because there are two sets of environments at the same time, so the complexity and resource requirements will increase, because it has two sets of environments.

In addition, although the speed has been improved, but in the process of implementation, the switch control, no matter how fast the switching speed, if not combined with other technologies, can not achieve complete seamless switching.

Simulate a blue-green deployment

Next, we use nginx's upstream to briefly simulate the blue-green deployment scenario. The specific scenarios are as follows. The blue version is currently active. By adjusting the nginx setting, the green version is set to the current active version.

Prepare beforehand

In advance, start two services on the two ports of 7001 docker 7002 to display different information. For convenience of demonstration, use tornado to make a mirror image. Different parameters passed when starting through the docker container are used to display different services.

Docker run-d-p 7001 in 8080 liumiaocn/tornado:latest python / usr/local/bin/daemon.py "Hello blue/green service: v1 in 7001" docker run-d-p 7002 purl 8080 liumiaocn/tornado:latest python / usr/local/bin/daemon.py "Hello blue/green service: v2 in 7002"

Execution log

[root@kong ~] # docker run-d-p 7001docker run 8080 liumiaocn/tornado:latest python / usr/local/bin/daemon.py "Hello blue/green service: v1 in 7001" 70c74dc8e43d5635983f7240deb63a3fc0599d5474454c3bc5197aa5c0017348 [root@kong ~] # docker run-d-p 7002 purl 8080 liumiaocn/tornado:latest python / usr/local/bin/daemon.py "Hello blue/green service: v2 in 7002" 6c5c2ea322d4ac17b90feefb96e3194ec8adecedaa4c944419316a2e4bf07117 [root@kong ~] # curl http://192.168.163.117:7001Hello, Service: Hello blue/green service: v1 in 7001 [root@kong ~] # curl http://192.168.163.117:7002Hello, Service: Hello blue/green service: v2 in 7002 [root@kong ~] #

Start nginx

[root@kong ~] # docker run-p 9080 nginxd3b7098c44890c15918dc47616b67e5e0eb0da7a443eac266dbf26d55049216a 80-- name nginx-blue-green-d nginxd3b7098c44890c15918dc47616b67e5e0eb0da7a443eac266dbf26d55049216a [root@kong ~] # docker ps | grep nginx-blue-greend3b7098c4489 nginx "nginx- g 'daemon..." 10 seconds ago Up 9 seconds 0.0.0.0 seconds 9080-> 80/tcp nginx-blue-green [root@kong ~] #

Nginx code snippet

Prepare the following nginx code snippet to add it to the / etc/nginx/conf.d/default.conf of nginx. The simulation method is very simple. Zero traffic is indicated by down (weight cannot be set to zero in nginx). At the beginning, 100% of the traffic is sent to the blue version.

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

The method of modifying default.conf

You can achieve this by installing vim in the container, or you can modify it locally and pass it in via docker cp, or you can modify it directly by sed. If you install vim in the container, you can use the following

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

Before modification

# 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 proxy the PHP scripts to Apache listening on 80 # # location ~. Php$ {# proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1 php$ {# root html; # fastcgi_pass 127.0.0.1 # 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; #} #

After modification

# cat default.confupstream nginx_blug_green {server 192.168.163.117 down; 7001 weight=100; server 192.168.163.117 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_blug_green;} # error_page 404 / 404.html; # redirect server error pages to the static page / 50x.html # error_page 500502 503504 / 50x.html; location = / 50x.html {root / usr/share/nginx/html;} # proxy the PHP scripts to Apache listening on 127.0.1 proxy the PHP scripts to Apache listening on 80 # # location ~. Php$ {# proxy_pass #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1 pass the PHP scripts to FastCGI server listening on 9000 # # location ~\. Php$ {# root html; # fastcgi_pass 127.0.0.1 php$ 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; #} #

Reload nginx settin

# nginx-s reload2018/05/28 04:39:47 [notice] 321: signal process started#

Confirm the result

All of the 10 calls output v1 in 7001.

[root@kong ~] # cnt=0; while [$cnt-lt 10]

> do

Curl http://localhost:9080

> let cnt++

> done

Hello, Service: Hello blue/green service: v1 in 7001

Hello, Service: Hello blue/green service: v1 in 7001

Hello, Service: Hello blue/green service: v1 in 7001

Hello, Service: Hello blue/green service: v1 in 7001

Hello, Service: Hello blue/green service: v1 in 7001

Hello, Service: Hello blue/green service: v1 in 7001

Hello, Service: Hello blue/green service: v1 in 7001

Hello, Service: Hello blue/green service: v1 in 7001

Hello, Service: Hello blue/green service: v1 in 7001

Hello, Service: Hello blue/green service: v1 in 7001

[root@kong ~] #

Blue and green deployment: switch to green version

By adjusting the weight of default.conf, and then executing nginx-s reload, you can dynamically switch to the green version without stopping the nginx service, and the target will output all traffic to v2 in 7002.

The method of modifying default.conf

You only need to adjust the weight of the server in upstream as follows:

Upstream nginx_blug_green {server 192.168.163.117:7001 down; server 192.168.163.117:7002 weight=100;}

Reload nginx settin

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

Confirm the result

[root@kong ~] # cnt=0; while [$cnt-lt 10]; do curl http://localhost:9080; let cnt++; done

Hello, Service: Hello blue/green service: v2 in 7002

Hello, Service: Hello blue/green service: v2 in 7002

Hello, Service: Hello blue/green service: v2 in 7002

Hello, Service: Hello blue/green service: v2 in 7002

Hello, Service: Hello blue/green service: v2 in 7002

Hello, Service: Hello blue/green service: v2 in 7002

Hello, Service: Hello blue/green service: v2 in 7002

Hello, Service: Hello blue/green service: v2 in 7002

Hello, Service: Hello blue/green service: v2 in 7002

Hello, Service: Hello blue/green service: v2 in 7002

[root@kong ~] #

Summary

The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support. If you want to know more about it, please see the relevant links below.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report