Nginx application: Using Nginx for blue-green deployment

Source: Internet
Author: User
Tags docker ps docker cp docker run

This article mainly introduces the Nginx application: using Nginx for blue-green deployment, has a certain reference value, and now share to everyone, the need for friends can refer to

This article describes the blue-green deployment and how to use Nginx to most easily simulate the way blue-green deployment

Blue-Green Deployment

Blue-Green deployment focuses on the following features
1. Blue version and green version exist simultaneously
2. The actual operating environment is blue or green, only one of them, through the switch control

Pros and cons Analysis: The advantage lies in its speed and rollback. And the shortcomings are obvious. Can be quickly rolled back because there are two sets of environments at the same time, so the complexity and the need for resources will increase, because there are two sets of environment. In  addition, although the speed has improved, but in the process of implementation, switch control, no matter how fast switching speed, if not combined with other technologies, or can not be completely seamless switch.

Simulate a blue-green deployment

Next we use the nginx upstream to simply simulate a blue-green deployment scenario. The specific scenario is as follows, the current active is the blue version, by adjusting the Nginx settings, the green version is set to the current active version.

version Description
Router Users visit the MicroServices under this deployment via http://localhost:8090
Blue version Currently active for the blue version, service on port 7001, with the hint message "Hello blue/green service:v1 in 7001"
Green version Upcoming green version, service on port 7002 with the hint message "Hello blue/green service:v2 in 7002"

Prepare beforehand

Prior to the 7001/70,022 ports to start two services, to display different information, for demonstration convenience, using Tornado to make a mirror, through the Docker container launch when the parameters passed different for the display of different services.

Docker run-d-P 7001:8080 liumiaocn/tornado:latest python/usr/local/bin/daemon.py "Hello blue/green service:v1 in 7001" Docker run-d-P 7002: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 7001:8080 liumiaocn/tornado:latest python/usr/local/bin/daemon.py "Hello blue/green Servi Ce:v1 in 7001 "70c74dc8e43d5635983f7240deb63a3fc0599d5474454c3bc5197aa5c0017348[root@kong ~]# Docker run-d-P 7002: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:80--name nginx-blue-green-d Nginxd3b7098c44890c15918dc47616b67e5e0eb0da7a443eac266dbf26d55049216a[root@kong ~]# Docker PS |grep nginx-blue-greend3b7098c4489        nginx                      "nginx-g ' daemon ..."   seconds ago up       9 seconds        0.0.0.0:9080->80/tcp     Nginx-blue-green[root@kong ~]#

Nginx Code Snippet

Prepare the following Nginx code snippet to add it to the Nginx/etc/nginx/conf.d/default.conf, the simulation is very simple, by down to indicate that the traffic is 0 (Nginx cannot set the weight to 0), at the beginning of 100% The traffic is sent to the blue version.

HTTP {upstream Nginx_blug_green {    server 192.168.163.117:7001 weight=100;    Server 192.168.163.117:7002 down;} server {    listen       ;    server_name  www.liumiao.cn 192.168.163.117;    Location/{        proxy_pass http://nginx_blug_green;    }}

Ways to modify Default.conf

The effect can be achieved by installing vim in the container, either locally and then via Docker CP, or directly sed modification. If you install Vim in a container, use the following method

[Root@kong ~]# Docker exec-it nginx-lb sh# apt-get update ... Omit # apt-get Install vim ... Omitted

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 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; #}}#

after modification

# cat Default.confupstream nginx_blug_green {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_blug_green;    } #error_page 404/404.html;    # REDIRECT Server error pages to the static page/50x.html # Error_page 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; #}}#

Reload Nginx Settings

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

Confirm Results

10 calls all outputs are v1 in 7001

[Root@kong ~]# cnt=0; While [$cnt-lt]> do> Curl http://localhost:9080> let cnt++> Donehello, Service:hello blue/green Service  : v1 in 7001Hello, Service:hello blue/green service:v1 in 7001Hello, Service:hello blue/green service:v1 in 7001Hello, Service:hello Blue/green service:v1 in 7001Hello, Service:hello blue/green service:v1 in 7001Hello, Service:hello bl Ue/green service:v1 in 7001Hello, Service:hello blue/green service:v1 in 7001Hello, Service:hello blue/green service: V1 in 7001Hello, Service:hello blue/green service:v1 in 7001Hello, Service:hello blue/green service:v1 in 7001[root@ko Ng ~]#

Blue-Green Deployment: Switch to the green version

By adjusting the weight of the default.conf and then performing nginx-s reload, the dynamic switch to the green version is done without stopping the Nginx service, and the target will output all the traffic v2 in 7002

Ways to modify Default.conf

You only need to make the following adjustments to the server's weights in upstream:

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

Reload Nginx Settings

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

Confirm Results

[root@kong ~]# cnt=0; while [$cnt-lt]; doing curl http://localhost:9080; let cnt+ +;  Donehello, Service:hello blue/green service:v2 in 7002Hello, Service:hello blue/green service:v2 in 7002Hello, Service  : Hello blue/green service:v2 in 7002Hello, Service:hello blue/green service:v2 in 7002Hello, Service:hello Blue/green Service:v2 in 7002Hello, Service:hello blue/green service:v2 in 7002Hello, Service:hello Blue/green service:v2 in 70 02Hello, Service:hello blue/green service:v2 in 7002Hello, Service:hello blue/green service:v2 in 7002Hello, Service: Hello blue/green service:v2 in 7002[root@kong ~]# 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.