Can You Efficiently Set Up a Docker Swarm Using VS Code’s Remote SSH Terminal?

Tangela Sampson
6 min readNov 30, 2023

--

What is Docker Swarm?

Docker Swarm is an orchestration management tool that runs on Docker applications. It helps end-users in creating and deploying a cluster of Docker nodes. Each node of a Docker Swarm is a Docker daemon, and all Docker daemons interact using the Docker API.

What are nodes in Swarm ?

In Docker Swarm, nodes are the individual instances (machines) that collectively form a cluster, allowing you to deploy and manage containerized applications. There are two primary types of nodes in a Docker Swarm:

Manager Node: Manager nodes oversee the administration of the swarm and coordinate the deployment of services.

Worker Nodes: Worker nodes are responsible for executing the tasks (containers) assigned by the manager nodes.

Prerequisites:

  • Launch three EC2 instances with Ubuntu 20.04 (one for the master node and two for the worker nodes).
  • Ensure that you have an AWS account and the necessary credentials.
  • VSCode environment .

Foundational:

Install Docker on All Hosts

Connect to each AWS EC2 instance using SSH in your VSCode terminal

Begin by logging into the AWS Management Console with your credentials. Navigate to the EC2 Dashboard and initiate the instance creation process by clicking on “Launch Instance.” Select “Ubuntu Server 20.04 LTS” as the Amazon Machine Image (AMI) and choose an appropriate instance type. Configure instance details, specifying the number of instances (set to 3) and additional settings if needed, such as IAM roles or user data. Adjust storage configurations as required and add optional tags for organizational purposes. Ensure the security group allows SSH access by either creating a new one or using an existing group.

Review your configuration and launch the instances, specifying or creating an SSH key pair for secure access. Deploy a total of 3 instances ( 1 MasterNode and 2 WorkerNode).

When setting up security groups for Docker Swarm on AWS EC2 instances, it’s essential to configure rules that allow communication between nodes and also enable external access, if needed. Here are the important ports to consider in the security group configuration: ( Validate the worker nodes all share the same security group as the master).

Inbound Traffic

  • SSH (Port 22): from your IP address
  • Allow port 7946/udp for Docker Swarm communication among nodes.
  • Allow Swarm management traffic (Port 2377/tcp) from the Swarm nodes’ IP addresses.
  • Allow additional ports for your application or services (if applicable).

Outbound Traffic:

  • Allow all outbound traffic or restrict based on your application’s requirements

Connect the MasterNode through VS Code . Example of config file we used below: (Save File )

.VS Code Config

Remote to MasterNode “Remote SSH plugin in VS Code” — connect to Host

Installing docker on all hosts

This example downloads the script from https://get.docker.com/ and runs it to install the latest stable release of Docker on Linux:


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

Now lets verify Docker is Installed

Verify it was installed

Change the hostname on each node to identify the master node and the two worker nodes

This ensures each node has a unique identifier within the Swarm.

sudo hostnamectl set-hostname <my master node name>

Initilize the Docker Swarm on the current machine and sets it up as a manager. Executing the command to initiate a swarm activates the Docker Engine in swarm mode. Use “docker swam init”to establish a single-node swarm on the current node. This process involves transitioning the current node into swarm node. Proceed to WorkerNode and complete the same process.

docker swarm init

Create a new file called “config” with the following information provided

config file

If you are uncertain about locating the correct path to your .pem file, you can make use of the following command.

readlink -f dockerSwam.pem

Executing the command to initiate a swarm activates the Docker Engine in swarm mode. Use “docker Swarm init “to establish a single-node swarm on the current node. This process involves transitioning the current node into swarm node.

docker swarm init

To add a worker node to the Swarm, utilize the “docker swarm join” command. This command is displayed in the output when you initialize the Swarm on the manager node and resembles the following.

docker swarm join --token <SWARM-TOKEN> <MANAGER-IP-ADDRESS>:<MANAGER-PORT>

Then, verify the node status:

docker node ls

Verifying the status of all nodes once complete :

Now that we have successfully complete this project . let’s leave the swarm.

Wait! Actually before removing docker swarm I have a surprise for you 👇🏼

Advanced Section :

As the lead engineer, you’ve reviewed the Swarm configuration and you are now Advanced: ready to deploy your services on the Swarm. Consider the following steps as you deploy the service:

Using only the CLI, ssh into the master node host and run the command to create your service using an Official image of your choice out of the following company preferred images:

1. nginx

2. apache

3. redis

4. python/alpine

5. ubuntu

· Initially only launch 1 replica.

· Run the commands to verify the service has been created and is running

· Verify that the service has scaled

Let’s Begin

Here is great documentation to help you follow along :

Clone the Repository

git clone https://github.com/dockersamples/docker-swarm-visualizer

docker -compose : This command is used for defining and running multi-container Docker applications

cd docker-swarm-visualizer
docker-compose up -d

This advanced utility offers a visual depiction of your Swarm Cluster setup. It visually represents running containers on each node, making it an excellent tool for illustrating container distribution in workshops or presentations about Docker.

Now we can finish removing the last swarm. Execute this command on the manager node to forcefully remove it from the Swarm, initiating the departure process and effectively disbanding the Swarm. It’s important to note that this command must be executed on every node you intend to remove from the Swarm. After running it on the manager node, other nodes can follow suit, leaving the Swarm.

docker swarm leave --force

Thank you for being a part of this journey with me. I hope this was helpful!

Let’s connect Linkedln !

--

--

Tangela Sampson

Cloud Security Engineer ☁️ | Cybersecurity 🔐 | Linux | Python🐍