How to configure keepalived service in CentOS 6/7


Keepalived is a service in Linux to manage the failover between two servers. You can deploy this service in High-viability environment where your public interface IP(Virtual IP address) need to switch between two server when one goes down.

Requirement
Server1 ip address : 192.168.35.147
Server2 ip address: 192.168.35.144
Virtual ip address: 192.168.35.150

Install dependency package on both server
# yum install gcc kernel-headers kernel-devel

Install keepalived package on both server
# yum install keepalived

Configure keepalived on server1
# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.35.150
    }
}
Configure keepalived on server2
# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.35.150
    }
}

Start service on both server (On CentOS 6)
# service keepalived start
# chkconfig keepalived on

Start service on both server (On CentOS 7)
#sysctemctl start keepalived.service
#systemctl enabled keepalived.service

To test the keepliaved service configure apache service on both server.
# yum install httpd

Start apache service (On CentOS 6)
# service httpd start
#chkconfig httpd on

Start apache service (On CentOS 7)
#systemctl  start httpd
#systemctl enabled httpd

Now access server using virtual ip which you have defined in configuration file
URL:  http://Virtual_IP_Address/