Keepalived[1] 提供 VRRP 实现,并允许您配置 Linux 机器使负载均衡,预防单点故障。HAProxy[2] 提供可靠、高性能的负载均衡,能与 Keepalived 完美配合。由于 lb1 和 lb2 上安装了 Keepalived 和 HAproxy,如果其中一个节点故障,虚拟 IP 地址(即浮动 IP 地址)将自动与另一个节点关联,使集群仍然可以正常运行,从而实现高可用。若有需要,也可以此为目的,添加更多安装 Keepalived 和 HAproxy 的节点。先运行以下命令安装 Keepalived 和 HAproxy。
apt install keepalived haproxy psmisc -y
HAproxy
在两台用于负载均衡的机器上运行以下命令以配置 Proxy(两台机器的 Proxy 配置相同):
vi /etc/haproxy/haproxy.cfg
以下是示例配置,供您参考:
global log /dev/log local0 warning chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon stats socket /var/lib/haproxy/stats defaults log global option httplog option dontlognull timeout connect 5000 timeout client 50000 timeout server 50000 frontend kube-apiserver bind *:6443 mode tcp option tcplog default_backend kube-apiserver backend kube-apiserver mode tcp option tcp-check balance roundrobin default-server inter 10s downinter 5s rise 2 fall 2 slowstart 60s maxconn 250 maxqueue 256 weight 100 server kube-apiserver-1 192.168.1.243:6443 check # Replace the IP address with your own. server kube-apiserver-2 192.168.1.244:6443 check # Replace the IP address with your own. server kube-apiserver-3 192.168.1.245:6443 check # Replace the IP address with your own.
保存文件并运行以下命令以重启 HAproxy。
systemctl restart haproxy
使 HAproxy 在开机后自动运行
systemctl enable haproxy
确保您在另一台机器 (master2) 上也配置了 HAproxy。
Keepalived
两台机器上必须都安装 Keepalived,但在配置上略有不同。
运行以下命令以配置 Keepalived
vi /etc/keepalived/keepalived.conf
以下是示例配置,供您参考(请注意 server 字段。请记住 6443 是 apiserver 端口):
global_defs{ notification_email{ } script_userroot enable_script_security router_idLVS_DEVEL vrrp_skip_check_adv_addr vrrp_garp_interval1 vrrp_gna_interval1 } vrrp_scriptchk_haproxy{ script"/usr/bin/killall -0 haproxy" weight-50 fall3 rise5 timeout2 } vrrp_instancehaproxy-vip{ stateMASTER priority100 interfaceeth0# Network card virtual_router_id60 advert_int1 authentication{ auth_typePASS auth_pass1111 } unicast_src_ip192.168.1.243# The IP address of this machine unicast_peer{ 192.168.1.244# The IP address of peer machines } virtual_ipaddress{ 192.168.1.210/24# The VIP address } track_script{ chk_haproxy# 这个对应上面的vrrp_script } }