kubernetes部署haproxy、keepalived为kube-apiserver做集群
也可以用nginx、keepalived做负载均衡,看大家的需求。
# yum -y install haproxy keepalived
haproxy的配置文件(三台一样):
cat > /etc/haproxy/haproxy.cfg <<EOF global log /dev/log local0 log /dev/log local1 notice chroot /var/lib/haproxy stats socket /var/run/haproxy-admin.sock mode 660 level admin stats timeout 30s user haproxy group haproxy daemon nbproc 1 defaults log global timeout connect 5000 timeout client 10m timeout server 10m listen admin_stats bind 0.0.0.0:10080 mode http log 127.0.0.1 local0 err stats refresh 30s stats uri /status stats realm welcome login\ Haproxy stats auth admin:123456 stats hide-version stats admin if TRUE listen kube-master bind 0.0.0.0:8443 mode tcp option tcplog balance source server 192.168.111.10 192.168.111.10:6443 check inter 2000 fall 2 rise 2 weight 1 server 192.168.111.11 192.168.111.11:6443 check inter 2000 fall 2 rise 2 weight 1 server 192.168.111.12 192.168.111.12:6443 check inter 2000 fall 2 rise 2 weight 1 EOF
启动haproxy:
# for SERVICES in haproxy;do systemctl enable $SERVICES; systemctl restart $SERVICES; systemctl status $SERVICES; done
keepalived配置文件(1主):
cat > /etc/keepalived/keepalived.conf <<EOF global_defs { router_id lb-master-105 } vrrp_script check-haproxy { script "killall -0 haproxy" interval 5 weight -30 } vrrp_instance VI-kube-master { state MASTER priority 120 dont_track_primary interface ens33 virtual_router_id 68 advert_int 3 track_script { check-haproxy } virtual_ipaddress { 192.168.111.9 } } EOF
keepalived配置文件(2备):
cat > /etc/keepalived/keepalived.conf <<EOF global_defs { router_id lb-backup-105 } vrrp_script check-haproxy { script "killall -0 haproxy" interval 5 weight -30 } vrrp_instance VI-kube-master { state BACKUP priority 110 dont_track_primary interface ens33 virtual_router_id 68 advert_int 3 track_script { check-haproxy } virtual_ipaddress { 192.168.111.9 } } EOF
启动keepalived:
for SERVICES in keepalived;do systemctl enable $SERVICES; systemctl restart $SERVICES; systemctl status $SERVICES; done