Skip to main content

Raymii.org Logo (IEC resistor symbol)logo

Quis custodiet ipsos custodes?
Home | About | All pages | RSS Feed | Gopher

Simple keepalived failover setup on Ubuntu 14.04

Published: 13-06-2014 | Author: Remy van Elst | Text only version of this article


Table of Contents


We are going to set up very simple keepalived IP failover on Ubuntu 14.04.Keepalived is a piece of software which can be used to achieve high availabilityby assigning two or more nodes a virtual IP and monitoring those nodes, failingover when one goes down. Keepalived can do more, like load balancing andmonitoring, but this tutorial focusses on a very simple setup, just IP failover.

If you like this article, consider sponsoring me by trying out a Digital OceanVPS. With this link you'll get $100 credit for 60 days). (referral link)

Internally keepalived uses VRRP. The VRRP protocol ensures that one ofparticipating nodes is master. The backup node(s) listens for multicast packetsfrom a node with a higher priority. If the backup node fails to receive VRRPadvertisements for a period longer than three times of the advertisement timer,the backup node takes the master state and assigns the configured IP(s) toitself. In case there are more than one backup nodes with the same priority, theone with the highest IP wins the election.

I'm also a fan of Corosync/Pacemaker, you can see my articles about Corosynchere.

We'll install nginx and edit the default webpage, just to see where the IP ispointing to.

Requirements

You'll need the following to get started with keepalived:

I'll be using Ubuntu 14.04 servers in this example. These servers are in the10.32.75.0/24 network. The virtual IP will be 10.32.75.200.

Install packages

Use apt to install the required packages:

apt-get install nginx keepalived

Configuring keepalived

Create the config file on the first server (10.32.75.12):

vim /etc/keepalived/keepalived.conf

Edit and paste the following config:

! Configuration File for keepalivedvrrp_instance VI_1 {    state MASTER    interface eth0    virtual_router_id 51    priority 150    advert_int 1    authentication {        auth_type PASS        auth_pass $ place secure password here.    }    virtual_ipaddress {        10.32.75.200    }}

Create the config file on the second server (10.32.75.14):

vim /etc/keepalived/keepalived.conf

Edit and paste the following config:

! Configuration File for keepalivedvrrp_instance VI_1 {    state MASTER    interface eth0    virtual_router_id 51    priority 100    advert_int 1    authentication {        auth_type PASS        auth_pass $ place secure password here.    }    virtual_ipaddress {        10.32.75.200    }}

The priority must be highest on the server you want to be the master/primary.It can be 150 on the master, and 100, 99, 98, 97 on the slaves. Thevirtual_router_id must be the same on all nodes and the auth_pass must alsobe the same. My network configuration is on eth0, change it if yours is onanother one.

Configuring NGINX

For this example I have set up a very simple NGINX server with a very simpleHTML page.

vim /usr/share/nginx/html/index.html

Server 1:

<!DOCTYPE html><html><head><title>Keepalived 1!</title><style>    body {        width: 35em;        margin: 0 auto;        font-family: Tahoma, Verdana, Arial, sans-serif;    }</style></head><body><h1>Keepalived 1 - MASTER!</h1></body></html>

Server 2:

<!DOCTYPE html><html><head><title>Keepalived 2!</title><style>    body {        width: 35em;        margin: 0 auto;        font-family: Tahoma, Verdana, Arial, sans-serif;    }</style></head><body><h1>Keepalived 2 - backup!</h1></body></html>

sysctl

In order to be able to bind on a IP which is not yet defined on the system, weneed to enable non local binding at the kernel level.

Temporary:

echo 1 > /proc/sys/net/ipv4/ip_nonlocal_bind

Permanent:

Add this to /etc/sysctl.conf:

net.ipv4.ip_nonlocal_bind = 1

Enable with:

sysctl -p

Start & Failover

When the website is set up we can start both NGINX and Keepalived on bothservers:

service keepalived startservice nginx start

Visit the IP you configured as a failover IP in your browser. You should see thepage for server 1.

Let's do a test failover. On server 1, stop keepalived:

service keepalived stop

Refresh the webpage. You should see the page for server 2. The logging will showsomething like this:

tail /var/log/syslog

Output:

Jun 13 22:50:59 ha2-ubu1 Keepalived_vrrp[1579]: VRRP_Instance(VI_1) Transition to MASTER STATEJun 13 22:51:00 ha2-ubu1 Keepalived_vrrp[1579]: VRRP_Instance(VI_1) Entering MASTER STATEJun 13 22:51:01 ha2-ubu1 ntpd[1445]: Listen normally on 9 eth0 10.32.75.200 UDP 123Jun 13 22:51:01 ha2-ubu1 ntpd[1445]: peers refreshedJun 13 22:51:01 ha2-ubu1 ntpd[1445]: new interface(s) found: waking up resolver

As you can see, for a simple IP failover, keepalived is much simpler thancorosync/pacemaker to set up.

You can read more on keepalived on their website. Another article heredescribes how to do load balancing with keepalived.

Tags: cluster, heartbeat, high-availability, keepalived, network, tutorials, vrrp