Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Tutorials»How to Setup IP Failover with KeepAlived on Ubuntu & Debian

    How to Setup IP Failover with KeepAlived on Ubuntu & Debian

    By RahulOctober 22, 20194 Mins Read

    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.

    Advertisement

    You may also intrested in our tutorial How to 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 of the setup with the above structure. Let’s move to the 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 update
    sudo apt-get install linux-headers-$(uname -r)
    

    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 LB1.

    Now create or edit Keepalived configuration /etc/keepalived/keepalived.conf file on LB1 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 {
         [email protected]
         [email protected]
       }
       notification_email_from [email protected]
       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 the 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.

    vim /etc/keepalived/keepalived.conf
    
    ! Configuration File for keepalived
    
    global_defs {
       notification_email {
         [email protected]
         [email protected]
       }
       notification_email_from [email protected]
       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 Service

    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 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

    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.
    

    cluster high availability IP Failover keepalived
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    An Introduction to the “./configure” Command: Compiling Source Code in Linux

    Getting Started with Linux Command line: The Beginning

    Backing Up Your Linux System with Rsync: A Step-by-Step Guide

    View 9 Comments

    9 Comments

    1. B M Shukla on July 9, 2021 6:52 am

      state MASTER
      Why both the configuration files have MASTER as status?

      Reply
    2. Dave on February 16, 2021 6:41 am

      Why both state are MASTER?

      Reply
    3. Sravan on December 16, 2019 6:28 pm

      I am getting below error when running keepalived

      Keepalived[1423]VRRP child process(16237) died: Respawning
      Keepalived_vrrp[11111]: pid 11111 exited with permanent error CONFIG. Terminating

      My keepalived version is below
      Keepalived v1.2.24

      OS: Ubuntu 16

      Reply
    4. Daniel on November 18, 2019 7:34 am

      Ahh I see, ignore that, once it fails over it keeps an iron grip on the IP until it loses it. Or at least it does on my servers. A full shutdown on the secondary pushed it back to the primary. So I guess I should spec up my servers the same.

      Reply
    5. Daniel on November 18, 2019 7:27 am

      This article was great and I was able to configure this correctly however I noticed both keepalived configs state MASTER. So my first attempt ignored the first host. I was able to get past it via another article which advised SLAVE

      Reply
    6. Adam S. on November 21, 2018 5:27 pm

      How do you solve the problem of apt-daily-upgrade in Ubuntu 18.04 knocking the virtual IP offline when it restarts systemd-networkd? The virtual IP disappears, but keepalived keeps communicating to the other loadbalancers, so they don’t takeover, and the first loadbalancer never attempts to bind to the virtual IP again unless you restart keepalived. This means that once a week the virtual IP disappears and the application behind the loadbalancers is inaccessible.

      Reply
    7. Israel on May 25, 2018 7:49 pm

      Ola, não vi como ele vai identificar que o link caiu, ou seja, se a interface estiver “UP”, ele nao pricsa trocar o link, porém se não houver internet no link como ele vai tomar a decisão e trocar o link?

      Reply
    8. Rajasekhar on May 5, 2018 6:00 am

      can you please help me in configuring keepalived with Active-passive-passive with 3 nodes

      Reply
      • Rahul K. on May 5, 2018 6:42 am

        You can configure the 3’rd server (LB3) same as added LB2.

        Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to Split Large Archives in Linux using the Command Line
    • System.out.println() Method in Java: A Beginner’s Guide
    • Split Command in Linux With Examples (Split Large Files)
    • Test Your Internet Speed from the Linux Terminal
    • 11 Practical Example of cat Command in Linux
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.