Nginx – Is load balancing or HA setup recommended

high-availabilityload balancingnginxweb-applicationsweb-server

I have an application which is running on a webserver(ws1) and connected to a app server(as1). I have the same application running on another webserver(ws2) and another appserver(as2). But I have only one reverse proxy server. So my setup is something like

server setup diagram

So currently I am load balancing between these servers. My Nginx configuration looks like this:

http {
    upstream myapp1 {
        server ws1.example.com;
        server ws2.example.com;

    }

    server {
        listen 80;

        location / {
            proxy_pass http://myapp1;
        }
    }
}

But is it better to load balance by Nginx or to make the HA setup. If I make HA setup should the web server and app server be clustered?

Best Answer

It's all very good to load balance the servers, but if the server providing the load balancing was to fall over, neither of the other two servers would be accessible.

One way to resolve that is to set up an HA arrangement so that if the load balancer was to drop out, another server would immediately take it's place.

To provide the HA you can use a service like keepalived which uses the VRRP protocol to provide a highly available Internet Address. In fact, it works very well. A configuration similar to the following would work.

Lets say you want a service to be visible at 10.10.10.100

Create 2 machines with one with IP 10.10.10.101 and the other with IP 10.10.10.102 Install keepalived service on both. Remember to set: net.ipv4.ip_nonlocal_bind = 1 In sysctl.conf on both machines.

Set up with nginx config as described.

First Nodes keepalived.conf:

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass secretpass
    }
    virtual_ipaddress {
        10.10.10.100  
    }
}

Second Node:

vrrp_instance VI_1 {
        state BACKUP
        interface eth0
        virtual_router_id 51
        priority 150
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass secretpass
        }
        virtual_ipaddress {
            10.10.10.100
        }
    }

Now what'll happen is when the master machine is running, it will provide the virtual ip 10.10.10.100 If you stop this machine, the other will take over the IP.

Fuller example: Simple keepalived failover setup on Ubuntu 14.04