웹 시스템을 구축 후 운영하다보니 코드 수정 부분을 자주 운영이관해야 했다.

그러다보니 배포시 무중단에 대한 니즈가 발생했고, 일주일간 꼬박 삽질했다...

이 글을 보는 사람들은 시간을 단축하길 바라며 자료를 남긴다.

 

일단 결론이 된 코드부터 공유하겠다.

1. nginx 설정

nginx가 배포할 대상 서버에 설치되어 있지 않다면 설치해주시길 바란다.

blue 배포시 nginx-blue.conf --> nginx.conf 로 덮어쓰고 nginx 서비스를 재기동하고,

green 배포시 nginx-green.conf --> nginx.conf로 덮어쓰고 nginx서비스를 재기동한다.

서버에 미리 nginx-blue.conf, nginx-green.conf 파일을 만들거나 github actions에서 /etc/nginx/ 경로에 2가지 파일을 배포한다.

/etc/nginx.conf 파일의 모습

아래에서 server localhost:8081 부분만 server localhost:8080로만 스위칭된다고 생각하면 된다.

# /etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    upstream serverurl {
        server localhost:8081; # green blue인 경우 8080
    }

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    sendfile            on;
    tcp_nopush          on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;


        location / {
            include /etc/nginx/uwsgi_params;
            proxy_pass http://serverurl;
        }
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }
}

 

2. 블루 그린 배포에서 nginx의 역할

위의 경우 80포트로 들어오는 요청을 blue인 경우 localhost:8081로 보내주고, green인 경우 localhost:8080으로 보내준다.

한 마디로 여기서는 무중단배포를 위한 연결다리 역할로만 사용한다.

 

다음 편에서는 github actions의 workflow 설정을 다뤄보겠다.(아래 링크 참조)

2023.08.07 - [웹개발] - [docker] fastapi + nginx 블루 그린 배포(무중단배포)[2]

 

반응형

+ Recent posts