Overview
HAProxy を使用してロードバランサーを構築します。
インストール
haproxy をインストールします。
myadmin@ubuntu:~$ sudo DEBIAN_FRONTEND=noninteractive apt-get -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confold install haproxy
/etc/haproxy/haproxy.cfg
/etc/haproxy/haproxy.cfg を確認します。
デフォルト値
デフォルト値は下記の通りです。
root@ubuntu:~# grep -v -e '^\s*#' -e '^\s*$' /etc/haproxy/haproxy.cfg | expand | tr -s [:space:] | sed 's/^\s/ /g'
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
ca-base /etc/ssl/certs
crt-base /etc/ssl/private
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 http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http
設定値
設定例は下記の通りです。この設定は OpenShift 用の設定例ですが、設定値の意味は一般的なロードバランサーそのままとなりますので、他の用途でも流用できます。尚、backend server の指定に FQDN を指定していますが、この場合、HAProxy は名前解決ができないと起動しません。
myadmin@ubuntu:~$ sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.orig myadmin@ubuntu:~$ sudo tee /etc/haproxy/haproxy.cfg <<"EOF" 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 ca-base /etc/ssl/certs crt-base /etc/ssl/private 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 http option httplog option dontlognull timeout connect 5000 timeout client 50000 timeout server 50000 errorfile 400 /etc/haproxy/errors/400.http errorfile 403 /etc/haproxy/errors/403.http errorfile 408 /etc/haproxy/errors/408.http errorfile 500 /etc/haproxy/errors/500.http errorfile 502 /etc/haproxy/errors/502.http errorfile 503 /etc/haproxy/errors/503.http errorfile 504 /etc/haproxy/errors/504.http # ingress-http frontend ingress-http bind *:80 option tcplog mode tcp default_backend ingress-http-pool backend ingress-http-pool mode tcp balance roundrobin server node0 node0.test.si1230.com:80 check server node1 node1.test.si1230.com:80 check server node2 node2.test.si1230.com:80 check server bs bs.test.si1230.com:80 check # ingress-https frontend ingress-https bind *:443 option tcplog mode tcp default_backend ingress-https-pool backend ingress-https-pool mode tcp balance roundrobin option ssl-hello-chk server node0 node0.test.si1230.com:443 check server node1 node1.test.si1230.com:443 check server node2 node2.test.si1230.com:443 check server bs bs.test.si1230.com:443 check # k8s-api frontend k8s-api bind *:6443 option tcplog mode tcp default_backend k8s-api-pool backend k8s-api-pool mode tcp balance roundrobin option ssl-hello-chk server node0 node0.test.si1230.com:6443 check server node1 node1.test.si1230.com:6443 check server node2 node2.test.si1230.com:6443 check server bs bs.test.si1230.com:6443 check # config frontend config bind *:22623 option tcplog mode tcp default_backend config-pool backend config-pool mode tcp balance roundrobin option ssl-hello-chk server node0 node0.test.si1230.com:22623 check server node1 node1.test.si1230.com:22623 check server node2 node2.test.si1230.com:22623 check server bs bs.test.si1230.com:22623 check EOF
設定の有効化
設定を有効化します。
myadmin@ubuntu:~$ sudo systemctl restart haproxy.service && systemctl status haproxy.service
Ubuntu 22.04 ロードバランサー構築 HAProxy