DEV Community

Cover image for Simple Round-Robin Network with Docker
Joey Ohannesian
Joey Ohannesian

Posted on

Simple Round-Robin Network with Docker

☁️Cloud Fundamentals Helping with Local Networking?

After studying and passing the AWS Solutions Architect & Developer Associate exams, seeing that Docker creates what amounts to a VPC locally really surprised me. My cloud and networking fundamentals are helping me better understand how a program running locally works? Well, when you go back to the basics and remember that a VPC is essentially a fancy AWS-account contained (at least by default) LAN, then it all makes sense.

🎯Learning Target

The objective for this small project is exactly what the title says, create a simple round-robin network with Docker. Success means pinging the same alias and not getting the same container back every time.

👶Explain Like I'm 5

Every time you connect to google.com, you're not loading the same server. Google has some fancy routing, but when it's broken down all the requests are distributed to different servers. This is called load balancing. Round-robin load balancing is a specific type of load balancing. Of course, Google's implementation of load balancing is leaps and bounds more efficient than what I'm doing here. Consider this a "poor man's load balancer."

⏮️Prerequisites

📌 Docker Engine + CLI installed

⭐Let's Begin

Docker creates a virtual network automatically. This network is composed of three networks: bridge, host, and none. We want all of our traffic to be isolated from the default network to show that our round-robin network is doing what it should be doing.

The default Docker network

Step 1

Create a virtual network in Docker. We want to isolate our containers-to-be away from the default Docker "bridge" network.

docker network create rr-net

Default Docker network + created net

Step 2

Create two elasticsearch v2 containers and give them the same alias. This way, when we start to lookup the alias, we will see if the network traffic is routed correctly. We use elasticsearch v2 for a few reasons. It's not very large, opens on the same port, and it responds as JSON with the data we need when hit with a cURL request.

docker container run -d --network rr-net --network-alias search elasticsearch:2

Successful elasticsearch v2 creation

Step 3

Create a CentOS 7 container with an interactive bash shell connected to the round-robin network.

docker container create --network rr-net -it centos bash

Step 4

Run the curl search a few times and see if the "name" field changes.

curl -s search:9200

Success!

✅Wrap Up

In this lesson, I showed how to create a simple round-robin network using Docker's virtual networks. Docker is proving to be an invaluable tool and I am looking forward to seeing how I can use it in future write-ups.

Top comments (1)

Collapse
 
victorhazbun profile image
Victor Hazbun

This is from Docker Mastery course (Udemy), recommended.