Load Balancer - HAProxy
์๋น์ค ๊ตฌ์ฑ๋

์๋ฒ ์ ๋ณด
VM1
192.168.10.116
Linux (CentOS 7)
Load Balancer
VM2
192.168.10.121
WindowsServer2016
์น ์๋น์ค (IIS)
VM3
192.168.10.122
WindowsServer2016
์น ์๋น์ค (IIS)
VM4
192.168.10.123
WindowsServer2016
์น ์๋น์ค (IIS)
VM5
192.168.10.125
WindowsServer2016
์น ์๋น์ค (IIS)
HAProxy ์ค์
์ค์น
sudo yum -y install haproxy
์ค์น๊ฐ ์๋ฃ๋๋ฉด ๊ธฐ๋ณธ์ ์ธ ์ค์ ํ์ผ์ ๋ค์ ๊ฒฝ๋ก์ ์์
vi /etc/haproxy/haproxy.cfg
ํ์ผ ๋ด์ฉ ์์
๋ฐฑ์๋ ์ค์
backend web_main
balance roundrobin
server web1 192.168.10.121:80 check
server web2 192.168.10.122:80 check
backend web_abc
balance roundrobin
server web1 192.168.10.123:80 check
backend web_xyz
balance roundrobin
server web1 192.168.10.125:80 check
HTTP ์ค์
frontend http_front
bind *:80
default_backend web_main
WEB UI ์ค์
listen stats
bind *:9000
mode http
stats enable
stats hide-version
stats realm Haproxy Statistics
stats uri /
HAProxy ์๋น์ค
์์
systemctl start haproxy
๋ถํ ํ ์๋ ์คํ
systemctl enable haproxy
์ํ ํ์ธ
systemctl status haproxy
์คํ ์ค๋ฅ ์ ํ์ธ ๋ฐฉ๋ฒ: ๋ฃจํธ๋ก ํฌ๊ทธ๋ผ์ด๋์์ ์๋์ผ๋ก ์์ํ๊ณ ์ค๋ฅ ๋ฉ์์ง๋ฅผ ํ์ธ
haproxy -f /etc/haproxy/haproxy.cfg -db
์๋น์ค๊ฐ ์์๋์ง ์๊ณ ์๋ ์ค๋ฅ ๋ฉ์์ง์ ๊ฐ์ด ๋ฐ ๊ฒฝ์ฐ
์ค๋ฅ๋ฉ์์ง: "Starting frontend mssql_frontend: cannot bind socket [0.0.0.0:1433]"
setsebool -P haproxy_connect_any=1
setsebool -P haproxy_connect_any=1
๋ช
๋ น์ด์ ์ญํ
setsebool -P haproxy_connect_any=1
๋ช
๋ น์ด์ ์ญํ setsebool: SELinux์ boolean ๊ฐ์ ์ค์ ํ๋ ๋ช ๋ น์ด
-P: ๋ณ๊ฒฝ ์ฌํญ์ ์๊ตฌ์ ์ผ๋ก ์ ์ฉํ์ฌ ์์คํ ์ฌ๋ถํ ํ์๋ ์ ์ง
haproxy_connect_any=1: HAProxy๊ฐ ๋ชจ๋ ๋คํธ์ํฌ ์๋น์ค์ ์ฐ๊ฒฐํ ์ ์๋๋ก ํ์ฉํ๋ boolean ๊ฐ์ ํ์ฑํ
๋ฐฉํ๋ฒฝ ์ค์ (ํฌํธ ๋ฐ ์๋น์ค ๋ฑ๋ก)
์๋ก์ด zone ์์ฑ
firewall-cmd --permanent --new-zone=webserver
๋ฐฉํ๋ฒฝ์ ์๋น์ค ์ถ๊ฐ
firewall-cmd --permanent --zone=webserver --add-service=http
firewall-cmd --permanent --zone=webserver --add-service=https
๋ฐฉํ๋ฒฝ์ ํฌํธ ์ถ๊ฐ
firewall-cmd --permanent --zone=webserver --add-port=80/tcp
firewall-cmd --permanent --zone=webserver --add-port=443/tcp
firewall-cmd --permanent --zone=webserver --add-port=9000/tcp
firewalld ์ฌ์์ ๋ฐ ์๋ก ๋ฑ๋กํ zone ํ์ฑํ
firewall-cmd --reload
firewall-cmd --set-default-zone=webserver
์ ์ ์ค์ ์ฌ๋ถ ํ์ธ
firewall-cmd --list-services --zone=webserver
firewall-cmd --list-ports --zone=webserver
์์ ์ค์ธ ํฌํธ ํ์ธ
๋ฐฉํ๋ฒฝ์์ ํฌํธ๋ฅผ ์ค์ ํด๋ ํด๋น ํฌํธ๋ก ์ค์ ์๋น์ค๊ฐ ์์ ๋๊ณ ์์ด์ผ ๋ํ๋จ
๋ฐฉํ๋ฒฝ ์คํ์ ๋์์ผ๋ ํ๋ก์ธ์ค๊ฐ ์ ๋ ์๋ ๊ฒ (ํฌํธ๋ฅผ ์ด๊ณ ๋๊ธฐํ๊ณ ์์ง ์์ ์ํ)
netstat -tulpn | grep LISTEN
grep -w '80/tcp' /etc/services
certbot SSL ์ธ์ฆ์ ๋ฐ๊ธ
letsencrypt์ผ๋ก ๋ฐ๊ธํ๊ธฐ ์ํด์๋ certbot ํ๋ก์ธ์๋ก ๋ฐ๊ธํ ์ ์์ผ๋ฉฐ CentOS7์์๋ epel-release๋ฅผ ์ค์นํด์ผ ๋ค์ด๋ก๋๊ฐ ๊ฐ๋ฅ
sudo yum -y install epel-release
sudo yum -y install certbot
๋ฐ๊ธํ๋ ๋ฐฉ๋ฒ
sudo certbot certonly --standalone -d <domain>
Multi-Domain
sudo certbot certonly --standalone -d <domain1> -d <domain2> -d <domain3>
์ฃผ์ํ ์ ์ 80๋ฒ ํฌํธ๋ก ์ ์ํ์ฌ ssl์ ๋ฐ๊ธํ๊ฒ ๋๋๋ฐ haproxy๋ฅผ 80๋ฒ ํฌํธ๋ฅผ ์ฌ์ฉํ๊ณ ์๋ค๋ฉด ์๋น์ค๋ฅผ ์ค๋จํ๊ณ ์งํ
์ค์น๊ฐ ์๋ฃ๋๋ฉด ๋ค์ ๊ฒฝ๋ก์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ค์น
/etc/letsencrypt/live/<domain>/
ํ์ผ์ ๋ถ๋ฌ์ค๋๊ฒ์ ๊ฐ์ํ ํ๊ธฐ ์ํด์ ๋ค์ ๋ช
๋ น์ด๋ก ํ๋๋ก ํฉ์น๋ค
mkdir /etc/haproxy/certs
cat /etc/letsencrypt/live/<domain>/*.pem > /etc/haproxy/certs/ssl.pem
์ธ์ฆ์ ์๋ ๊ฐฑ์ ์ค์
Bash ์ ์คํฌ๋ฆฝํธ ํ์ผ ์์ฑ
vi /bin/letsencrypt.sh
#!/bin/sh
systemctl stop haproxy
/usr/bin/certbot renew > /var/log/letsencrypt/renew.log
cat /etc/letsencrypt/live/<domain>/*.pem > /etc/haproxy/certs/ssl.pem
fuser -k 80/tcp
systemctl start haproxy
์คํฌ๋ฆฝํธ ํ์ผ์ ๊ถํ ๋ถ์ฌ
chmod +x /bin/letsencrypt.sh
์คํฌ๋ฆฝํธ ํ์ผ ์คํํ๊ณ ์์ฑ๋ ๋ก๊ทธ ํ์ผ ๋ง์ง๋ง ์์ ์ผ์ ํ์ธ
/bin/letsencrypt.sh
ls -l /var/log/letsencrypt
crontab์ ์ด์ฉํ์ฌ ์คํฌ๋ฆฝํธ ํ์ผ ์๋ ์คํ ์ค์
sudo crontab -e
๋งค์ผ ์ค์ 3์ ๋ง๋ค ์คํ
0 3 * * * /bin/letsencrypt.sh
crontab ์๋น์ค ์์
service crond start
HTTPS ๋ฐ ๋๋ฉ์ธ๋ณ ์ ์ํ ๋ฐฑ์๋ ์ค์
vi /etc/haproxy/haproxy.cfg
๊ธฐ์กด ์ ์ํ 80 ํฌํธ ์ค์ ์ ๋ค์๊ณผ ๊ฐ์ด ๋ณ๊ฒฝ
HTTP ์์ฒญ์ ๋ฐ์๋ค์ด๋ ํ๋ก ํธ์๋๋ก, ํด๋ผ์ด์ธํธ๊ฐ HTTPS๊ฐ ์๋ ์์ฒญ์ ๋ณด๋ผ ๊ฒฝ์ฐ์๋ redirect ์ง์์ด๋ฅผ ์ฌ์ฉํ์ฌ HTTPS๋ก ๋ฆฌ๋๋ ์
frontend http-in
bind *:80
redirect scheme https if !{ ssl_fc }
HTTPS ์ค์
forntend https
bind *:433 ssl crt /etc/haproxy/certs/ssl.pem
acl host_abc hdr_end(host) -i abc.gsti.co.kr
acl host_xyz hdr_end(host) -i xyz.gsti.co.kr
mode http
use_backend web_abc if host_abc
use_backend web_xyz if host_xyz
default_backend web_main
์ฌ๋ฌ๊ฐ ์ธ์ฆ์ ํ์ผ(.pem) ์ธํ
๋ฐฉ๋ฒ <์ฐธ๊ณ >
frontend ft_test
mode http
bind 0.0.0.0:443 ssl crt /certs/haproxy1.pem crt /certs/haproxy2.pem
use_backend bk_cert1 if { ssl_fc_sni my.example.com } # content switching based on SNI
use_backend bk_cert2 if { ssl_fc_sni my.example.org } # content switching based on SNI
โป ๋ฆฌ๋
์ค ์คํ๋ ํฌํธ ํ์ธ ๋ฐ ๋ฑ๋ก
์คํ๋ HTTP Port ํ์ธ๋ฐฉ๋ฒ
semanage port -l | grep http_port_t
HTTP Port ์คํํ๊ธฐ ์ํด ์ถ๊ฐํด์ฃผ๋ ๋ฐฉ๋ฒ
semanage port -a -t http_port_t -p tcp 8081
HTTP Port ๋ซ๊ธฐ ์ํด ์ ๊ฑฐ
semanage port -d -t http_port_t -p tcp 8081
๊ธฐํ
CentOS ์ค์น ์ฐธ๊ณ : https://copycoding.tistory.com/269
Last updated