본문 바로가기

인프라/Docker

서버 이중화 테스트 환경 구축

로컬에서 docker를 통해 테스트 환경 구축

 

docker-compose.yaml

version: '3.3'

services:
  loadbalancer:
    image: sihyun2/proxy_server
    ports:
      - 80:80
      - 81:81
    working_dir: /etc/haproxy
    entrypoint: ["/bin/bash","-c"]
    command:
      -  haproxy -f haproxy.cfg && tail -f /dev/null
    networks:
      staticip:
        ipv4_address: 172.19.0.2 
  
  webserver1:
    image: sihyun2/php-server
    volumes:
      - /c/Project/docker/admin:/var/www/html/admin
      - /c/Project/docker/home:/var/www/html/home
    entrypoint: ["/bin/bash","-c"]
    command:
      - chmod 777 -R /var/www/html/admin && chmod 777 -R /var/www/html/home && /usr/sbin/httpd -k restart && /usr/sbin/php-fpm && tail -f /dev/null
    networks:
      staticip:
        ipv4_address: 172.19.0.3 
  
  webserver2:
    image: sihyun2/php-server
    volumes:
      - /c/Project/docker/admin:/var/www/html/admin
      - /c/Project/docker/home2:/var/www/html/home
    entrypoint: ["/bin/bash","-c"]
    command:
      - chmod 777 -R /var/www/html/admin && chmod 777 -R /var/www/html/home && /usr/sbin/httpd -k restart && /usr/sbin/php-fpm && tail -f /dev/null
    networks:
      staticip:
        ipv4_address: 172.19.0.5

networks:
  staticip:
    ipam:
      config:
        - subnet: 172.19.0.0/16
          gateway: 172.19.0.1

 

로드 밸런싱은 haproxy를 통해 실행 됩니다.

haproxy.cfg

#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   https://www.haproxy.org/download/1.8/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

    # utilize system-wide crypto-policies
    ssl-default-bind-ciphers PROFILE=SYSTEM
    ssl-default-server-ciphers PROFILE=SYSTEM

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
    bind *:80
    default_backend             app

frontend admin
    bind *:81
    default_backend             admin
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend admin
    balance     roundrobin
    cookie SERVER insert indirect nocache

    server      app1 172.19.0.3:81 check cookie app1
    server      app2 172.19.0.5:81 check cookie app2

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
    balance     roundrobin
    cookie SERVER insert indirect nocache
    server  app1 172.19.0.3:80 check cookie app1
    server  app2 172.19.0.5:80 check cookie app2

.쿠키 값을 통해 sticky session이 구현이 됩니다.

 

vhost를 통해 내부에서도 서버가 두개 돌도록하였습니다.

<VirtualHost *:80>
       ServerName localhost:80
       ServerAlias localhost:80
       DocumentRoot "/var/www/html/home/public"
       <Directory "/var/www/html/home/public">
         Options FollowSymLinks
	 AllowOverride All
	 Require all granted
       </Directory>
</VirtualHost>

<VirtualHost *:81>
       ServerName localhost:81
       ServerAlias localhost:81
       DocumentRoot "/var/www/html/admin/public"
       <Directory "/var/www/html/admin/public">
         Options FollowSymLinks
         AllowOverride All
         Require all granted
       </Directory>
</VirtualHost>

결과