Configuring Docker Swarm

John Tyree
3 min readJan 28, 2024

--

What is a Node?

A node is an instance of a Docker engine. There are worker nodes and manager nodes. These different nodes make up a Docker Swarm.

What is Docker Swarm?

Docker Swarm allows for management of clusters and scheduling for users who work with Docker. A main benefit of Docker Swarm is that it helps ensure high availability. If a service becomes unhealthy, Docker Swarm will automatically launch a new service to replace the unhealthy service.

Three EC2 instances need to be launched within the AWS console:

  • I will name the instances: “Node1”, “Node2”, “Node3” and will be selecting an Ubuntu 20.04 AMI
  • Each instance will need to be using the same VPC
  • All EC2 instances will need to be using the same keypair and keep the keypair file path handy.
  • A security group with inbound rules to allow HTTP, HTTPS, and SSH from the “My IP” option under Source. Add the VPC by copying the IPv4 CIDR from the VPC that will be used by each of the three instances (see screenshot). Each of the three instances will need to use this same security group.

Once the Instances are up and running, navigate to VScode to setup the config files

Using VScode, ssh into the three EC2 instances

Next, install Docker on all three instances using the command below:

sudo curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh

Check to see if Docker installed by running the following command:

sudo docker version

After Docker is installed on each node, change the hostname to match the EC2 instances. For example, mine will be classified as “Node1”, “Node2”, and “Node3”. Use the two commands below on all three instances to rename the hostname and reboot:

sudo hostnamectl set-hostname [NAME OF HOST]
sudo reboot

To initiate the swarm, run the following command below into Node1:

sudo docker swarm init --advertise-addr [PRIVATE IP ADDRESS OF NODE1]

After running the command to initiate the swarm, the output will give the command to run in Node2 and Node3 to add to the swarm from Node1. Node1 will act as the master node and Node2 and Node3 will act as the worker nodes

Run the command below in Node1 to see the status of each node in the swarm

sudo docker node ls

Each node is now active! A Docker Swarm has successfully been initiated with a manager node (Node1) and two worker nodes (Node2 & Node3)

Remember…

Leave the swarm by running the command below in each node

sudo docker swarm leave --force

Also remember to stop and terminate any running EC2 instances to avoid any extra costs.

--

--