우선 windows환경에서 Haproxy를 사용하기 위해서는 리눅스 환경이 필요하므로 WSL(Windows Subsystem for Linux)을 설치해주자.
WSL은 쉽게 말하면 windows에서 리눅스를 실행 할 수 있게 해주는 녀석이다!
WSL 설치
1. PowerShell을 관리자 권한으로 실행
2. wsl --install을 통해 wsl 설치
3. 이후 사용자 이름과 비밀번호 입력해주면 설치 완료
- 이후에는 PowerShell에서 Ubuntu를 입력하여 들어올 수 있다.
HAproxy 설치
sudo apt update - 패키지 업데이트
sudo apt upgrade - 패키지 업데이트
sudo apt install haproxy -- haproxy 설치
haproxy -v -- haproxy 버전확인
haproxy 설정 파일을 편집해 주자!
haproxy를 실행해주자
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /var/run/haproxy.sock mode 666 level admin
stats timeout 30s
user haproxy
group haproxy
daemon
# Default SSL material locations
ca-base /etc/ssl/certs
crt-base /etc/ssl/private
# See: https://ssl-config.mozilla.org/#server=haproxy&server-version=2.0.3&config=intermediate
ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384
ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets
defaults
log global
mode tcp
option tcplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
frontend lobby_frontend
bind *:9000 // 클라이언트의 요청을 받아들이는 곳
mode tcp // tcp 프로토콜을 사용함
// 요청을 처리할 기본 백엔드 서버를 지정
default_backend dynamic_lobby_backend
frontend game_frontend
bind *:1666
mode tcp
default_backend dynamic_game_backend
backend dynamic_lobby_backend
balance roundrobin // 로드 밸런서가 백엔드 서버에 요청을 분배하는 방식
// lobi-server-1은 6666을 사용한다. check는 서버가 동작하는지 헬스체크
server lobby_srv1 lobi-server-1:6666 check
server lobby_srv2 lobi-server-2:6667 check
backend dynamic_game_backend
balance roundrobin
server game_srv1 game-server-1:16666 check
server game_srv2 game-server-2:16667 check
server game_srv3 game-server-3:16668 check
이게 내가 사용한 환경설정파일이다!
주의할 점으로는
1. backend에 지정된 서버들이 실제로 열려있어야만 haproxy에서 check를 통해 로드 밸런싱을 해줄 수 있다는 것이다.
2. server lobby_srv1 lobi-server-1:6666 check 이렇게 서버를 할당해 줄 때
lobi-server-1은 실제로는 lobby_srv1의 외부 포트이다. 이렇게 호스트 이름으로 라우팅 해주려면 해당 이름을 로드 밸런서가 올바르게 해석할 수 있는 환경을 마련해주어야 한다.
(여기서는 docker-compose.yml에서 지정해줌)
나는 docker-compose를 통해 서버들을 열어줬다.
EX ) docker-compose.yml 파일 예시
game-server-1:
build:
context: ./GAME_SERVER
dockerfile: Dockerfile
container_name: game-server-1
ports:
- '16666:3000' # TCP 게임 서버 포트
environment:
- PORT=16666
- HOST=0.0.0.0
- CLIENT_VERSION=1.0.0
- DB1_HOST=mysql
- DB1_PORT=3306
- DB1_USER=root
- DB1_PASSWORD=orenodb
- REDIS_HOST=redis
- REDIS_PORT=6379
depends_on:
- mysql
- redis
game-server-2:
build:
context: ./GAME_SERVER
dockerfile: Dockerfile
container_name: game-server-2
ports:
- '16667:3000' # TCP 게임 서버 포트
environment:
- PORT=16667
- HOST=0.0.0.0
- CLIENT_VERSION=1.0.0
- DB1_HOST=mysql
- DB1_PORT=3306
- DB1_USER=root
- DB1_PASSWORD=orenodb
- REDIS_HOST=redis
- REDIS_PORT=6379
depends_on:
- mysql
- redis
도움이 될만한 명령어들