Using a load balancer

Websites that deal with huge traffic generally use a technique called load balancing to improve the availability and responsiveness of applications. A load balancer distributes incoming traffic among multiple servers hosting same content. The distribution of load is determined by various scheduler algorithms.

In this section, we will see how to add a load balancer in front of our application servers (assuming that they are running on the IPs 127.0.0.1, 127.0.0.2, and 127.0.0.3 on the port 9000) using different HTTP web servers.

Apache HTTP

The Apache HTTP server provides a secure, efficient, and extensible server that supports HTTP services. The Apache HTTP server can be used as a load balancer through its mod_proxy and mod_proxy_balance modules.

To use Apache HTTP as a load balancer, mod_proxy and mod_proxy_balancer have to be present in the server. To set up the load balancer, all we need to do is update /etc/httpd/conf/httpd.conf.

Let's update the configuration step by step:

  1. Declare VirtualHost:
    <VirtualHost *:80>
    </VirtualHost>
  2. Disable the forward proxy for VirtualHost so that our server cannot be used for masking the identities of clients from the source servers:
    ProxyRequests off
  3. Instead of a document root, we should add a proxy with balancer identifier and BalanceMembers. Also, if we want to use the round-robin strategy, we also need to set it as lbmethod (load balancing method):
    <Proxy balancer://app>
        BalancerMember http://127.0.0.1:9000
           BalancerMember http://127.0.0.2:9000
           BalancerMember http://127.0.0.3:9000
        ProxySet lbmethod=byrequests
    </Proxy>
  4. Now, we need to add the access permissions for the proxy, which should be accessible to everyone:
                    Order Deny,Allow
                    Deny from none
                    Allow from all
  5. Finally, we need to map the proxy to the path that we want to load the application on the server to. This can be done with a single line:
    ProxyPass / balancer://app/

The configuration that needs to be added to the Apache HTTP configuration file is as follows:

<VirtualHost *:80>
        ProxyPreserveHost On
        ProxyRequests off
        <Proxy balancer://app>

                BalancerMember http://127.0.0.1:9000
                BalancerMember http://127.0.0.2:9000
                BalancerMember http://127.0.0.3:9000
                Order Deny,Allow
                Deny from none
                Allow from all

                ProxySet lbmethod=byrequests
        </Proxy>
        ProxyPass / balancer://app/
</VirtualHost>

To enable SSL, we will need to add the following code to the VirtualHost definition:

SSLEngine on
SSLCertificateFile /path/to/domain.com.crt
SSLCertificateKeyFile /path/to/domain.com.key

Note

This configuration has been tried on Apache/2.4.10 on July 31, 2014.

For more information on Apache HTTP's mod_proxy module, refer to http://httpd.apache.org/docs/2.2/mod/mod_proxy.html.

The nginx server

The nginx server is a high performance HTTP server and a reverse proxy as well. It is also an IMAP/POP3 proxy server. We can configure nginx to act as a load balancer using two modules—proxy and upstream. These two modules are part of the nginx core and are available by default.

The nginx configuration file, nginx.conf, is generally located at /etc/nginx. Let's update it to use nginx as a load balancer for our application step by step:

  1. First, we need to define an upstream module for our cluster of application servers. The syntax is as follows:
    upstream <group> {
    <loadBalancingMethod>;
    server <server1>;
    server <server2>;
    }

    The default load balancing method is round-robin. So, we need not specify it explicitly when we wish to use it. Now, for our application, the upstream module will be as follows:

    upstream app {
        server 127.0.0.1:9000;
        server 127.0.0.2:9000;
        server 127.0.0.3:9000;
    }
  2. Now, all that we need to do is proxy all the requests. To do this, we must update the server module's location module:
    server {
        listen       80 default_server;
        server_name  localhost;
        …
        location / {
            proxy_pass http://app;        
            proxy_set_header Host $host;
        }
    }

The nginx server also supports proxying WebSocket. To enable WebSocket connections, we need to add two headers to the location module. So, if our Play application uses WebSocket, we can define the location module as follows:

location / {
    proxy_pass http://app;
    proxy_set_header Host $host;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

To enable SSL, we need to add the following settings to the server definition:

    ssl_certificate     /path/to/domain.com.crt;
    ssl_certificate_key path/to/domain.com.key;

Note

This configuration has been tested on nginx/1.4.7.

Refer to the nginx documentation at http://nginx.org/en/docs/http/load_balancing.html#nginx_load_balancing_configuration for more details.

lighttpd

The lighttpd server is a lightweight web server designed and optimized for high performance environments. All the utilities that may be required are available as modules and can be included as per our requirements. We can set lighttpd as a frontend server for our Play application using the mod_proxy module. We need to make a few configuration changes to achieve this. They are as follows:

  1. Update the lighttpd.conf file (generally located at /etc/lighttpd/) to load additional modules.
  2. By default, loading modules is disabled. This can be enabled by uncommenting this line:
    include "modules.conf"
  3. Update modules.conf (located in the same directory as lighttpd.conf) to load the mod_proxy module.
  4. By default, only mod_access is enabled. Update server.modules to the following code:
    server.modules = (
        "mod_access",
        "mod_proxy"
    )
  5. Now, enable loading the settings for mod_proxy by uncommenting this line:
    include "conf.d/proxy.conf"
  6. Update the proxy.conf file (generally located at /etc/lighttpd/conf.d/) with the server proxy configuration. The q module has only three settings:
    • proxy.debug: This setting enables/disables the log level
    • proxy.balance: This setting is a load balancing algorithm (round-robin, hash, and fair)
    • proxy.server: This setting is where requests are sent

      The expected format of defining a proxy.server setting is as follows:

      ( <extension> =>
          ( [ <name> => ]
          ( "host" => <string> ,
              "port" => <integer> ),
          ( "host" => <string> ,
              "port" => <integer> )
        ),
        <extension> => ...
      )

    The terms in this code are explained as follows:

    • <extension>: This term is the file extension or prefix (if started with "/"); empty quotes, "", match all the requests
    • <name>:This term is the optional name that shows up in the generated statistics of mod_status
    • host: This term is used to specify the IP address of the proxy server
    • port: This term is used to set the TCP port on its corresponding host (the default value is 80)
  7. Update the proxy settings as required:
    server.modules += ( "mod_proxy" )
    proxy.balance = "round-robin"
    proxy.server = ( "" =>
        ( "app" =>
            (
                "host" => "127.0.0.1",
                "port" => 9000
            ),
            (
                "host" => "127.0.0.2",
                "port" => 9000
            ),
            (
                "host" => "127.0.0.3",
                "port" => 9000
           )
        )
    )

    Note

    This configuration has been tried on lighttpd/1.4.35 on March 12, 2014.

    For more information on the configuration settings of mod_proxy, refer to http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModProxy.

High Availability Proxy

High Availability Proxy (HAProxy) offers high availability, load balancing, and proxying for TCP and HTTP-based applications. We can set HAProxy as a load balancer by updating the haproxy.cfg configuration file (it is generally located at /etc/haproxy/).

Let's make the required configuration changes step by step:

  1. First, we need to define the backend cluster. The syntax for defining a backend is as follows:
    backend <name>
    balance    <load balance method>
    server <sname> <ip>:<port>
    server    <sname> <ip>:<port>
  2. So, the backend for our application will be as follows:
    backend app
        balance     roundrobin
        server  app1 127.0.0.1:9000
        server  app1 127.0.0.2:9000
        server  app1 127.0.0.3:9000
  3. Now, we just need to point requests to the backend cluster. We can do this by updating the frontend section:
    frontend  main *:80
    default_backend             app

No additional configuration is required for an application using WebSockets.

Note

This configuration has been tried on HAProxy version 1.5.9 2014/11/25.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.10.32