Security

[Docker] 컨테이너 기반의 로드밸런싱 구축

min8282 2025. 4. 15. 03:22

이번 실습에서는 Docker 컨테이너를 활용하여 3개의 웹 서버를 구축하고, Kali Linux에 Nginx를 설치해 로드 밸런싱을 구현하는 과정을 정리해 보겠습니다. 각 웹 서버는 서로 다른 index.html을 제공하고, 이를 로드 밸런서를 통해 순차적으로 접속할 수 있도록 설정합니다.


1. 목표

  • Docker로 웹 서버 컨테이너 3개 생성 (web01, web02, web03)
  • 각 웹 서버는 서로 다른 index.html 페이지를 가짐
  • Kali Linux에 Nginx 설치 후 로드 밸런서 역할 수행
  • Nginx의 nginx.conf를 수정하여 로드 밸런싱 구현
  • 볼륨 마운트를 이용하여 컨테이너에 HTML 파일 연결

2. 웹 서버 구성 방법 (호스트 디렉터리를 마운트)

* 웹 서버를 구성할 때 호스트 디렉터리를 마운트 하는 방법 말고 cp 명령어를 이용해서 웹 서버 index 페이지를 수정하거나, 저장소 볼륨을 이용해서 index 페이지를 수정하는 방법도 존재한다.

볼륨 마운트로 사용할 디렉터리를 각 생성

각 디렉터리에 index.html 생성

web02, web03 폴더에도 동일하게 index.html 생성


3. 컨테이너 실행

각 디렉터리를 볼륨으로 지정해서 web01~web03 3개의 웹 서버를 생성

생성된 컨테이너 확인


4. Kali Linux에 Nginx 설치 및 설정

로드밸런싱 구축

  1. 칼리리눅스(호스트)에 nginx 서버를 설치 (로드밸런싱 역할)
  2. web01, web02. web03을 컨테이너 실행
  3. 볼륨을 이용해서 index.html 을 생성
  4. nginx conf를 수정해서 -> 로드밸런싱 구축

1) 기존 웹 서버 서비스 중지

sudo service apache2 stop

 

2) Nginx 설치 및 실행

sudo apt update
sudo apt install nginx
sudo service nginx start
sudo systemctl status nginx.service

5. Nginx 설정 파일 수정 (로드 밸런서 수정)

기본 설정 파일을 열고, 아래와 같이 nginx.conf를 수정

sudo vim /etc/nginx/nginx.conf

기존 설정은 모두 주석 처리한 후, 위 내용을 추가합니다. (후에 오류가 발생할 수 있어서…)

nginx가 설치된 칼리리눅스에서 sudo vim /etc/nginx/nginx.conf을 열어서 아래와 같이 필요한 것 이외는 주석처리 후에 들여 쓰기
더보기

예시

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {
        upstream backend-lb {
                server 127.0.0.1:8001;
                server 127.0.0.1:8002;
                server 127.0.0.1:8003;
        }

        server {
                listen 80 default_server;
                listen [::]:80 default_server;

                location / {
                        proxy_pass http://backend-lb;
                }
        }
        ##
        # Basic Settings
        ##

        #sendfile on;
        #tcp_nopush on;
        #types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

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

        ##
        # SSL Settings
        ##

        #ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        #ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        #gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        #include /etc/nginx/conf.d/*.conf;
        #include /etc/nginx/sites-enabled/*;
}

#mail {
#       # See sample authentication script at:
#       # <http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript>
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

위와 같이 입력이 완료된 후에 아래와 같이 nginx 서버를 다시 시작한 후에 80포트로 접속하여 새로고침을 하시면 로드밸런싱 진행 (오류가 발생하면 conf 파일 내 작성 다시 확인해야 함)

sudo service nginx restart

설정 저장 후 Nginx 재시작


6. 결과 확인

웹 브라우저에서 Kali Linux의 80번 포트로 접속

새로고침할 때마다 Test Page, Test Page 02, Test Page 03이 순차적으로 출력되므로 로드 밸런싱이 잘 작동된다.