Configuring Load Balancer with Haproxy and Node.js

Load Balancing is needed to distribute load of work across multiple resources – in computer science terminology, these multiple resources can be pieces of software or hardware, where same work processes are waiting to be executed on processing request. So in web application, we will need to do load balance to control user requests to server. Complexity of load balance can be done in various formats (physical architecture). In this article we had simulated a load balancing architecture with haproxy server and node.js application servers.

We expect knowledge of reader in web technology at intermediate level.

Below is a picture, where load balancing scenario is drawn.

Load Balancer

Now to simulate this load balancing work, we had taken one haproxy server and spawned 2 node.js server on same computer.

Some commands for haproxy –

1> To install : sudo apt-get install haproxy

2> To start : sudo service haproxy start

3> To stop : sudo service haproxy stop

4> To restart : sudo service haproxy restart

5> To reload configuration : sudo service haproxy reload

To edit configurations of haproxy, we need to edit the /etc/haproxy/haproxy.cfg file.

 

First, the main configurations in the haproxy server for load balancing (haproxy.cfg)-

....
    frontend localnodes
      bind *:80
      mode http
      default_backend servers

    backend servers
      mode http
      balance roundrobin
      option forwardfor
      http-request set-header X-Forwarded-Port %[dst_port]
      http-request add-header X-Forwarded-Proto https if { ssl_fc }
      option httpchk HEAD / HTTP/1.1\r\nHost:localhost
      server server1 127.0.0.1:3000 check
      server server2 127.0.0.1:3001 check

Here we have two node.js server processes in port 3000 and 3001 up and they are doing the same work. In the backend section, we have added two node.js server in port 3000 and 3001.

For the above configuration, the 2 lines are –

server server1 127.0.0.1:3000 check
server server2 127.0.0.1:3001 check

The scheme for load balancing is written in the following line –

balance roundrobin

Also in frontend part we had configured the backend as –

default_backend servers

Which is mentioned in backend section.

Now the node.js server set up –


var http = require('http');
var morgan       = require('morgan');

var server1 = http.createServer(function (req, res) {
  console.log("Request for:  " + req.url + "-- port 3000 ");
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello Node.js\n');
}).listen(3000, "127.0.0.1");

var server2 = http.createServer(function (req, res) {
  console.log("Request for:  " + req.url + "-- port 3001 ");
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello Node.js\n');
}).listen(3001, "127.0.0.1");

server1.once('listening', function() {
  console.log('Server running at http://127.0.0.1:3000/');
});

server2.once('listening', function() {
  console.log('Server running at http://127.0.0.1:3001/');
});

When we hit on the browser in http://127.0.0.1, if everything goes well,
We will have following output in browser –


  Hello Node.js

In above, we are viewing request on 3000 and 3001 port one after another, which is round robin load balancing scheme in haproxy server.

We will discuss different schemes of haproxy load balancing in our next article.

If you find this article helpful, you can connect us in Google+ and Twitter for other updates.

Leave a Reply

Your email address will not be published. Required fields are marked *