~Setting up a Docker Swarm Environment~

Asonti Ginn
7 min readJul 12, 2023

Today's Goals

~ Create 3 Ubuntu instances in AWS with docker installed.

~ Use Visual Studio Code to SSH into our instances.

~ Create our Swarm and add our Workers.

Key Terms

~ Docker: With docker you can build, update, and run containers.

~ Container: A container is made from an image, and it packages up code so it can run quickly in other environments.

~ VS Code: A code editor and it offers many extensions to make VS Code run like an IDE.

~ Ubuntu: Ubuntu is a free Open-Source Linux distribution.

~ Swarm: This is a service provided by docker that helps with container orchestration.

~ Node: An instance inside a docker swarm.

~EC2 Instance: Stands for Elastic Compute Cloud which is a virtual server inside the AWS Cloud.

~ Sudo: Allows you to run commands as if you are the root user in a terminal.

Prerequisites

~ An AWS Account and basic AWS knowledge.

~ Visual Studio Code downloaded.

~ Basic Command Line knowledge.

Instance Set Up

Begin by logging into AWS, go to your EC2 console, and launch a new set of instances.

Since we are making a multi node environment we need to launch multiple instances. For this project launch 3 and add a common name.

Change your default amazon Linux image to an Ubuntu image. We want to keep our project as free as possible so make sure your instance type is t2.micro.

If you have a key pair on your computer you would like to use, you can use a previously made one, if not, generate a new key pair.

We need to edit our Network settings. You can keep your instances inside your default VPC be sure to check that your instance will automatically assign a public IP.

We are going to create a new security group to be sure we have all the correct settings we need.

Our security group needs 3 rules. We need to allow our computer and VPC access to this environment.

Add the following rules:

~ SSH from anywhere.

~ All traffic from your IP.

~ All traffic from your VPC CIDR block. You can find this under the name of your VPC. You will have to type this CIDR block in the source box.

The last thing we need to add is User Data. At the bottom of your screen open the advanced details and locate the user data field.

In the user data field add the following information. I will break down the sections for better understanding.

~ #!/bin/bash: The start of a script, it lets the computer know its executable and to run this information with a bash interpreter.

~ apt-get install update -y: This updates anything inside the instance so we don’t have to.

~ curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh: These commands will install docker inside each instance so we can create our swarm.

#!/bin/bash
apt-get install update -y
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh

Once you are done adding your user data you can launch your instances.

To help with telling the difference between nodes, I suggest you add numbers after the names of your instances.

Instance Connection

Now it is time to connect to our instances to get them working together.

Open your VS Code and select the terminal.

When you open the terminal, you need to see what directory you are in. Your key pair automatically goes to the downloads folder so if you are not there already cd to that directory.

cd <insertyourpath>

Add 2 additional terminals attached to this terminal, those will be the additional worker nodes later.

Return back to your AWS Console and select your first node. Click connect and select SSH Client. Copy the Example at the bottom of the ssh client.

In your VS code terminal paste the example into your first terminal. You will be asked for a fingerprint, type yes and you should now be ssh into your instance.

We have successfully ssh into our first instance. Repeat the same process for both your remaining instances.

Once you have ssh into all your instances your screen should look similar to this. I did clear my screen to make the picture more visually appealing.

Inside each terminal run the following command to be sure docker is installed properly. It will tell you what version of docker you have installed as well as additional docker information.

sudo docker version

SWARMING TOGETHER

Now we need to create our swarm, add our manager, and workers to the swarm. But before we can do that lets rename our terminals so we can know the difference between each.

I used the names: manager, node1, and node 2. You can name your nodes anything you would like.

sudo hostnamectl set-hostname <INSERTNAME>

After running this command in each terminal, we need to reboot the terminals so the name can appear. Be sure to run this command in each terminal not just the first one or only the first name will change.

sudo reboot

Now we need to connect back, to make it simple click on the terminal and hit the up arrow until the ssh command returns back on the screen and run it.

You should see that all your terminals have been named with the proper name.

We finally get to set up our swarm. Return back to AWS and go to your manager instance, in my case this is ‘NODE1’. Retrieve the Private IP address and copy it to your clipboard.

In your manager terminal paste the following command:

sudo docker swarm init --advertise-addr <PASTEYOURIP>

This command makes this terminal the manager of docker swarm. It will push out a command that we will use in the nodes to add them to the swarm and establish them as workers.

Copy your swarm token.

In the nodes type sudo then paste the token and it should now make the nodes workers.

sudo <COPIEDTOKEN>

Now let's check to make sure the manager can see the nodes available.

sudo docker node ls

All your nodes are now set up and ready to go!

For more, easy to follow articles, follow me on Medium!

--

--

Asonti Ginn

Hey! I am taking you on my Cloud engineering journey with easy to follow, how-to articles. All provided with an under 10-minute read and PLENTY of pictures!☁️