>  기사  >  운영 및 유지보수  >  카나리아 게시에 nginx 시뮬레이션을 사용하는 방법

카나리아 게시에 nginx 시뮬레이션을 사용하는 방법

王林
王林앞으로
2023-05-15 11:25:05852검색

카나리아 릴리스/그레이스케일 릴리스

카나리아 릴리스의 핵심은 시행착오입니다. 카나리아방출의 기원 자체는 인간 산업의 발전 과정에서 자연의 아름다운 생명체들이 겪는 비극적인 이야기입니다. 카나리아는 광부의 안전을 위해 목숨을 걸고 실수를 저지릅니다. 전반적인 보안을 위해 약간의 비용이 사용됩니다. 지속적 배포를 실행하는 경우 카나리아는 1% 또는 10분의 1과 같은 아주 적은 양의 트래픽을 사용하여 특정 버전이 정상인지 확인합니다. 비정상적인 경우에는 최저 비용으로 기능을 달성하고 위험이 줄어듭니다. 정상이라면 100%에 도달할 때까지 점차적으로 가중치를 늘려가며 모든 트래픽을 새 버전으로 원활하게 전환할 수 있습니다. 그레이스케일 출판은 일반적으로 비슷한 개념입니다. 회색은 검정색과 흰색 사이의 전환입니다. 파란색 또는 녹색인 파란색 및 녹색 배포와는 다릅니다. 그레이스케일 릴리스/카나리아 릴리스는 두 가지가 동시에 존재하는 기간을 가지지만 둘의 해당 트래픽은 카나리아 릴리스가 그레이스케일 릴리스와 다르다면 차이점은 목적에 따라야 합니다. 카나리아 릴리스의 목적은 시행착오인 반면, 그레이스케일 릴리스는 안정적인 릴리스에 관한 것이며 카나리아 릴리스에서는 문제가 없습니다. 그레이 스케일 출판 상황에서.

카나리아 릴리스 시뮬레이션

다음으로 nginx의 업스트림을 사용하여 카나리아 릴리스 시나리오를 간단히 시뮬레이션합니다. 구체적인 시나리오는 다음과 같습니다. 현재 메인 버전이 활성화되어 있으며, nginx 설정을 조정하고 카나리아 버전의 가중치를 지속적으로 조정함으로써 최종적으로 원활한 출시가 이루어졌습니다.

카나리아 게시에 nginx 시뮬레이션을 사용하는 방법

미리 준비하세요

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

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:7001
hello, service :hello main service: v1 in 7001
[root@kong ~]# curl http://192.168.163.117:7002
hello, service :hello canary deploy service: v2 in 7002
[root@kong ~]#

Start nginx

[root@kong ~]# docker run -p 9080:80 --name nginx-canary -d nginx
659f15c4d006df6fcd1fab1efe39e25a85c31f3cab1cda67838ddd282669195c
[root@kong ~]# docker ps |grep nginx-canary
659f15c4d006    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을 사용하여 트래픽이 0임을 나타냅니다(nginx에서는 가중치를 0으로 설정할 수 없음). 처음에는 트래픽의 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.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_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 reload
2018/05/28 05:16:20 [notice] 319#319: signal process started
#

결과 확인

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

[root@kong ~]# cnt=0; while [ $cnt -lt 10 ]; let cnt++; hello 메인 서비스: 7001의 v1
안녕하세요, 서비스 :hello 메인 서비스: 7001의 v1
hello, service :hello 메인 서비스: 7001의 v1
hello, service :hello 메인 서비스: 7001의 v1
hello, service :hello 메인 서비스: 7001의 v1
hello, service :hello 메인 서비스: v1 in 7001
hello, service :hello 메인 서비스: v1 in 7001
hello, service :hello 메인 서비스: v1 in 7001
hello, service :hello 메인 서비스: v1 in 7001
hello, service :hello 메인 서비스 : v1 in 7001
[root@kong ~]#

Canary 릴리스: Canary 버전 트래픽 가중치 10%

default.conf의 가중치를 조정한 후 nginx -s reload를 실행하여 가중치를 조정합니다. Canary 버전을 10%로 설정하면 트래픽의 10%가 새 서비스를 실행합니다.

default.conf 수정 방법

다음과 같이 업스트림에서 서버의 가중치만 조정하면 됩니다.

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

nginx 다시 로드 settings

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

결과 확인

[root@kong ~]# cnt=0; while [ $cnt -lt 10 ]; let cnt++; 7002의 v2

hello, service :hello canary 배포 서비스: 7002의 v2
hello, service :hello canary 배포 서비스: 7002의 v2
hello, service :hello canary 배포 서비스: 7002의 v2
hello, 서비스 : hello 메인 서비스 : 7001의 v1
hello, service :hello canary 배포 서비스: 7002의 v2
hello, service :hello canary 배포 서비스: 7002의 v2
hello, service :hello canary 배포 서비스: 7002의 v2
hello, service :hello canary 배포 서비스: v2 in 7002
hello, service :hello canary 배포 서비스: v2 in 7002
[root@kong ~]#

Canary 릴리스: Canary 버전 트래픽 가중치는 50%

기본 가중치를 조정하여 .conf 그런 다음 nginx -s reload를 실행하고 카나리아 버전의 가중치를 50%로 조정하면 트래픽의 50%가 새 서비스를 실행합니다

default.conf를 수정하는 방법


가중치만 조정하면 됩니다. 다음과 같이 업스트림에 서버를 추가합니다:

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

Reload nginx settings

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

确认结果

[root@kong ~]# cnt=0; while [ $cnt -lt 10 ]; do curl ; 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 reload
2018/05/28 05:24:29 [notice] 346#346: signal process started
#

确认结果

[root@kong ~]# cnt=0; while [ $cnt -lt 10 ]; do curl ; 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 reload
2018/05/28 05:26:37 [notice] 353#353: signal process started

确认结果

[root@kong ~]# cnt=0; while [ $cnt -lt 10 ]; do curl ; 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 ~]#

위 내용은 카나리아 게시에 nginx 시뮬레이션을 사용하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 yisu.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제