Creating A Docker Swarm in AWS That Has Manager and Worker Nodes

Sharon Johnson
5 min readMay 7, 2023

--

Let’s get started with the what is:

What is Docker Swarm? A group of either physical or virtual machines that are running the Docker application and that have been configured to join together in a cluster. The activities of the cluster are controlled by a swarm manager, and machines that have joined the cluster are referred to as nodes.

What are Nodes?

Manager nodes handle cluster management tasks:

Worker nodes are also instances of Docker Engine whose sole purpose is to execute containers. Worker nodes don’t participate in the Raft distributed state, make scheduling decisions, or serve the swarm mode HTTP API.

You can create a swarm of one manager node, but you cannot have a worker node without at least one manager node.

Task:

Using AWS, create a Docker Swarm that consists of one manager and three worker nodes.

Verify the cluster is working by deploying the following tiered architecture:

  • a service based on the Redis docker image with 4 replicas
  • a service based on the Apache docker image with 10 replicas
  • a service based on the Postgres docker image with 1 replica

Let’s get started!

Step 1: Create a Docker Swarm that consists of one manager and three worker nodes.

Open up your AWS console and navigate to the EC2 and lets launch 4 instances one for the manager and one for each worker nodes.

I will be creating the 4 instances using Amazon Linux 2(if you look to the right of the page under summary you and select to create all 4 instances at one time), select your instance type as t2 micro, your key pair you can select an existing or create a new one.

In the Network setting we will need to make sure that your security groups are created with the proper settings. You can choose to click create security groups from here.

From here make sure your security group have the following settings, security groups acts as a virtual firewall for your EC2 instances to control incoming and outgoing traffic. Both inbound and outbound rules control the flow of traffic to and traffic from your instance. So from here click on create security group and edit the inbound rules

TCP port

The following ports must be available. On some systems, these ports are open by default.

  • Port 2377 TCP for communication with and between manager nodes
  • Port 7946 TCP/UDP for overlay network node discovery
  • Port 4789 UDP (configurable) for overlay network traffic.
  • Also make sure to add SSH with port 22.

Next let proceed to the Advance detail and add in user data to install docker once we launch the instance.

Let’s launch our instances.

All instance has been created so lets name each instance one we will name Manger Node and the other three are for the worker nodes.

Step 2: Setting up Manager and Worker Node to create our cluster.

So let configure the manager node first, click on the manager node and click on connect, I will SSH into the manger node instance through the AWS CLI (cloud shell) to setup the swarm mode.

Now that you are SSH into the manager node, let join swarm using the following command

docker swarm init --advertise-addr <ip> (use the private IP address)

You should get the following return with the manager token that can be attached to the three workers nodes.

So from here you will need to go back to the instance for each worker and SSH into the three worker nodes to have them join into the manager node.

Let’s use the following command to get a list of nodes

docker node ls

Ok we have all 4 nodes, one is a leader(manager) and the others are the three workers.

Step 3: Adding a service based on the Redis docker image with 4 replicas

docker service create --name redis --replicas 4 redis

Step 4: Adding a service based on the Apache docker image with 10 replicas

docker service create --name apache --replicas 10 httpd

Step 5: Adding a service based on the Postgres docker image with 1 replica

docker service create --name postgres --replicas 1 -e POSTGRES_PASSWORD=mypassword postgres

Now that all of the services are added we can now run the following command to list

docker service ls

Project has been completed.

--

--

No responses yet