How to Setup IP Failover on Ubuntu with KeepAlived

Keepalived is used for IP failover between two servers. Its facilities for load balancing and high-availability to Linux-based infrastructures. It worked on VRRP (Virtual Router Redundancy Protocol) protocol. In this tutorial, we have configured IP failover between two Linux systems running as a load balancer for load balancing and high-availability infrastructures.
You may also intrested in our tutorial Setup HAProxy on Ubuntu & Linuxmint .
Network Scenario:
  1. LB1 Server: 192.168.10.111 (eth0)   2. LB2 Server: 192.168.10.112 (eth0)   3. Virtual IP: 192.168.10.121 
keepalived-vrrp-network
I hope you get a better understanding about the setup with above structure. Let’s move to configuration IP failover setup between LB1 and LB2 servers.

Step 1. Install Required Packages

First of all, Use the following command to install required packages to configure Keepalived on the server.
$ sudo apt-get install linux-headers-$(uname -r) 

Step 2. Install Keepalived

Keepalived packages are available under default apt repositories. So just use below command to install it on both servers.
$ sudo apt-get install keepalived 

Step 3. Setup Keepalived on LB1.

Now create or edit Keepalived configuration /etc/keepalived/keepalived.conf file on LB1 and add following settings. Update all red highlighted values with your network and system configuration.
! Configuration File for keepalived  global_defs {    notification_email {      sysadmin@mydomain.com      support@mydomain.com    }    notification_email_from lb1@mydomain.com    smtp_server localhost    smtp_connect_timeout 30 }  vrrp_instance VI_1 {     state MASTER     interface eth0     virtual_router_id 101     priority 101     advert_int 1     authentication {         auth_type PASS         auth_pass 1111     }     virtual_ipaddress {         192.168.10.121     } } 

Step 4. Setup KeepAlived on LB2.

Also, create or edit Keepalived configuration file /etc/keepalived/keepalived.conf on LB2 and add the following configuration. While making changes in LB2 configuration file, make sure to set priority values to lower than LB1. For example below configuration is showing 100 priority value than LB1 has it 101.
! Configuration File for keepalived  global_defs {    notification_email {      sysadmin@mydomain.com      support@mydomain.com    }    notification_email_from lb2@mydomain.com    smtp_server localhost    smtp_connect_timeout 30 }  vrrp_instance VI_1 {     state MASTER     interface eth0     virtual_router_id 101     priority 100     advert_int 1     authentication {         auth_type PASS         auth_pass 1111     }     virtual_ipaddress {         192.168.10.121     } } 
1. Priority value will be higher on Master server, It doesn’t matter what you used in state. If your state is MASTER but your priority is lower than the router with BACKUP, you will lose the MASTER state. 2. virtual_router_id should be same on both LB1 and LB2 servers. 3. By default single vrrp_instance support up to 20 virtual_ipaddress. In order to add more addresses you need to add more vrrp_instance

Step 5. Start KeepAlived Deamon

Start KeepAlived service using the following command and also configure to autostart on system boot.
$ sudo service keepalived start 

Step 6. Check Virtual IPs

By default virtual IP will be assigned to master server, In the case of master gets down, it will automatically assign to the slave server. Use the following command to show assigned virtual IP on the interface.
$ ip addr show eth0 
Sample output
2: eth0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000     link/ether 08:00:27:b9:b0:de brd ff:ff:ff:ff:ff:ff     inet 192.168.10.111/24 brd 192.168.1.255 scope global eth0        valid_lft forever preferred_lft forever     inet 192.168.10.121/32 scope global eth0        valid_lft forever preferred_lft forever     inet6 fe80::11ab:eb3b:dbce:a119/64 scope link        valid_lft forever preferred_lft forever 

Step 7. Verify IP Failover Setup

  1. Shutdown master server (LB1) and check if ips are automatically assigned to slave server.
$ ip addr show eth0 
  1. Now start LB1 and stop slave server ( LB2 ). IPs will automatically assigned to master server.
$ ip addr show eth0 
  1. Watch log files to insure its working
$ tailf /var/log/syslog 
Sample Output
Feb  7 17:24:51 tecadmin Keepalived_healthcheckers[23177]: Registering Kernel netlink reflector Feb  7 17:24:51 tecadmin Keepalived_healthcheckers[23177]: Registering Kernel netlink command channel Feb  7 17:24:51 tecadmin Keepalived_healthcheckers[23177]: Opening file '/etc/keepalived/keepalived.conf'. Feb  7 17:24:51 tecadmin Keepalived_healthcheckers[23177]: Configuration is using : 11104 Bytes Feb  7 17:24:51 tecadmin Keepalived_healthcheckers[23177]: Using LinkWatch kernel netlink reflector... Feb  7 17:24:52 tecadmin Keepalived_vrrp[23178]: VRRP_Instance(VI_1) Transition to MASTER STATE Feb  7 17:24:53 tecadmin Keepalived_vrrp[23178]: VRRP_Instance(VI_1) Entering MASTER STATE Feb  7 17:24:53 tecadmin avahi-daemon[562]: Registering new address record for 192.168.10.121 on eth0.IPv4. 

Thanks for Visit Here

Comments