블루-그린 배포를 위해 nginx 시뮬레이션을 사용하는 방법

WBOY
풀어 주다: 2023-06-03 12:31:03
앞으로
854명이 탐색했습니다.

블루-그린 배포

블루-그린 배포의 핵심은 다음과 같습니다

  • 1. 블루 버전과 그린 버전이 동시에 존재합니다

  • 2. 파란색 또는 녹색 중 하나만 가능하며 스위치로 제어됩니다.

장점과 단점 분석: 장점은 속도와 롤백에 있습니다. 그리고 단점도 분명합니다. 두 가지 환경 세트가 동시에 존재하기 때문에 빠른 롤백이 가능하며, 두 가지 환경 세트가 있기 때문에 복잡성과 소요 리소스가 증가하게 됩니다.
또한, 속도가 향상되었지만 구현 과정에서 스위치 제어는 스위칭 속도가 아무리 빠르더라도 다른 기술을 결합하지 않으면 여전히 완전히 원활한 스위칭을 달성할 수 없습니다.

청록색 배포 시뮬레이션

다음으로 nginx의 업스트림을 사용하여 청록색 배포 시나리오를 간단히 시뮬레이션합니다. 구체적인 시나리오는 다음과 같습니다. nginx 설정을 조정하면 파란색 버전이 현재 활성화된 버전으로 설정됩니다.

블루-그린 배포를 위해 nginx 시뮬레이션을 사용하는 방법

미리 준비하세요

시연의 편의를 위해 7001/7002 두 포트에서 두 서비스를 미리 시작하여 서로 다른 정보를 표시하도록 tornado를 사용하여 이미지를 만들고 도커 실행 시 전달된 매개변수를 전달했습니다. 컨테이너가 시작되었습니다. Different는 서비스 간의 차이점을 표시하는 데 사용됩니다.

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"
로그인 후 복사

실행 로그

[root@kong ~]# docker run -d -p 7001: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: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:7001
hello, service :hello blue/green service: v1 in 7001
[root@kong ~]# curl http://192.168.163.117:7002
hello, service :hello blue/green service: v2 in 7002
[root@kong ~]#
로그인 후 복사

Start nginx

[root@kong ~]# docker run -p 9080:80 --name nginx-blue-green -d nginx
d3b7098c44890c15918dc47616b67e5e0eb0da7a443eac266dbf26d55049216a
[root@kong ~]# docker ps |grep nginx-blue-green
d3b7098c4489    nginx           "nginx -g 'daemon ..."  10 seconds ago    up 9 seconds    0.0.0.0:9080->80/tcp   nginx-blue-green
[root@kong ~]#
로그인 후 복사

nginx 코드 스니펫

다음 nginx 코드 스니펫을 준비하여 nginx의 /etc/nginx/conf.d/default.conf에 추가하고 시뮬레이션 방법은 매우 간단합니다. down을 사용하여 트래픽이 0임을 나타냅니다(nginx에서는 가중치를 0으로 설정할 수 없음). 처음에는 트래픽의 100%가 블루 버전으로 전송됩니다.

http {
upstream 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;
  location / {
    proxy_pass http://nginx_blug_green;
  }
}
로그인 후 복사

default.conf 수정 방법

컨테이너에 vim을 설치하면 효과를 얻을 수 있고, 로컬에서 수정한 다음 docker cp를 통해 전달하거나 sed로 직접 수정할 수도 있습니다. 컨테이너에 vim을 설치하는 경우 다음 방법을 사용하세요

[root@kong ~]# docker exec -it nginx-lb sh
# apt-get update
...省略
# apt-get install vim
...省略
로그인 후 복사

수정 전

# cat default.conf
server {
  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.conf
upstream 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  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 reload
2018/05/28 04:39:47 [notice] 321#321: signal process started
#
로그인 후 복사

결과 확인

10번 호출 후 모두 출력 All 7001의 v1입니다

[root@kong ~]# cnt=0; while [ $cnt -lt 10 ]
> do
> let cnt++
> 안녕하세요, 서비스: /그린 서비스: 7001의 v1
hello, 서비스 :hello 블루/그린 서비스: 7001의 v1
hello, 서비스 :hello 블루/그린 서비스: 7001의 v1
hello, 서비스 :hello 블루/그린 서비스: 7001의 v1
hello, 서비스 :hello 블루/그린 서비스: 7001의 v1
hello, 서비스 :hello 블루/그린 서비스: 7001의 v1
hello, 서비스 :hello 블루/그린 서비스: 7001
hello, 서비스 :hello 블루/그린 서비스: v1 in 7001
hello, service :hello blue/green 서비스: v1 in 7001
hello, service :hello blue/green 서비스: v1 in 7001
[root@kong ~]#

블루-그린 배포: 그린 버전으로 전환

default.conf의 가중치를 조정한 다음 nginx -s reload를 실행하면 nginx 서비스를 중지하지 않고도 그린 버전으로 동적으로 전환할 수 있습니다. 대상은 7002

에서 모든 트래픽을 출력합니다.

default.conf 수정 방법

다음과 같이 업스트림에서 서버의 가중치를 조정하세요.

upstream nginx_blug_green {
  server 192.168.163.117:7001 down;
  server 192.168.163.117:7002 weight=100;
}
로그인 후 복사

nginx 설정 다시 로드

# nginx -s reload
2018/05/28 05:01:28 [notice] 330#330: signal process started
#
로그인 후 복사
결과 확인

[root @kong ~]# cnt= 0; while [ $cnt -lt 10 ]; do 컬 ; let cnt++; service :hello blue/green 서비스: 7002의 v2
hello, service :hello blue/green 서비스: 7002의 v2

hello, 서비스 :hello 블루/그린 서비스: 7002의 v2
hello, 서비스 :hello 블루/그린 서비스: 7002의 v2
hello, service :hello 블루/그린 서비스: 7002의 v2
hello, 서비스 : hello 블루/그린 서비스: v2 in 7002
hello, 서비스 :hello 블루/그린 서비스: 7002
hello, 서비스 :hello 블루/그린 서비스: 7002
hello, 서비스 :hello 블루/그린 서비스: 7002
hello, 서비스 :hello 블루/그린 서비스: v2 in 7002
[root@kong ~]#

위 내용은 블루-그린 배포를 위해 nginx 시뮬레이션을 사용하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:yisu.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!