Handling nginx Failover With KeepAlived

by on February 25, 2010 · 11 comments· LAST UPDATED May 21, 2013

in , ,

How do configure to release and obtain VIP (virtual IP) when nginx is dead, down or system is rebooted for the kernel upgrades?

Edit /usr/local/etc/keepalived/keepalived.conf and add the following section to check whether nginx is alive or dead:
# vi /usr/local/etc/keepalived/keepalived.conf
Updated file on both lb0 and lb1:

vrrp_script chk_http_port {
        script "/usr/bin/killall -0 nginx"
        interval 2
        weight 2
}
vrrp_instance VI_1 {
        interface eth0
        state MASTER
        virtual_router_id 51
        priority 101
        authentication {
            auth_type PASS
            auth_pass Add-Your-Password-Here
        }
        track_script {
            chk_http_port
        }
        virtual_ipaddress {
                202.54.1.1/29 dev eth1
        }
}
 

Save and close the file. Reload keealived:
# /etc/init.d/keepalived restart
If nginx died due to any issues keepalived will release master VIP and backup server will become active. When master nginx LB0 comes backs online, the backup LB1 will go down in backup state.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 11 comments… read them below or add one }

1 Ravi Chandra February 25, 2010 at 11:55 am

Hi,

I have been following blog since about 8 months now. I like it and I cant restrain from appreciating your efforts.

Can you share knowledge you have on “How to distribute load between several apache servers and decrease response delay for end users”.

Also a small advice: Please provide some back ground in addition to implementation. For example: you can explain about reverse proxy in 3-4 lines before you jump onto impementation.

Thanks.

Reply

2 Simon August 14, 2010 at 1:34 am

In order to allow nginx to bind to the virtual IP on machine(s) which are in vrrp BACKUP state, on debian, I had to do the following —

1) Add net.ipv4.ip_nonlocal_bind=1 to /etc/sysctl.conf
2) Run sysctl -p

Then nginx can start even when the machine hasn’t got the virtual IP(s).

Reply

3 Klaus February 6, 2012 at 1:33 pm

Thanks a lot Simon, that hint made my day!

Reply

4 benjo April 4, 2013 at 6:42 pm

We are using keepalived for vrrp facility to allow network level ip fail over for our application.

If we need to take care applicaiton hang issue or any problem with it so in that case , do we need any script to integrate with keepalived or what is the better way?

Reply

5 saurabh January 30, 2014 at 4:29 pm

Keepalived config for Master host(HOST A) which has its interface IP in the VIP block

::interface ip of (eth1 30.30.30.1/24) sample config::

eth1 is 30.30.30.1/24
keepalived.conf file(Master one)::
vrrp_instance VI {
state MASTER
interface eth1
virtual_router_id 1
advert_int 1
priority 150
authentication {
auth_type NONE
auth_pass 1112
}
virtual_ipaddress {
30.30.30.1/24
30.30.30.3/24
}
}

In this file there is no field to specifically save the primary address & all configured addresses goes to the VIP list block. Can you please let me know where i can use primary ip address will be configured in above configuration file.

Moreover if we add Primary address (which is always the interface address as ip owner case says) to the VIP block in KA: we see the following issue::

We have two hosts which are participating in a VRRP instance. They have same priorities(above see config)::

Host A – Interface IP “30.30.30.1” (Lower), priority “150” (same), vrrp instance “1” (same).
Host B – Interface IP “30.30.30.2” (Higher), priority “150” (same), vrrp instance “1” (same).

Description – Currently we have Host B as “Master” and Host A as “Backup” because Host B’s Interface IP is higher than Host A and their priorities are same.
We add two entries in the VIP (virtual IP address) block in “Keepalived.conf” file of both the hosts i.e. 30.30.30.3 & 30.30.30.1 (Host A interface IP).
As per the config if we reload keepalived on both the hosts, Host A interface should become the master as this will be our IP address Owner case. But this does not happens.
Expected behavior – Host A Interface should become Master as Interface IP address is same as primary IP (IP address owner case).
Current behavior – Host B interface acts as Master. Host A state is fluctuating between Master & Backup and its Not consistent.
Secondly when we stop the KA on host A in this scenario it removes the Virtual IP address from the interfaces, since in this case we have added the real interface IP address here thus that IP address is also removed (from the actual interface). This will result in even bad state for a system.

Reply

6 saurabh January 30, 2014 at 4:32 pm

i have query regarding above mail that in keepalive ip owner case is not working as expected.. can you please help me how we can test this case. is my configuration is valid ?

And one more query where primary ip address is configured in keepalived.conf file ?

Reply

7 Klaus January 31, 2014 at 11:52 am

Hi Saurabh,

you need a extra IP for VIP. Lets say your NginX1 has 192.168.1.1 (RIP=Real-IP) and your NginX2 has 192.168.1.2 (RIP), than you will need an extra, third IP for VIP.

Keep in mind, your NginXes will always talk to the Backend servers with their RIP.

Reply

8 Klaus January 31, 2014 at 11:53 am

Sorry forgot, in the above Case your VIP could be 192.168.1.3 for example.

Reply

9 saurabh February 1, 2014 at 2:46 pm

IF we Configure different ip(except real ip[RIP]) in VIP block as you suggested 192.168.1.3 but in this case how we achieve ip owner case ? As ip owner case is that when your real interface ip is similar to virtual ip(vip) configured under VRRP then it is the ip owner case and by default it have priority 255 and become master… how we can test this scenario then ??? with above explanation it seems we cant configure RIP under VIP block is it right ????

please help me regarding above query…

Reply

10 saurabh January 31, 2014 at 1:33 pm

Thanks for your reply!
But still i am facing issue i already mentioned in host A(interface RIP is 30.30.30.1/24) & i used one RIP(real ip) and other is different 30.30.30.3/24 in VIP block and other host B RIP is 30.30.30.2/24.(please refer above for config of host A)

virtual_ipaddress {
30.30.30.1/24
30.30.30.3/24
}

Priority is same here in both host.

As per the config if we reload keepalived on both the hosts, Host A interface should become the master as this will be our IP address Owner case. But this does not happens.

Expected behavior – Host A Interface should become Master as Interface IP address is same as primary IP (IP address owner case).

Current behavior – Host B interface acts as Master. Host A state is fluctuating between Master & Backup and its Not consistent.

Secondly when we stop the KA on host A in this scenario it removes the Virtual IP address from the interfaces, since in this case we have added the real interface IP address here thus that IP address is also removed (from the actual interface). This will result in even bad state for a system.

Reply

11 saurabh February 5, 2014 at 10:04 am

Can anyone help me how ip owner case works in keepalive package for vrrp , please use sample configuration of two host connectivity to show me how it works….Thanks in advance for your help…

Reply

Leave a Comment

Tagged as: , , , , , , , , ,

Previous Faq:

Next Faq: