Floating IP Setup Using KeepAlived on CentOS – 6/7/8 

Keepalived is used for IP failover between two servers. It facilities for load balancing and high-availability to Linux-based infrastructures. It works on VRRP ( Virtual Router Redundancy Protocol ) protocol. We have running two load balance servers using HAProxy and now we need to implement VRRP between both servers. This tutorial will help you to configure KeepAlived, Use this tutorial to configure HAProxy on both servers.

Network Scenario:

Now we are implementing ip failover setup between LB1 and LB2 servers.

Graphical representation of Fail over Setup:
keepalived-vrrp-network
Step 1 – Install Required Packages

Use the following command to install required packages to configure Keepalived on the server.

# yum install gcc kernel-headers kernel-devel
Step 2 – Install Keepalived

Keepalived is available in centos base repository. Install it using yum command line tool.

# yum install keepalived

Keepalived configuration File: /etc/keepalived/keepalived.conf

Step 3 – Configure Keepalived on LB1

Edit Keepalived configuration file on LB1 and add following configuration. 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 eth1
    virtual_router_id 51
    priority 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.10.121
    }
}
Step 4 – Configure KeepAlived on LB2

Edit Keepalived configuration file on LB2 and add 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 eth1
    virtual_router_id 51
    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

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

# service keepalived start
# chkconfig keepalived on
Step 6 – Check Virtual IPs

By default virtual IP will be assigned to master server, In 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 eth1

Sample output

2: eth1:  mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
    link/ether 00:0c:29:6f:ed:60 brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.111/24 brd 192.168.1.255 scope global eth1
    inet 192.168.10.121/32 scope global eth1
    inet6 fe80::20c:29ff:fe6f:ed60/64 scope link
       valid_lft forever preferred_lft forever
Step 7 – Verify IP Failover

Follow the below process to test keepalived failover is working correctly.

  • Shutdown master server ( LB1 ) and check if ips are automatically assigned to the slave server.# ip addr show eth1
  • Now start LB1 and stop slave server ( LB2 ). IPs will automatically be assigned to master server.# ip addr show eth1
  • Watch log files to insure its working# tailf /var/log/messages Sample OutputMar 19 17:30:24 localhost Keepalived_vrrp[6958]: VRRP_Instance(VI_1) Transition to MASTER STATE Mar 19 17:30:25 localhost Keepalived_vrrp[6958]: VRRP_Instance(VI_1) Entering MASTER STATE Mar 19 17:30:25 localhost Keepalived_vrrp[6958]: VRRP_Instance(VI_1) setting protocol VIPs. Mar 19 17:30:25 localhost Keepalived_healthcheckers[6957]: Netlink reflector reports IP 192.168.10.121 added Mar 19 17:30:25 localhost avahi-daemon[1407]: Registering new address record for 192.168.10.121 on eth1.IPv4. Mar 19 17:30:25 localhost Keepalived_vrrp[6958]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for

I hope this article will help to setup IP failover between two load balance servers.