apache failover using keepalived

Hi Techrunnr Readers, this document deals with configuration of apache failover using keepalived. Configuring failover helps to run a service continuously if any of the systems failed without any help of human intervention.

apache

Here we are configuring Failover for apache service using keepalived with floating IP. Floating IP will switch from master to backup server automatically when the master is unavailable.

Here is the configuration to how to set up failover for apache service.

Prerequisites

* 2 Servers with root access
* Floating IP

Installation

My setup master server is 10.0.0.253 and backup server is 10.0.0.252 . 10.0.0.247 is floating IP.

1. Install Apache web server in both servers.

apt-get install apache2 //Ubuntu

yum install httpd //CENTOS

2. Edit index.html page in both servers

vi /var/www/html/index.html

Primary


vi /var/www/html/index.html

Secondary

3. Install keepalived.

apt-get install keepalived //Ubuntu

yum install keepalived //Centos
Configuration

1. Configure keepalived as follows in the master server (10.0.0.253)


vrrp_script chk_apache2 {
script "pidof apache2"
interval 2
}
vrrp_instance VI_1 {
interface ens160
state MASTER
advert_int 2
virtual_router_id 51
priority 100
authentication {
auth_type PASS
auth_pass SimplePassword
}
unicast_src_ip addr:10.0.0.253
unicast_peer {
10.0.0.252
}
track_script {
chk_apache2
}
virtual_ipaddress {
10.0.0.247
}
}

NOTE: if you are using centos or Redhat , then change apache2 to httpd and Configure interface name according to the environment.

2. Configure keepalived as follows in the backup server (10.0.0.252)


vrrp_script chk_apache2 {
script "pidof apache2"
interval 2
}
vrrp_instance VI_1 {
interface ens160
state BACKUP
advert_int 2
virtual_router_id 51
priority 50
authentication {
auth_type PASS
auth_pass SimplePassword
}
unicast_src_ip 10.0.0.252
unicast_peer {
10.0.0.253
}
track_script {
chk_apache2
}
virtual_ipaddress {
10.0.0.247
}
}

NOTE: if you are using centos or Redhat , then change apache2 to httpd and Configure interface name according to the environment.

3. Now start keepalived service on both the servers.

/etc/init.d/keepalived restart
Testing

1. Now you can access your web server using the floating IP.

2. If you type ” ip addr ” in each server you can see the following output shows floating Ip assigned.


1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host 
valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:86:f9:dc brd ff:ff:ff:ff:ff:ff
inet 10.0.0.253/24 brd 10.0.0.255 scope global ens160
valid_lft forever preferred_lft forever
inet 10.0.0.247/32 scope global ens160
valid_lft forever preferred_lft forever
inet 10.0.0.247/24 brd 10.0.0.255 scope global secondary ens160:0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe86:f9dc/64 scope link 
valid_lft forever preferred_lft forever

3. Now you can shut down the master server and access the web server.


You can still see floating IP serves the users request and these requests are served from the backup server.

if you check the ” ip addr ” output you can see the floating IP has switched from Master server to Backup server.

 

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:ca:58:05 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.252/24 brd 10.0.0.255 scope global ens160
       valid_lft forever preferred_lft forever
    inet 10.0.0.247/32 scope global ens160
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feca:5805/64 scope link 
       valid_lft forever preferred_lft forever

That’sĀ it. Now you have successfully configured failoverĀ for apache service using keepalived.

 

© 2018, Techrunnr. All rights reserved.

#1
Questions Answered
Articles Written
Overall Points

Prabhin Prabharkaran

He is Technical professional. He is a person who loves to share tricks and tips on the Internet. He Posts what he does!!

Leave a Reply

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.