In my case, I use HAProxy to LoadBalance traffic for the kube api server tasks and create a Virtual IP within it using Keep Alived
sudo apt install -y haproxy
sudo apt install -y keepalived
sudo nano /etc/sysctl.conf
# Add this on the bottom of the file
net.ipv4.ip_nonlocal_bind=1
# Activate the configuration
sudo sysctl -p
Set the VirtualIP and the load balancing algorithm, make it the same if you want equal load balancing:
sudo nano /etc/keepalived/keepalived.conf
# INI WAJIB YAK
chmod 644 /etc/keepalived/keepalived.conf
# Add this to the file
vrrp_script check_haproxy {
script "killall -0 haproxy" # cheaper than pidof
interval 2 # check every 2 seconds
weight 2 # add 2 points of priority if OK
}
vrrp_instance VI_1 {
state BACKUP # Use BACKUP on all nodes for equal load balancing | Options: MASTER / BACKUP
interface eth0 # Network interface to bind the virtual IP
virtual_router_id 51 # Make it same for all nodes
priority 100 # Same priority on all nodes for equal load balancing
advert_int 1 # Interval sending advertisement on backup routers
virtualipaddress {
192.168.100.210 # The floating IP
}
track_script {
check_haproxy
}
}
sudo service keepalived restart
Move on to configuring HAProxy, you can access the config at the path below:
sudo nano /etc/haproxy/haproxy.cfg
# Add these lines at the bottom of the existing configuration
# Frontend configuration: Receives traffic on the virtual IP
frontend k8s-api
bind 192.168.100.210:6444
default_backend k8s-api-backend
# Backend configuration: Distributes traffic to all master nodes
backend k8s-api-backend
balance roundrobin
option tcp-check
server master1 192.168.100.20:6443 check
server master2 192.168.100.21:6443 check
server master3 192.168.100.22:6443 check