Setting up a Docker Swarm Environment - A Breakdown

Hafsah Robleh
Women in Technology
6 min readAug 5, 2023
Picture credits: https://www.aneo.eu/lorchestration-docker-swarm/

Hey everyone! Today I’ll be walking you through how to set up a Docker Swarm environment. Docker Swarm can play a crucial role in various contexts. It helps manage and control a group of computers (or servers) to work together as a team to run and manage applications.

Objectives for this project include:

  • Installing docker on all hosts
  • Verifying that docker is in an active state
  • Changing the hostname on each node to be able to identify the master node and the two worker nodes
  • Validating the worker nodes all share the same security group as the master node
  • Adding the ssh key to each node
  • Running the necessary containers using the CLI
  • Creating the Swarm using one master node and two worker nodes

Prerequisites

To follow along in this project, You’ll need an AWS account, the free tier is fine. As well as an IDE, I’ll be using Visual Studio Code.

Let's begin!

Creating the Security Group

When creating the security group for the nodes, It's important to remember a few things.

  1. The VPC used must be the default VPC
  2. The inbound rules must allow SSH & HTTP from anywhere (IPV4), as well as All Traffic from the VPC.

This is done by getting the IPv4 CIDR block for the default VPC, and pasting it into the custom source for all traffic, like so,

This is how the finished inbound rules should look like.

Screenshot: by the author.

Creating the key pair

Next, ensure you have a key pair created. I already have my key pair downloaded, However, if you do not, now is the time to create one before we launch our launch template.

Go to the EC2 dashboard and in the left menu, there will be a keypair option. Create one, ensuring it ends in .pem and is the RSA type. Lastly, ensure you properly download it as you will not have access to it again.

Creating the Launch template

To create the 3 instances, I’ll be using a Launch template. Navigate to the EC2 dashboard.

After creating a name, I will be using the Ubuntu 20.04 AMI.

Screenshot: by the author.

For instance type, I chose t2 micro. Choose your key pair, and ensure you do not choose a subnet. For the firewall, choose the security group we created earlier. For security groups choose the one we created earlier.

Then go to the advanced network configuration and enable Auto-assign public IP. This will allow us to connect to our instance.

Screenshot: by the author.

Lastly, go to the advanced details, and put the following script in the user data

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

This script will automatically update all packages as well as install docker at launch time.

Success! The link above will lead us to our launch template page, in it click on the “Actions” on the top right.

Screenshot: by the author.

This will allow us to launch our three instances from this template. Change the number of instances from 1 to 3 and launch the instances.

All launched!

Now to change the names of the created instances, I’m going to be calling them Manager1, Node2 and Node3.

Connecting to the Instances

In my Visual Studio code terminal, I need to change my directory to where my key is stored which is the Downloads folder.

I then navigated to my EC2 dashboard to my manager instance and clicked on the “Connect”

and ran the example in my terminal.

I’m now connected to my first Ubuntu instance, and I can verify that our user data installed docker by checking the docker version.

Next, I’ll change this hostname to manager1 so I can differentiate between the three nodes.

sudo hostnamectl set-hostname manager1

and then reboot. This is necessary to see the hostname change.

sudo reboot

We’re in!

I’m going to quickly run sudo rebootagain, since the instance is telling me a restart is required. This closed the connection to the host and I had to reconnect, this time I did not get this error.

Note: When using the sudo reboot command and getting kicked out of the connection, I had some trouble reconnecting back. As well as getting a “connection refused error”. This was the same case for all three nodes and was fixed by retrying two or three times and waiting a bit, it eventually connects.

Helpful tip: If you lose the ability to type in commands entirely during this process, use control c to get back the terminal.

To open up the two parallel terminals I hovered over the plus and used the split terminal command,

Now I’ll be opening up two separate terminals and doing the same for each.

Your three terminals should now look something like this,

Note: I accidentally ran the hostname command incorrectly on my second node which is why it shows two manager nodes, I fix this in a bit.

Now I’m going to be changing the hostnames of the second two nodes to node2 and node3.

sudo hostnamectl set-hostname node2
sudo hostnamectl set-hostname node3

and Remember, to see the new name you need to use the command sudo reboot

Initializing the Swarm

Now that all the nodes are properly named, we can start the swarm.

On the manager node, use the following command

sudo docker swarm init

Now the first node is a manager. Below is the command we will be using for the other 2 nodes.

Now I’m going to be copying and pasting this token into the node2 and node3

All done! We just successfully created a swarm docker environment, complete with one manager and two workers. This can be a really helpful setup for distributing and managing applications across multiple nodes efficiently.

I hope this step-by-step breakdown was helpful. If you have any questions or comments, let me know! I do Devop project documentation breakdowns regularly so follow me for me more.

You can also find me on Linkedin for more Devop-related projects and news at https://www.linkedin.com/in/hafsahrobleh/ Feel free to connect with me and have a wonderful day!

--

--