Load Balancing in Docker
Load Balancing in Docker

In previous post I explained a little bit about different methods of load balancing. I promised to cover Direct route method in another post. Here it is.

Demo time

For the demo I’m going to use a very powerful tool in Linux kernel, LVS. This module provides different methods for load balance traffics. Including NAT, Tunneling and Direct Route. Here I’m going to implement Direct Route method within Docker containers.

Prerequisites

I’m using Ubuntu, other distros should work too. But commands and installations might but a little bit different.

I assume that you have decent knowledge of Linux, Docker and Linux Networking. Otherwise it might be a little bit difficult for you to understand the concept.


  1. This setup require Docker on your system. If you are using Ubuntu 14.04 or later version, you can install latest Docker daemon with the following command:
1
2
$ curl http://get.docker.com | sh
$ sudo usermod -aG docker $USER

Now re-login to your system.

  1. We need IPVS kernel module on your Docker Host machine. In order to install that in Ubuntu issue the following command:
1
$ sudo apt-get install ipvsadm
  1. To make sure that every thing is fine try the following commands:
1
2
$ docker run hello-world
$ sudo ipvsadm -l
  1. Get the following source code from GIT.
1
$ git clone https://github.com/boynux/docker-load-balancer

There shouldn’t be any error from execution of above commands.

Building images

For this demo, I’m going to create 3 Docker images with Nginx installed. They just show a simple HTML page with their hostname in it. This way we can see how load balancing happens and every time you refresh pages it’ll show different hostname.

Go to the source code directory you just pulled from github.

  1. Create Nginx Docker image:
1
2
3
$ cd docker-load-balancer
$ cd nginx
$ docker build -t boynux:nginx .
  1. Create IPVS image:
1
2
3
$ cd docker-load-balancer
$ cd ipvs
$ docker build -t boynux:ipvs .

Running the demo

Now to run the whole setup issue the following command: Please note that the following IP (virtual IP) could be any IP. This is entry point to load balancer. Just make sure it’s not within your computer or Docker subnet.

$ cd docker-load-balancer
$ ./run 172.20.10.1

Now if you try to open that IP (virtual IP) in your browser or with curl. If you refresh your system the hostname will change.

What to know more about Docker networking? Advanced Docker networking

To stop the demo just press ctrl-c as instructed. It’ll clean-up everything. For more details you can see code. It’s very simple and easy to understand.

Any questions? Please leave comments.



comments powered by Disqus

ABOUT BOYNUX bLOG

My notes and experiments with different technologies and tools. All information here is solely my personal thougths and does not relate to my employer's point of view in anyways.


FOLLOW BOYNUX FOR UPDATES AND BLOGS


FORK ME!

© Copyright 2012-2017