HAProxy 설치 및 Custom Health check를 적용한 Reverse Proxy Server을 구현
chr=/var/lib/haproxy
sudo mkdir -p $chr/{bin,lib,lib64}
list="$(ldd /bin/bash | egrep -o '/lib.*\.[0-9]')"
sudo chmod 755 /var/lib/haproxy/lib # 카피 권한을 주기 위해 사용
sudo chmod 755 /var/lib/haproxy/lib64
for i in $list; do cp -v --parents "$i" "${chr}"; done
cp -v /bin/bash $chr
#!/bin/bash
status=$(curl -s http://www.api.com/status)
if [[ "$status" == "master" ]]; then
exit 0
else
exit 1
fi
sudo chmod +x /var/lib/haproxy/bin/check_server_status.sh # 스크립트 실행 가능하게 함
sudo chown haproxy:haproxy /var/lib/haproxy/bin/check_server_status.sh # 파일 소유자를 HAProxy 사용자로 변경
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
external-check
insecure-fork-wanted
insecure-setuid-wanted
defaults
mode tcp
timeout connect 4s
timeout client 15s
timeout server 15s
#timeout tunnel 365d
frontend web_frontend
bind *:5000
default_backend master_backend # 5000번 포트로 트래픽이 들어오면 이름이 master_backend인 backend를 실행
acl is_master_alive nbsrv(main_backend) gt 0 # master_backend 의 code가 0보다 클때 is_master_alive는 True
use_backend slave_backend if !is_master_alive # is_master_alive가 False이면 slave_backend를 실행
backend master_backend
option external-check
external-check path "/var/lib/haproxy/bin: /usr/bin:/bin" # 경로 지정
external-check command /var/lib/haproxy/bin/check_server1_status.sh # 스크립트 파일 실행
server master_server 127.0.0.1:7000 check inter 1s
backend slave_backend
option external-check
external-check path "/var/lib/haproxy/bin: /usr/bin:/bin"
external-check command /var/lib/haproxy/bin/check_server2_status.sh
server slave_server 127.0.0.1:7001 check inter 1s
```
listen stats
bind 0.0.0.0:80
mode http
balance
timeout client 5000
timeout connect 4000
timeout server 30000
stats uri /haproxy_stats # 127.0.0.1:80/haproxy_stats로 접속 시 status 확인 가능
stats realm HAProxy\ Statistics
stats auth username:password # 웹 콘솔에 접근하는 계정/암호 설정
stats admin if TRUE # 위 auth를 통해 admin쿠키를 설정하여 admin 쿠키를 TRUE로 설정한 client에 대해서만 접근이 가능하도록 함.
```