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 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
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 interfaceeth0 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 interfaceeth0 virtual_router_id 101 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress {192.168.10.121 } }
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
- Shutdown master server (LB1) and check if ips are automatically assigned to slave server.
ip addr show eth0
- Now start LB1 and stop slave server (LB2). IPs will automatically assigned to master server.
ip addr show eth0
- 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.
9 Comments
state MASTER
Why both the configuration files have MASTER as status?
Why both state are MASTER?
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
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.
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
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.
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?
can you please help me in configuring keepalived with Active-passive-passive with 3 nodes
You can configure the 3’rd server (LB3) same as added LB2.