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.
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.
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 2 $ curl http://get.docker.com | sh $ sudo usermod -aG docker $USER
Now re-login to your system.
1 $ sudo apt-get install ipvsadm
1 2 $ docker run hello-world $ sudo ipvsadm -l
1 $ git clone https://github.com/boynux/docker-load-balancer
There shouldn’t be any error from execution of above commands.
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 2 3 $ cd docker-load-balancer $ cd nginx $ docker build -t boynux:nginx .
1 2 3 $ cd docker-load-balancer $ cd ipvs $ docker build -t boynux:ipvs .
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.
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