Nginx Blue/Green Rolling 무중단 배포 실습 (초간단)

2025. 4. 16. 13:04·🔹 DevOps
728x90
SMALL
  • Rocky Linux 8.10 기반 실습용 가상 서버 대상
  • 3대 서버 (LB / APP-BLUE / APP-GREEN) 활용
  • Nginx upstream의 weight 설정을 통한 단계적 트래픽 전환 실습

1. 서버 구성

역할IP 주소설명

lb-server 192.168.56.101 Nginx 로드밸런서
app-blue 192.168.56.102 기존 서비스 버전 (v1)
app-green 192.168.56.104 신규 서비스 버전 (v2)
Client Server 192.168.56.103 curl request 날리는 서버

2. 모든 서버: Nginx 설치

sudo dnf install -y nginx
sudo systemctl enable --now nginx

3. APP 서버 HTML 페이지 설정

▶ app-blue (192.168.56.102)

echo "<h1>BLUE</h1>" | sudo tee /usr/share/nginx/html/index.html

▶ app-green (192.168.56.104)

echo "<h1>GREEN</h1>" | sudo tee /usr/share/nginx/html/index.html

4. lb-server Nginx 구성 파일 수정

▶ /etc/nginx/nginx.conf 내부에 http {} 블록을 아래처럼 수정

-------------------------------------

http {
    upstream sungbin {
        server 192.168.56.102 weight=10;  # 초기: BLUE 100%
        server 192.168.56.104 down;   # 초기: GREEN 0%
    }

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

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    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
    # for more information.
    include /etc/nginx/conf.d/*.conf;
-------------------------------------------

▶ 프록시 설정을 /etc/nginx/conf.d/proxy.conf 파일로 분리

# /etc/nginx/conf.d/proxy.conf

server {
    listen 80 default_server;
    server_name _;                 # make default

    location / {
        proxy_pass http://sungbin; # request to upstream 'sungbin' block server
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

▶ Nginx 설정 적용

sudo nginx -t && sudo systemctl restart nginx
  • 화면에 진짜 나오는지 체크한다

5. 트래픽 분산 테스트

  • client 서버에서, Load Balancer 서버로 통신을 시켜보자
for i in {1..20}; do curl -s http://192.168.56.101 | grep -oE 'BLUE|GREEN'; done
  • 결과는 20대 0 이다


6. weight 자동 조정 스크립트

▶ /usr/local/bin/switch-weight.sh

#!/bin/bash

BLUE_WEIGHT=$1
GREEN_WEIGHT=$2
NGINX_CONF="/etc/nginx/nginx.conf"

if [ -z "$BLUE_WEIGHT" ] || [ -z "$GREEN_WEIGHT" ]; then
  echo " Usage: $0 <blue_weight> <green_weight>"
  echo "  ex) $0 8 2"
  exit 1
fi

# 백업
cp -p "$NGINX_CONF" "$NGINX_CONF.bak.$(date +%s)"

# BLUE (192.168.56.102)
if [ "$BLUE_WEIGHT" = "0" ]; then
  sed -i 's/server 192.168.56.102.*/        server 192.168.56.102 down;/' "$NGINX_CONF"
else
  sed -i "s/server 192.168.56.102.*/        server 192.168.56.102 weight=$BLUE_WEIGHT;/" "$NGINX_CONF"
fi

# GREEN (192.168.56.104)
if [ "$GREEN_WEIGHT" = "0" ]; then
  sed -i 's/server 192.168.56.104.*/        server 192.168.56.104 down;/' "$NGINX_CONF"
else
  sed -i "s/server 192.168.56.104.*/        server 192.168.56.104 weight=$GREEN_WEIGHT;/" "$NGINX_CONF"
fi

nginx -t && nginx -s reload && echo "✅ Updated: BLUE=$BLUE_WEIGHT, GREEN=$GREEN_WEIGHT"

 

  • 실행 권한 추가
sudo chmod +x /usr/local/bin/switch-weight.sh
  • Test1) 현재 버전 9, 신규 버전 1로 설정한다
[root@master nginx]# switch-weight.sh 9 1
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
✅ Updated: BLUE=9, GREEN=1
  • client 에서 curl 명령어로, 9 대 1 이 적용되었나 확인

 

  • Test2) 현재 버전 5, 신규 버전 5로 설정한다
switch-weight.sh 5 5
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
✅ Updated: BLUE=5, GREEN=5

 

  • Test3) 현재 버전 0, 신규 버전 10 으로 설정한다
[root@master nginx]# switch-weight.sh 0 10
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
✅ Updated: BLUE=0, GREEN=10


✅ 실습 마무리

  • switch-weight.sh를 통해 rolling 전환이 간편하게 가능

사실 이는 기초이며,
추가적인 테스트를 한 뒤 kubernetes - deployment (blue/green) 티스토리 작성하겠다

LIST

'🔹 DevOps' 카테고리의 다른 글

AWS와 Terraform의 관계 vs Kubernetes와 Helm의 관계  (1) 2025.03.06
OpenSearch 또는 ECK 기반의 로깅 시스템 구축 및 고도화  (1) 2025.02.17
GitOps 작동 방식 (간단한 흐름)  (1) 2025.02.14
EKS / GitHub Actions & ArgoCD를 활용 CI/CD 배포 파이프라인 구축 및 운영 (전체 과정)  (1) 2025.02.14
AWS 3 Tier architecture / terraform 정리 (미완)  (0) 2025.02.06
'🔹 DevOps' 카테고리의 다른 글
  • AWS와 Terraform의 관계 vs Kubernetes와 Helm의 관계
  • OpenSearch 또는 ECK 기반의 로깅 시스템 구축 및 고도화
  • GitOps 작동 방식 (간단한 흐름)
  • EKS / GitHub Actions & ArgoCD를 활용 CI/CD 배포 파이프라인 구축 및 운영 (전체 과정)
terranbin
terranbin
Studying Computer Science
  • terranbin
    Engineer
    terranbin
  • 전체
    오늘
    어제
    • 분류 전체보기 (129)
      • ☁️Cloud (42)
        • AWS (38)
        • MS Azure (4)
      • 🐳 Infra (1)
        • System (12)
        • DRBD (3)
      • 🔌Network (8)
      • 🔹Storage (8)
      • 🔹Kubernetes (15)
      • 🔹 DevOps (8)
      • 🔹Study (4)
      • 🔹Install (6)
      • 🔹ETC (2)
      • 🔹PostMan (6)
      • 🔹Openstack (3)
      • 🔹RcloneView (6)
      • 🔹Test (0)
      • 🔹Debug (2)
      • 🔹DBMS (2)
  • 블로그 메뉴

    • 홈
  • 링크

    • sungbin
    • Github
  • 공지사항

  • 인기 글

  • 태그

    rocky8
    SAA
    network
    aws dlt
    centos7
    EBS
    EC2
    AWS
    S3
    rocky9
    postman
    kubernetes
    distributed load testing
    rcloneview
    ceph
    terraform
    kubectl
    OpenStack
    Google Drive
    설치
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
terranbin
Nginx Blue/Green Rolling 무중단 배포 실습 (초간단)
상단으로

티스토리툴바