Set up a Nginx Load balancer for a dockerized Node.js Application

Aagam Vadecha
Jun 14 · 4 min read

If you’re here then you probably know the advantages of using docker for your application to summarize them, docker containers speed up your deployments, make your application portable to any platform,images are lightweight, simplifies maintenance and are highly scalable.

The job of a load balancer is to efficiently distribute incoming traffic across a group of backend servers known as server pool. A load balancer acts as a “traffic cop” sitting in front of your backend servers, routing client requests across all servers capable of handling those requests in a manner that maximizes speed and ensures that no one server is overworked, which would degrade performance.

Prerequisites:
Docker Basics

Overview:
Here in this blog we will be seeing how to Dockerize a simple Node.js application and deploying 2 docker containers of the application on a Ubuntu server on different ports and then configure Nginx to run as a load balancer in front of both docker containers to increase performance and responsiveness. This is just a demo, general concepts are covered and the sample can be scaled to as many hundreds of containers across multiple servers(“nodes”)

Something like this :

We’ll be setting up 3 containers in total, 2 for Node.js and 1 for Nginx. The Node.js containers will be running on host ports 5001 and 5002 which will be mapped to container port 5000 respectively.

Alright, Let’s get started


Here is what our app.js looks like. It responds with a simple Hello “Name” which can be passed to it as an environment variable while creating containers from images just so that we could differentiate between the two containers and make sure our load balancer works properly.

If you’re not familiar with docker, I’d strongly recommend to check this out once and understand how docker works.
Lets create a Dockerfile to build an image for this app it’ll be pretty simple as follows

Next we need go to the directory where we have saved the app.js and the Dockerfile open the shell and then run a “docker build -t customimagename:tag .” to build an image from the docker file.

RUN: docker build -t nodeapp:001

Once the image is built you can see it using the docker images command

And then spin up containers from the image on different host ports using the — publish(-p) option and pass an environment variable “name” with the -e option to differentiate between two containers.

  • docker container run -p 5001:5000 --name helloworld -d nodeapp:001
  • docker container run -p 5002:5000 --name customized -e "name=aagam" -d nodeapp:001

Check the browser for your http://ip:port and verify that the containers are successfully deployed.

Here we can see one container is running on host port 5001 and other on host port 5002. Seems good !


We have successfully deployed two Node.js containers now we need to add a Nginx Load balancer in front of it. To set up a Nginx container we need to write a nginx configuration file and a separate Dockerfile, so move to a fresh folder and create two files named nginx.conf and Dockerfile which will look as follows:

So what did we just type in it? We set up a proxy in nginx.conf using upstream and added our two server addresses which needed to be load balanced and we set the load balancing algorithm to be least_connection instead of the default round robin.
To learn more about load balancing with nginx you can refer this

After setting up these files we can build an image from it using
docker build -t nginxbalancer:001 .

And then spin up a container on any host port we’ll be using port 5000

docker container run -p 5000:80 -d nginxbalancer:001

If things worked correctly for you which I hope did 😆, You should visit your IP:5000 on your browser and hit refresh multiple to see both outputs on the same address meaning that nginx is redirecting our http request to both containers and balancing the load overall.

Good Job !
We just set up a Nginx Load balancer for a dockerized Node application using 2 containers, As previously said the concept can be scaled according to needs.

Thanks for reading! If you liked what you read, then leave a 👏 and follow.

Faun

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts

Aagam Vadecha

Written by

Faun

Faun

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts