This is a text-only version of the following page on https://raymii.org: --- Title : Simple keepalived failover setup on Ubuntu 14.04 Author : Remy van Elst Date : 13-06-2014 URL : https://raymii.org/s/tutorials/Keepalived-Simple-IP-failover-on-Ubuntu.html Format : Markdown/HTML --- 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 availability by assigning two or more nodes a virtual IP and monitoring those nodes, failing over when one goes down. Keepalived can do more, like load balancing and monitoring, but this tutorial focusses on a very simple setup, just IP failover. <p class="ad"> <b>Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below:</b><br><br> <a href="https://leafnode.nl">I'm developing an open source monitoring app called Leaf Node Monitoring, for windows, linux & android. Go check it out!</a><br><br> <a href="https://github.com/sponsors/RaymiiOrg/">Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.</a><br><br> <a href="https://www.digitalocean.com/?refcode=7435ae6b8212">You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $100 credit for 60 days. </a><br><br> </p> Internally keepalived uses VRRP. The VRRP protocol ensures that one of participating nodes is master. The backup node(s) listens for multicast packets from a node with a higher priority. If the backup node fails to receive VRRP advertisements for a period longer than three times of the advertisement timer, the backup node takes the master state and assigns the configured IP(s) to itself. In case there are more than one backup nodes with the same priority, the one with the highest IP wins the election. I'm also a fan of Corosync/Pacemaker, you can see my articles about [Corosync here][2]. We'll install nginx and edit the default webpage, just to see where the IP is pointing to. ### Requirements You'll need the following to get started with keepalived: * 2 servers in the same network I'll be using Ubuntu 14.04 servers in this example. These servers are in the `10.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 keepalived vrrp_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 keepalived vrrp_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. The `virtual_router_id` must be the same on all nodes and the `auth_pass` must also be the same. My network configuration is on `eth0`, change it if yours is on another one. ### Configuring NGINX For this example I have set up a very simple NGINX server with a very simple HTML 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, we need 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 both servers: service keepalived start service nginx start Visit the IP you configured as a failover IP in your browser. You should see the page 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 show something like this: tail /var/log/syslog Output: Jun 13 22:50:59 ha2-ubu1 Keepalived_vrrp[1579]: VRRP_Instance(VI_1) Transition to MASTER STATE Jun 13 22:51:00 ha2-ubu1 Keepalived_vrrp[1579]: VRRP_Instance(VI_1) Entering MASTER STATE Jun 13 22:51:01 ha2-ubu1 ntpd[1445]: Listen normally on 9 eth0 10.32.75.200 UDP 123 Jun 13 22:51:01 ha2-ubu1 ntpd[1445]: peers refreshed Jun 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 than corosync/pacemaker to set up. You can read more on keepalived on [their website][3]. Another article [here][4] describes how to do load balancing with keepalived. [1]: https://www.digitalocean.com/?refcode=7435ae6b8212 [2]: https://raymii.org/s/tags/corosync.html [3]: http://keepalived.org [4]: http://gcharriere.com/blog/?p=339 --- License: All the text on this website is free as in freedom unless stated otherwise. This means you can use it in any way you want, you can copy it, change it the way you like and republish it, as long as you release the (modified) content under the same license to give others the same freedoms you've got and place my name and a link to this site with the article as source. This site uses Google Analytics for statistics and Google Adwords for advertisements. You are tracked and Google knows everything about you. Use an adblocker like ublock-origin if you don't want it. All the code on this website is licensed under the GNU GPL v3 license unless already licensed under a license which does not allows this form of licensing or if another license is stated on that page / in that software: This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. Just to be clear, the information on this website is for meant for educational purposes and you use it at your own risk. I do not take responsibility if you screw something up. Use common sense, do not 'rm -rf /' as root for example. If you have any questions then do not hesitate to contact me. See https://raymii.org/s/static/About.html for details.