Keep Alived

  • For Floating Virtual IP ,
    • default points to MASTER,
    • if MASTER fails then BACKUP
    • again once MASTER is up, points to it
  • ALternatives to keepalived are heartbeat, pacemaker ..etc

DOCS

Ref

Update repo

sudo apt update

install keepalived

on both server

sudo apt install keepalived

Setup Keepalived on LoadBalancerServer1 (say LBS1)

  • Virtual Router Redundancy Protocol (VRRP)
  • by default conf file should have 644 permission
vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
sysadmin@mydomain.com # custom to email
support@mydomain.com # custom to email
}
notification_email_from lbs1@mydomain.com # custom from email
smtp_server localhost # SMTP server address (here default localhost with port 25 mapped to some relay mail server)
smtp_connect_timeout 30
}
# Define the script used to check if haproxy is still working
vrrp_script chk_haproxy {
script "/usr/bin/killall -0 haproxy"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER # custom 'MASTER/ BACKUP'
interface eth0 # custom 'etho0/ eno1/ enp0s ..etc in "ip a"'
virtual_router_id 101 # should be same on both servers
priority 101 # custom set more than BACKUP, say BACKUP (99), MASTER (101)
advert_int 1
smtp_alert true # Enable Notifications Via Email
authentication {
auth_type PASS
auth_pass myP@ssword # Password for accessing vrrp. Same on all devices
}
unicast_src_ip 192.168.56.101 # Private IP address of master
unicast_peer {
192.168.56.102 # Private IP address of the backup haproxy
}
# The virtual ip address shared between the two loadbalancers
virtual_ipaddress {
192.168.56.103/24
}
# Use the Defined Script to Check whether to initiate a fail over
track_script {
chk_haproxy
}
}

Setup Keepalived on LoadBalancerServer2 (say LBS2)

  • Virtual Router Redundancy Protocol (VRRP)
vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
sysadmin@mydomain.com # custom to email
support@mydomain.com # custom to email
}
notification_email_from lbs1@mydomain.com # custom from email
smtp_server localhost # custom SMTP server address
smtp_connect_timeout 30
}
# Define the script used to check if haproxy is still working
vrrp_script chk_haproxy {
script "/usr/bin/killall -0 haproxy"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP # custom 'MASTER/ BACKUP'
interface eth0 # custom 'etho0/ eno1/ enp0s ..etc in "ip a"'
virtual_router_id 101 # should be same on both servers
priority 99 # custom set less than MASTER, say BACKUP (99), MASTER (101)
advert_int 1
smtp_alert true # Enable Notifications Via Email
authentication {
auth_type PASS
auth_pass myP@ssword # Password for accessing vrrp. Same on all devices
}
unicast_src_ip 192.168.56.102 # Private IP address of backup
unicast_peer {
192.168.56.101 # Private IP address of the master haproxy
}
# The virtual ip address shared between the two loadbalancers
virtual_ipaddress {
192.168.56.103/24
}
# Use the Defined Script to Check whether to initiate a fail over
track_script {
chk_haproxy
}
}

Check Virtual ip validity

  • if given virtual ip doesn't ping, take some other ip & try to add to device (without keepalived ) & ping it. If works use that IP instead

  • adding some IP to some network interface

ip addr add 10.25.95.245/24 dev eno1
//after ping test, remove
ip addr del 10.25.95.245/24 dev eno1

start & enable service

on both server

sudo systemctl start keepalived
sudo systemctl enable keepalived
  • check status
sudo systemctl status keepalived

Check the ip address assignment

say in master server

ip --brief add
//earlier output
enp0s8 UP 192.168.56.101/24
//now output
enp0s8 UP 192.168.56.101/24 192.168.58.10

so even virtual ip is assigned

  • shutdown the master server, now check same ip assignment for backup server - virtual ip should be mapped to it now

Custom script (track/notify)