0

Setup IP Failover with KeepAlived on Ubuntu & Debian

Introduction

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.

Network Scenario:
  1. Server1: 192.168.10.111 (eth0)
  2. Server2: 192.168.10.112 (eth0)
  3. Virtual IP: 192.168.10.121

I hope you get a better understanding of the setup with the above structure. Let’s move to the configuration IP failover setup between Server1 and Server2.

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 update
$ sudo apt-get upgrate
$ sudo reboot

Step 2 – Install Keepalived

Keepalived packages are available under default apt repositories. So just use a command to install it on both servers.

$ sudo apt-get install keepalived

Step 3 – Setup Keepalived on Server1.

Now create or edit the Keepalived configuration /etc/keepalived/keepalived.conf file on server1 and add the following settings. Update all red highlighted values with your network and system configuration.

vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     sysadmin@domain.com
     support@domain.com
   }
   notification_email_from server1@domain.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 Server2.

Also, create or edit Keepalived configuration file /etc/keepalived/keepalived.conf on server 2 and add the following configuration. While making changes in the server 2 configuration file, make sure to set priority values to lower than the server 1. For example below configuration is showing 100 priority value than the server 1 has it 101.

$ vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     sysadmin@mydomain.com
     support@mydomain.com
   }
   notification_email_from server2@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
    }
}

Priority the value will be higher on the Master server, It doesn’t matter what you used in the state. If your state is MASTER but your priority is lower than the router with BACKUP, you will lose the MASTER state.

virtual_router_id should be the same on both LB1 and LB2 servers.

By default single vrrp_instance supports up to 20 virtual_ipaddress. In order to add more addresses, you need to add more vrrp_instance

Step 5 – Start KeepAlived Service

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

$ sudo systemctl enable keepalived

$ sudo service keepalived start

Step 6 – Check Virtual IPs

By default virtual IP will be assigned to the 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

  • Shutdown master server (server1) and check if IPs are automatically assigned to the slave server.
$ ip addr show eth0
  • Now start server1 and stop the slave server (server2). IPs will automatically be assigned to the master server.
$ ip addr show eth0
  • Watch log files to ensure its working
$ tailf /var/log/syslog

Sample Output

Feb  7 17:24:51 Keepalived_healthcheckers[23177]: Registering Kernel netlink reflector
Feb  7 17:24:51 Keepalived_healthcheckers[23177]: Registering Kernel netlink command channel
Feb  7 17:24:51 Keepalived_healthcheckers[23177]: Opening file '/etc/keepalived/keepalived.conf'.
Feb  7 17:24:51 Keepalived_healthcheckers[23177]: Configuration is using : 11104 Bytes
Feb  7 17:24:51 Keepalived_healthcheckers[23177]: Using LinkWatch kernel netlink reflector...
Feb  7 17:24:52 Keepalived_vrrp[23178]: VRRP_Instance(VI_1) Transition to MASTER STATE
Feb  7 17:24:53 Keepalived_vrrp[23178]: VRRP_Instance(VI_1) Entering MASTER STATE
Feb  7 17:24:53 avahi-daemon[562]: Registering new address record for 192.168.10.

Leave a Reply

Your email address will not be published. Required fields are marked *