2022. 7. 11. 21:56ใ๐ฏ OpenSource/HAproxy
Intro
HAproxy ์ค์ ๋ฐ ๋์ ๋ฐฉ์์ ์์๋ณธ๋ค.
HAProxy๋ TCP/HTTP ํธ๋ํฝ์ ์ํํธ์จ์ด์ ์ผ๋ก ๋ก๋๋ฐธ๋ฐ์ฑ ํ ์ ์๋ ์คํ์์ค์ด๋ค.
๋๋ ์คํ์คํ HA ๊ตฌ์กฐ๋ฅผ ๊ตฌ์ถํ๋ฉด์ vip ์ ๋ํ ์์ฒญ์ ์ฌ๋ฌ ๋ฐฑ์๋ ์๋ฒ๋ค๋ก ๋ถ์ฐํ๋๋ก ํ๊ธฐ ์ํด HAProxy๋ฅผ ์ฌ์ฉํ๋ค.
ํ๋ฒ์ ์คํ์คํ์ ๊ฐ ์ ์ ๊ฐ mariadb์ DB์ ์ ์์ ์ผ๋ก ์ ๊ทผํ์ง ๋ชปํ๋ค๋ connection aborted ์๋ฌ๊ฐ mysql.err์ ๋ฐ์ํ๋ค.
root@wglee-controller-001:/var/log/mysql# tail -f error.log
2022-07-05 22:09:34 30861 [Warning] Aborted connection 30861 to db: 'placement' user: 'placement' host: 'wglee-controller-001' (Got an error reading communication packets)
2022-07-05 22:10:07 30862 [Warning] Aborted connection 30862 to db: 'neutron' user: 'neutron' host: 'wglee-controller-001' (Got an error reading communication packets)
2022-07-05 22:11:09 30863 [Warning] Aborted connection 30863 to db: 'neutron' user: 'neutron' host: 'wglee-controller-001' (Got an error reading communication packets)
2022-07-05 22:12:10 30864 [Warning] Aborted connection 30864 to db: 'keystone' user: 'keystone' host: 'wglee-controller-001' (Got an error reading communication packets)
๋๋์ฒด ๋ญ๊ฐ ํ๋ฉด์ mariadb์ timout ์์น๋ง ์ด์ฌํ ํ๋ํ๋๋ฐ
๊ฒฐ๊ตญ haproxy์์ vip๋ก ๋ฐ์ ์์ฒญ์ backend server๋ก ๋๊ธธ ๋์ ํ์์์์ ๊ฑธ๋ฆฌ๋ ๊ฒ์ด ์ด์๋ก ๋ณด์๋ค.^^...
๊ทธ ํ๋ก HAProxy์ ์ค์ ํ์ผ ๊ตฌ์ฑ์ ์ต์ ์ ์ ์์์ผ๊ฒ ๋ค๋ ์๊ฐ์ด ๋ค์ด์ ํ๋ฒ ์ ๋ฆฌํด ๋ณธ๋ค.
HAProxy Configuration
Haproxy ์ค์ ํ์ผ์ ํฌ๊ฒ ๋ค์ ์น์ ๋ค๋ก ์ด๋ฃจ์ด์ ธ ์๋ค.
global
# global settings here
defaults
# defaults here
frontend
# a frontend that accepts requests from clients
backend
# servers that fulfill the requests
1. global
ํ๋ก์ธ์ค ์ ๋ฐ์ ์ผ๋ก ์ ์ฉ๋๋ ๋ณด์/์ฑ๋ฅ ํ๋ ์ค์
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
stats timeout 30s
user haproxy
group haproxy
daemon
maxconn : HAProxy๊ฐ ๋ฐ์๋ค์ผ ์ปค๋ฅ์
์ ์ต๋์น. ๋ก๋๋ฐธ๋ฐ์์ ๋ฉ๋ชจ๋ฆฌ ๋ถ์กฑ์ ๋ฐฉ์งํ๋ค.
log : ๋ก๊ทธ๋ฅผ ๋จ๊ธธ ๊ฒฝ๋ก๋ฅผ ์ง์ ํ๋ค. local0์ผ๋ก ์ค์ ํด์ syslog ๊ธฐ๋ฅ์ ์ด์ฉํ ์ ์๋ค.
user / group : ๋ฐ๋ก ์ง์ ํ์ง ์์ผ๋ฉด haproxy๋ root ๊ถํ์ผ๋ก ๋์ํ๊ธฐ ๋๋ฌธ์ ๋ฏธ๋ฆฌ ๊ณ์ ์ ์์ฑํ๊ณ ์ง์ ํ๋๋ก ํ๋ค.
2. defaults
์ค์ ํ์ผ์์ ์ค๋ณต์ ์ ๊ฑฐํ๊ธฐ ์ํด ์ฌ์ฉํ๋ค.
default ์ธํ
์ frontend์ backend ์น์
์ ์ ์ฉ๋๋ค. (frontend, backend ์์ ์ค๋ฒ๋ผ์ด๋๋ ๊ฐ๋ฅ)
defaults
timeout connect 10s
timeout client 30s
timeout server 30s
log global
mode http
option httplog
maxconn 3000
timeout [ํญ๋ชฉ] : ๊ฐ ํญ๋ชฉ์ ๋ํ timout ์ ์ค์ ํ๋ค. "s" ๋ฅผ ๋ช
์ํ๋ฉด ์ด๋จ์ ์ค์ ์ ํ๊ฒ ๋๋ฉฐ, ์๋ฌด๊ฒ๋ ๋ถ์ด์ง ์์ ๊ฒฝ์ฐ ๊ธฐ๋ณธ์ ์ผ๋ก milliseconds ๋ก ์ค์ ๋๋ค.
timeout connect : HAProxy๊ฐ ๋ฐฑ์๋ ์๋ฒ์ TCP ์ฐ๊ฒฐ์ด established ๋ ๋๊น์ง ๋๊ธฐํ๋ ์๊ฐ
timeout client : client ↔ haproxy frontend ์ฌ์ด์ ์ฐ๊ฒฐ์ ๋ํด ๋๊ธฐํ๋ ์๊ฐ์ด๋ค. ์ด ์ต์
์ TCP ์ฒดํฌ์ ํํด์ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
timeout server : haproxy backend์์ server ๋ก ์์ฒญ์ ๋ณด๋ด๊ณ ๋๊ธฐํ๋ ํ์์์ ์๊ฐ์ด๋ค.
mode : HAProxy๊ฐ ์ด๋ค protocol์ ๋ํด Proxyํ ์ง๋ฅผ ์ ์. TCP / HTTP
log global : frontend ์์ global ์น์
์
maxconn : ๊ฐ frontend์์ ์์ฉํ ์ปค๋ฅ์
3. frontend
client๊ฐ connectํ ip์ port ๋ฑ์ ์ค์ ํ๋ค.
frontend wglee-openstack-vip
bind 20.20.0.5:80 ssl crt /etc/wglee.pem
mode http
option httpclose
option forwardfor
option accept-invalid-http-request
reqadd X-Forwarded-Proto:\ https
default_backend object_storage
bind : ๋ฐ์ธ๋ฉํ์ฌ listenํ ์์ดํผ์ ํฌํธ๋ฅผ ์ง์ ํ๋ค.
ssl, crt ์ต์
: HAProxy๊ฐ SSL/TLS ์ฒ๋ฆฌ๋ฅผ ํ๋๋ก ํ๋ค.
use_backend, default_backend : ํด๋น frontend ๋ก ๋ค์ด์จ ์์ฒญ์ ์ฒ๋ฆฌํด๋ฌ๋ผ๊ณ ๋ณด๋ผ backend.
(์ฐ์ ์์ : use_backend -> (use_backend๊ฐ ์คํจํ๋ฉด) default_backend)
4. backend
์์ฒญ์ ์ฒ๋ฆฌํ backend ์๋ฒ์ ๊ทธ๋ฃน
ํ์ real server ์ ๋ณด์ ๊ฐ ์ต์
์ ๋ฃ๋๋ค.
backend web_servers
balance roundrobin
cookie SERVERUSED insert indirect nocache
option httpchk HEAD /
default-server check maxconn 20
server server1 10.0.1.3:80 cookie server1
server server2 10.0.1.4:80 cookie server2
balance : ๋ก๋๋ฐธ๋ฐ์ฑํ ์๊ณ ๋ฆฌ์ฆ์ ์ง์ ํ๋ค. (roundrobin, leastconn)
option httpchk : HAProxy๊ฐ ๋ฐฑ์๋ ์๋ฒ์ ๋ํด Layer 7 ํฌ์ค ์ฒดํฌ๋ฅผ ํ๋๋ก ํ๋ค. ์๋ต์ด ์๋ ์๋ฒ์๋ ์์ฒญ ํฌ์๋ฉํ์ง ์์.
default-server : ์ดํ์ ๋ฐ๋ผ์ค๋ server ๋ค์ ๊ณตํต์ผ๋ก ์ ์ฉ๋๋ ๋ํดํธ ์ธํ
-> inter : health check ์ interval์ ์๋ฏธํ๋ค.
-> rise : backend server๊ฐ ๋์ ์ค์ด๋ผ๊ณ ์ฌ๊ธฐ๊ธฐ ์ํด ์ฑ๊ณต์ ์ผ๋ก ์ํ๋์ด์ผ ํ๋ health check์ ํ์ ( ๊ธฐ๋ณธ์ 2 )
-> fall : backend server๊ฐ ์ฃฝ์๋ค๊ณ ์ฌ๊ธฐ๊ธฐ ์ํด ์คํจํด์ผ ํ๋ health check์ ํ์. ( ๊ธฐ๋ณธ์ 3 )
-> check : backend server์ ๋ํด health check๋ฅผ ํ์ฑํ ํ๋ค.
server : ๋ฒก์๋๋ก ์ฌ์ฉํ ์๋ฒ ๋ฑ๋ก
5. listen
listen์ ์ฌ์ฉํ๋ฉด frontend์ backend ์ ๊ธฐ๋ฅ์ ํ๋ฒ์ ์ฌ์ฉํ๋ค๊ณ ๋ณด๋ฉด ๋จ
listen mariadb
bind wglee-openstack-vip:3306
mode tcp
balance leastconn
timeout client 10800s
timeout server 10800s
default-server port 9200 inter 2s downinter 5s rise 3 fall 2 slowstart 60s
server wglee-controller-001 20.20.0.20:3306 check
server wglee-controller-002 20.20.0.21:3306 check backup
server wglee-controller-003 20.20.0.22:3306 check backup
์์์ mariadb ์ connection aborted๊ฐ ๋ฐ์ํ๋ค๊ณ ํ๋๋ฐ ์์ ๊ฐ์ด timout client, timeout server๋ฅผ ๋๋ ค์ฃผ๊ณ ๋์ ํด๊ฒฐ ๋์๋ค.
port : port ํ๋ผ๋ฏธํฐ๋ฅผ ์ฌ์ฉํ๋ฉด health check ์ฉ์ผ๋ก ๋ค๋ฅธ port๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
๋๋ก ์ดํ๋ฆฌ์ผ์ด์
์ด ๋์ํ๋ ํฌํธ๋ณด๋ค ๋ค๋ฅธ port ๋ฅผ ์ฌ์ฉํด์ health check๋ฅผ ํด์ผ ํ๋ ๊ฒฝ์ฐ๊ฐ ์๋ค. ์ด ๊ฒฝ์ฐ์ ์ฌ์ฉํ๋ค.
์๋ฅผ ๋ค์ด์ mariadb๋ 3306์ผ๋ก ๋์ํ์ง๋ง health check๋ฅผ 3306 ํฌํธ๋ก ํ๋ ๊ฒ์ ์ ํฉํ์ง ์๋ค. ( curl ํ์ ๋ ๋ฐํ๋๋ HTTP_CODE๊ฐ ์์ ) ๊ทธ๋์ ๋์์ผ๋ก clustercheck๊ฐ xinetd๋ก ๋์ํ๋ 9200 ๋ฅผ ์ฒดํฌํ๋ค.
root@wglee-controller-001:~# curl -o /dev/null -s -w %{http_code} 20.20.0.20:9200 ; echo
200
root@wglee-controller-001:~# curl -o /dev/null -s -w %{http_code} 20.20.0.20:3306 ; echo
000
๋ด haproxy ์ค์ ๊ธฐ์ค์ผ๋ก frontend / backend / listen ์ ์ ์ฉ๋ ์ต์
์ ๊ฐ๊ฐ ์ค๋ช
ํ๋ค ๋ณด๋๊น ๊ณตํต์ผ๋ก ์ฌ์ฉํ ์ ์๋ ์ต์
๋ ์ฌ๊ธฐ์ ๊ธฐ ์ ์ด๋ฒ๋ ธ๋ค.
์ํฉ์ ๋ง๊ฒ ํ์ธํด์ ์ ์ฌ์ฉํด์ผ๊ฒ ๋ค.