Handling nginx Failover With KeepAlived

by on February 25, 2010 · 4 comments· last updated at May 21, 2013

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.



You should follow me on twitter here or grab rss feed to keep track of new changes.

This FAQ entry is 3 of 8 in the "CentOS / RHEL nginx Reverse Proxy Tutorial" series. Keep reading the rest of the series:

{ 4 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

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <kbd> <blockquote> <pre> <a href="" title="">

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

Previous Faq:

Next Faq: