Node.js

Configuring Load Balancer with HAProxy and Node.js

Load Balancing is needed in order to distribute the load of work across multiple resources – in computer science terminology, these multiple resources can be pieces of software or hardware, where similar work processes are waiting to be executed for a processing request.

So, in a web application, we will need to perform load balancing to control the user requests coming to a server. The configuration of load balancing can be done in various ways (different physical architecture). In this article we simulate a load balancing architecture with haproxy server and node.js application servers.

In the picture below, a load balancing scenario is shown.

Load-Balancer

Now, in order to simulate this load balancing work, we have used a haproxy server and spawned 2 node.js servers on the same computer.

Some commands for the 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 the configuration of HAProxy , we need to edit the /etc/haproxy/haproxy.cfg file.

The main configuration in the HAProxy server for load balancing (haproxy.cfg) is as follows:

....
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 ports 3000 and 3001 and they are doing the same work. In the backend section, we have added two node.js servers in ports 3000 and 3001.

For the above configuration, we have the following 2 lines:

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

In the frontend, we have configured the backend as –

default_backend servers

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 access the browser at http://127.0.0.1, if everything goes well, We will have the following output:

Hello Node.js

Above, we can see the requests on ports 3000 and 3001 one after another, which is a round robin load balancing scheme in the HAProxy server.

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

Piyas De

Piyas is Sun Microsystems certified Enterprise Architect with 10+ years of professional IT experience in various areas such as Architecture Definition, Define Enterprise Application, Client-server/e-business solutions.Currently he is engaged in providing solutions for digital asset management in media companies.He is also founder and main author of "Technical Blogs (Blog about small technical Know hows)"
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button