Sunday, May 15, 2016

Set up High Availability with keepalived

First install the dependency

yum install openssl openssl-devel popt popt-devel

Now install gcc to build keepalived

yum install gcc

Install kernel headers and kernel devel package

yum -y install kernel-headers kernel-devel

Download keepalived 1.2.19

wget http://www.keepalived.org/software/keepalived-1.2.19.tar.gz

tar xzvf keepalived*

cd keepalived*

./configure
make
make install

 Now create an init file and configure it to auto start after restart

vi /etc/init/keepalived.conf

description "load-balancing and high-availability service"

start on runlevel [2345]
stop on runlevel [!2345]

respawn

exec /usr/local/sbin/keepalived --dont-fork


Now create keepalived config file

mkdir -p /etc/keepalived

vi /etc/keepalived/keepalived.conf

vrrp_instance VI_1 {
        interface eth0
        state MASTER
        virtual_router_id 51
        priority 101
        unicast_src_ip primary_private_IP
        unicast_peer {
            secondary_private_IP
        }
        authentication {
            auth_type PASS
            auth_pass Add-Your-Password-Here
        }
        virtual_ipaddress {
                202.54.1.1/29 dev eth1
        }
}


Use unicast_peer and unicast_src_ip to use Unicast, otherwise by default it will use Multicast.

You can use nopreempt to prevent auto take over by Master.

You can use vrrp_script to track you own application status

vrrp_script chk_nginx {
    script "pidof nginx"
    interval 2
}


Complete config file will look like

vrrp_script chk_nginx {
    script "pidof nginx"
    interval 2
}

vrrp_instance VI_1 {
        interface eth0
        state MASTER
        virtual_router_id 51
        priority 101
        unicast_src_ip primary_private_IP
        unicast_peer {
            secondary_private_IP
        }
        authentication {
            auth_type PASS
            auth_pass Add-Your-Password-Here
        }
        virtual_ipaddress {
                202.54.1.1/29 dev eth1
        }
 
        track_script {
                chk_nginx
        }

    notify_master /etc/keepalived/master.sh

}

No comments:

Post a Comment