Configuring Linux High Availability with Keepalived

This guide was completed using CentOS 6.5, and walks you through building a keepalived RPM from source. In addition, configuring keepalived in a HA high availability configuration. I have configured Keepalived as a HA provider for both MySQL and Nginx load balancer. It is extremely stable software and I have been running it in production for 5 years.

First download the latest version of keepalived, then compile the software into an RPM to be installed.

Download keepalived using wget

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

un-compress the source code

tar -zxvf keepalived-1.2.9.tar.gz
cd keepalived-1.2.9

Configure and build the source code

./configure
make

Setup the RPM build environment

mkdir /root/rpmbuild
mkdir /root/rpmbuild/SOURCES/
mkdir /root/rpmbuild/SPECS

Copy the source code and SPEC files into the build directories

cp /root/keepalived-1.2.9.tar.gz /root/rpmbuild/SOURCES/
cp /root/keepalived-1.2.9/keepalived.spec /root/rpmbuild/SPECS/

Perform the build of Keepalived

rpmbuild -ba /root/rpmbuild/SPECS/keepalived.spec

The new KeepaliveD rpm is located here:

/root/rpmbuild/RPMS/x86_64/keepalived-1.2.9-5.x86_64.rpm

Install the RPM on both servers in the HA cluster.

rpm -ivh /root/rpmbuild/RPMS/x86_64/keepalived-1.2.9-5.x86_64.rpm

Ensure keepalived is stopped

service keepalived stop

In the next step we will configure Keepalived on a pair of MYSQL servers. The MySQL servers will both be writable. However, the VIP will only reside on one node. When the primary MySQL server fails the VIP will “float” to the secondary server.

In this configuration there are two MySQL Servers. MYSQL01 and MYSQL02 with a virtual IP (VIP) which floats between the two MYSQL servers.

Virtual IP: 172.16.3.207
MYSQL01 (MASTER HOST IP): 172.16.3.160
MYSQL02 (SLAVE HOST IP): 172.16.3.163

On the MASTER Keepalived Configuration: MYSQL01

! Configuration File for MASTER keepalived

global_defs {
   notification_email {
     user_email@domain.com
   }
   notification_email_from master@domain.com
   smtp_server smtp.domain.com
   smtp_connect_timeout 30
   
}

vrrp_sync_group VG1 {
   group {
      VI_101
   }
}

vrrp_instance VI_101 {
    state MASTER
    interface bond0
    virtual_router_id 52
    priority 255
    advert_int 5
    lvs_sync_daemon_interface bond0
    authentication {
        auth_type PASS
        auth_pass 3636
    }
    virtual_ipaddress {
        172.16.3.207/18 dev bond0
    }
}


virtual_server 172.16.3.207 3306 {
        delay_loop 2
        lb_algo wrr
        persistence_timeout 500
        protocol TCP
        real_server 172.16.3.160 3306 {
        weight 3
                TCP_CHECK {
                connect_timeout 15
                nb_get_retry 3
                delay_before_retry 10
                connect_port 3306
                }
      }
}
}

On the SLAVE Keepalived Configuration: MYSQL02

Lines which should be changed in the BACKUP keepalived.conf

notification_email_from slave@domain.com
state BACKUP

Edit the backup keepalived.conf file

vim /etc/keepalived/keepalived.conf
! Configuration File for backup keepalived

global_defs {
   notification_email {
     user_email@domain.com
   }
   notification_email_from slave@domain.com
   smtp_server smtp.domain.com
   smtp_connect_timeout 30
}

vrrp_sync_group VG1 {
   group {
      VI_101
   }
}

vrrp_instance VI_101 {
    state BACKUP
    interface eth0
    virtual_router_id 52
    priority 10
    advert_int 5
    lvs_sync_daemon_interface eth0
    authentication {
        auth_type PASS
        auth_pass 3636
    }
    virtual_ipaddress {
       172.16.3.207/18 dev eth0
    }
}

virtual_server 172.16.3.207 3306 {
        delay_loop 2
        lb_algo wrr
        persistence_timeout 500
        protocol TCP
        real_server 172.16.3.163 3306 {
        weight 3
                TCP_CHECK {
                connect_timeout 30
                nb_get_retry 3
                delay_before_retry 30
                connect_port 3306
                }
       }
}
}

Start keepalived on MYSQL01 and MYSQL02

service keepalived start

Verify that the Virtual IP address is up

ip addr show

You should see that the VIP 172.16.3.207 is up

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc mq master bond0 state UP qlen 1000
    link/ether f8:db:88:56:13:8f brd ff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc mq master bond0 state UP qlen 1000
    link/ether f8:db:88:56:13:8f brd ff:ff:ff:ff:ff:ff
4: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
    link/ether f8:db:88:56:13:8f brd ff:ff:ff:ff:ff:ff
    inet 172.16.3.160/18 brd 172.16.63.255 scope global bond0
    inet 172.16.3.207/18 scope global secondary bond0
    inet6 fe80::fadb:88ff:fe56:138f/64 scope link
       valid_lft forever preferred_lft forever
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s