Load Balancing containers

tirthaguha

Tirtha Guha

Posted on August 6, 2020

Load Balancing containers

In me previous post, I demonstrated a simple scenario of how we can run express based web applications in docker containers.

Often, for performance or resiliency, we run more than one instances of Web Applications. When you containerised your Web Application, you should be able to distribute the traffic evenly/weighted between multiple containers too.

In this exercise, I'm going to demonstrate how we can load balance between two containers.

What we need

  1. A system with docker installed in it.
  2. Complete the exercise in my previous post. If your system has docker, it won't take more that 15 mins.
  3. Basic knowledge of nginx

Lets get started

Quick recap: I have a repo in github with a basic express application. we're going to run this in a docker container.

Clone, Build and Run Container

$ git clone https://github.com/tirthaguha/card-deck-test.git
$ cd card-deck-test
$ touch Dockerfile
Enter fullscreen mode Exit fullscreen mode

Open Dockerfile in a editor and paste the following

FROM node:12-slim

WORKDIR /app
COPY ./package*.json ./
RUN npm ci --only-production
COPY ./ ./

EXPOSE 4000
CMD [ "node", "./bin/www" ]
Enter fullscreen mode Exit fullscreen mode

Build the Image

docker build -t card-deck-test .
Enter fullscreen mode Exit fullscreen mode

Run Multiple Instances of the same container image

docker run -p 4000:4000 -d card-deck-test
docker run -p 4001:4000 -d card-deck-test
Enter fullscreen mode Exit fullscreen mode

Open the following URLs in your browser to check if they are running fine on their individual ports
http://localhost:4000/card-deck/shuffle
http://localhost:4001/card-deck/shuffle

Load Balance them

Enter nginx. nginx is a popular simple open-sourced webserver, often used as a loadbalancer and reverseproxy.

Create the nginx.conf

In a different directory, outside the card-deck-test, create a directory

$ mkdir nginx-docker
$ cd nginx-docker
$ touch nginx.conf
$ touch Dockerfile
Enter fullscreen mode Exit fullscreen mode

Open nginx.conf in your editor and enter the following code

upstream card-app {
    server 172.17.0.1:4000 weight=1;
    server 172.17.0.1:4001 weight=1;
}

server {
    location / {
        proxy_pass http://card-app;
    }
}
Enter fullscreen mode Exit fullscreen mode

Here, you're redirecting web traffic between 2 docker containers running on 172.17.0.1:4000 and 172.17.0.1:4001 respectively. 172.17.0.1 is default docker gateway IP address. You're giving them the weight=1 for both, so traffic is going to be evenly routed, in a round-robin fashion.

Create the Dockerfile for nginx

Open the Dockerfile in the editor and lets put this code in it

FROM nginx

# remove default nginx conf file
RUN rm /etc/nginx/conf.d/default.conf

# Copy the nginx.conf file as default conf
COPY nginx.conf /etc/nginx/conf.d/default.conf
Enter fullscreen mode Exit fullscreen mode

Build and run the nginx docker container

Build the nginx image

$ docker build -t nginx-load-balancer .
Enter fullscreen mode Exit fullscreen mode

Run the nginx image

docker run -p 8080:80 -d nginx-load-balancer
Enter fullscreen mode Exit fullscreen mode

You now can run the nginx URL on the browser
http://localhost:8080/card-deck/shuffle

Happy Load Balancing!

💖 💪 🙅 🚩
tirthaguha
Tirtha Guha

Posted on August 6, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Load Balancing containers
express Load Balancing containers

August 6, 2020