Simulate DNS Round Robin with Containers

WiLL
2 min readNov 27, 2022

--

First of all, we need to know what is DNS Round Robin. Round-Robin DNS is basically a load-balancing technique. Here if you want to find out more details about Round-Robin DNS.

To simplify things, we will utilise Docker for this demo purpose. I will skip the part on how to use Docker since this article is about demonstrating DNS Round Robin.

Create 2 webserver containers using same network alias

We must first create docker network for these 2 webservers (let’s call it “testnet”).

docker network create testnet

Then, we create 2 webservers using same network alias “testserver”

docker container -d --network testnet --network-alias testserver nginx

Double-check if we have 2 webservers running. As shown below, we have 2 containers running nginx webserver: 21adbad825f1 & c4a35d3735aa

docker container ls

21adbad825f1 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp vigilant_hopper
c4a35d3735aa nginx "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp cranky_varahamihira

Customise each webserver to have unique response to identify from which the origin of the response

We can modify the response of the webservers. In this demo, I will add path “/test”. When request hits “/test”, the webserver will return response with the webserver container id, such as “Hello from [container-id] !!!!!”. Here is what I add in the default nginx config file.

    location / {
root /usr/share/nginx/html;
index index.html index.htm;

try_files $uri /test; # added line HERE

}



location = /test { # added line HERE

return 200 "Hello from $hostname !!!!!"; # added line HERE
} # added line HERE

FYI, if you use editor nano to modify your config file, you need to install nano on the containers as well.

 docker container exec -it 21adbad825f1 nano /etc/nginx/conf.d/default.conf 

Reload the webservers after modifying the config files and we are done with the webservers!

Simulate multiple requests to the webservers

To simplify the simulation of multiple requests, here I use CENTOS container on the same network as the webservers to generate multiple requests using bash script.

 docker container run --rm --network testnet centos /bin/sh -c 'while sleep 1; do curl -s testserver/test | awk "{print $1}"; done' 

Here is the result that we can see the requests are distributed among these 2 webservers.

Clean-ups

As good practice to free up unused resources, let’s clean up. Remove all the containers for testing, docker network, etc.

 docker container rm -f 21adbad825f1 c4a35d3735aa 
 docker network prune 

--

--

WiLL

Software Engineer • Tech Enthusiast • Digital Marketing • Part-time Doodler