Load Balancing Node.js Applications With NGINX and Docker

Load Balancing Node.js Applications With NGINX and Docker

http://ift.tt/2qM12Eu

In this article, we will see how easy it is to load balance Docker-ized Node.js applications with NGINX. We will create a simple Node.js application that serves an HTML file, containerize it with Docker, and containerize an NGINX instance that uses a round-robin algorithm to load balance between two running instances of this application.

Docker and Containers

Docker is a software container platform. Developers use Docker to eliminate “works on my machine” problem when collaborating with co-workers. This is done by putting pieces of a software architecture on containers (a.k.a. Docker-ize or containerize).

Using containers, everything required to make a piece of software run is packaged into isolated containers. Unlike Virtual Machines (VMs), containers do not bundle a full operating system—only libraries and settings required to make the software work are needed. This makes them efficient, lightweight, self-contained and guarantees that software will always run on the same configuration, regardless of where it’s deployed.

To learn more about Docker, take a look at this webinar.

Installing Docker

Everything that we will need to test this architecture is Docker. As the instances of our Node.js application and NGINX will run inside Docker containers, we won’t need to install them on our development machine. To install Docker, simply follow the instructions on their website.

Creating the Node.js Application

To show NGINX load balancing in action, we are going to create a simple Node.js application that serves a static HTML file. After that, we are going to containerize this application and run it twice. Lastly, we will configure a Docker-ized NGINX instance to dispatch requests to both instances of our application.

In the end, we will be able to reach http://localhost:8080 on our local machine to “randomly” get results from one or another instance. In fact, the result won’t be randomly decided, we will configure NGINX to use a round-robin algorithm to decide which instance will respond on each request.

But let’s tackle one step at a time. To create this application we will first create a directory for the application, and then create an  ndex.js file that will respond to HTTP requests.

To create the directory, let’s issue the following command: mkdir application. After that, let’s create the index.js file in this directory and paste the following source code:

Everything that this Node.js script does is to answer HTTP requests to http://localhost:8080 with and HTML tag that contains a message defined in the MESSAGE environment variable. To better understand how this works, we can run the following commands:

And then open http://localhost:8080 on a web browser. See? We got simple web page with the “Howdy!” message. Before proceeding, let’s stop our application by hitting Ctrl + C.

Dockerizing the Node.js Applications

To Docker-ize our Node.js application, we will need to create a file called Dockerfile in the application directory. The content of this file will be:

Note: If you don’t understand how a Dockerfile works, check out this reference.

After that we need to create an image, from this Dockerfile, which can be done through the following command:

And then we can run both instances of our application with the following commands:

After running both commands, we will be able to open both instances on a web browser by going to http://localhost:8081 and http://localhost:8082. The first URL will show a message saying “First instance” and the second URL will show a message saying “Second instance.”

Load Balancing With a Dockerized NGINX Instance

Now that we have both instances of our application running on different Docker containers and responding on different ports on our host machine, let’s configure an instance of NGINX to load balance requests between them. First we will start by creating a new directory.

In this directory, we will create a file called  \nginx.conf with the following code:

This file will be used to configure NGINX. On it, we define an upstream group of servers containing both URLs that respond for the instances of our application. By not defining any particular algorithm to load balance requests, we are using round-robin, which is the default on NGINX. There are several other options to load balance requests with NGINX, for example the least number of active connections, or the least average response time.

After that, we define a server property that configures NGINX to pass HTTP requests to http://my-app, which is handled by the upstream defined before. Also, note that we hard coded 172.17.0.1 as the gateway IP; this is the default gateway when using Docker. If needed, you can change it to meet your local configuration.

Now we will create the Dockerfile that will be used to Docker-ize NGINX with this configuration. This file will contain the following code:

Having created both files, we can now build and run NGINX containerized on Docker. We achieve that by running the following commands:

After issuing these commands, let’s open a web browser and access http://localhost:8080. If everything went well, we will see a web page with one of the two messages: First instance or Second instance. If we hit reload on our web browser a few times, we will realize that from time to time the message displayed switches between First instance or Second instance. This is the round-robin load balancing algorithm in action.

Note: To use the other algorithms available on NGINX, check their documentation for more information.

Aside: Securing Node.js Applications with Auth0

One of the most complex features to implement in an application is user authentication and identity management. Security for authentication and identity is an entire glossary unto itself.

Auth0 hosted login screen

If you need to implement a robust, highly customizable identity and access management system quickly and easily for your Node.js application, Auth0 can help. Take a look at Auth0 Node.js SDK Quickstart to properly secure your application.

Conclusion

Loading balancing applications with Docker and NGINX is an easy process. In this article, we have managed to achieve this goal with a few simple steps. All we had to do was to install Docker on our development machine, run two instances of Docker-ized applications and then configure a Docker-ized NGINX instance to round-robin requests to the application instances.

java

via DZone.com Feed https://dzone.com

May 23, 2017 at 07:27PM

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s