DOCKER SWARM

Lesekr
7 min readSep 15, 2023

--

Scenario: Your company is new to using containers and has been having issues with containerized workloads failing for their global shipping application. The company is worried about additional potential data loss as they have no way to reschedule containers that are failing. The company has one week to update their infrastructure and leverage the use of Docker Swarm so that data can be restored for the containers that are no longer in a healthy state. Because they are not familiar with Docker Swarm, they will need a step-by-step guide on setting up a Swarm for their containerized workloads to assist with container orchestration.

You are exclusively in charge of setting up Docker Swarm and giving your team a step-by-step setup manual. You should assume that there are new team members who may not have Docker installed on their devices. Your swarm creation should include the following steps via your remote ssh terminal in VS Code:

  • Installing docker on all hosts
  • Verifying that Docker is in an active state.
  • Change the hostname on each node to identify the master node and the two worker nodes.
  • Validating that the worker nodes all share the same security group as the master node.
  • Be sure to add your SSH key to each node.
  • making sure to run the necessary containers using the CLI.
  • Creating the Swarm using one master node and two worker nodes.
  • Show your team members the status of the Docker nodes.

Let the good times roll!

Getting started by creating a Docker Hosts

Activate your Amazon Web Server Console account. Look for EC2 on the home page after arriving there. Click on instance on the right-hand side of the EC2 main page.

  • Name the instance.
  • Application and OS Images (Amazon Machine Image):
  • Instance type: t2.micro
  • Key pair (login):
  • Network settings:
  • Advance Details: Enter the command below under the User Data section.
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh
  • Summary: There should be total of 3

Then launch the instance.

Edit the instance name. Click on the edit pencil to edit the name.

After changing the name. It’s time to open the terminal so that you can ssh into the first node. Use the hyperlink of the first node to go into the instance. Once on the instance page. To establish a connection with the instance, click the SSH client. An example is shown below.

The ssh should be copied from the sample and pasted into the terminal. Make sure to cd into Download.

After entering yes, the result should appear as below.

It changed to Ubuntu with the node IP address.

Verifying that Docker is in an active state.

Let’s copy the first node’s public IP to SSH Docker, as we have already set up the Docker environment. Currently in the SSH Client tab, click on EC2 instance connect to copy the public IP. Have the VS Code open and paste the public IP next to the host name.

    #IPV4 Address will change every time
HostName 54.167.148.25
User ubuntu
IdentityFile C:\Users\lesek\Downloads\Week16keypair.pem

After pasting the IP address and the pathway of the keypair, save. Click the double arrow labeled “Open a Remote Window” that is located on the bottom left corner of the screen.

The “Open a Remote Window” button will trigger the appearance of this window.

Connect to Host = Remote-SSH

Then a new screen will show up. Toggle to Docker. It will then ask you to go forward.

Click “next.” This is what the terminal will look like after establishing the request. Next, execute the command. cd .ssh

Let’s start by making a configuration file.

touch config

Then,

vim config

Use the command listed below after opening the vim configuration window.

Host (name of node 2)
HostName PrivateIP
User ubuntu
IdentityFile /root/.ssh/yourkey.pem

Host (name of node 3)
HostName PrivateIP
User ubuntu
IdentityFile /root/.ssh/yourkey.pem
  • Host: Name of node
  • Hostname: PrivateIP
  • User: Ubuntu

Once the necessary update has been made, to save the work, press esc and then :wq + enter.

Now create .pem pathway by running the command,

touch (nameofkeypair.pem)

Let’s inspect the file itself after it has been created.

vim (nameofkeypair)

In the vim text file, copy your key pair and paste it. It ought to resemble this.

After pasting the key-pair pathway, save it. Press esc, then :wq + enter. It will take you to the Docker SSH page.

The file path for the .pem file must now be created. Executing this command is the simplest approach to discovering the file path.

readlink -f filename.pem

Once the route has been found. Copy the pathway, then paste it in the vim configuration file. The illustration shows the pathway for Week16keypair.

After pasting the pathway of the go ahead and press esc, then :wq + enter.

Now it’s time to see if each node was created correctly. Use the following command to exit ssh to node 1.

cd ~

The next command to execute is,

ssh (nameofnode2)

For this illustration, it’s called “associate2.”

node1 changed to associate2.

Yes, it was a success!

Next, let’s do the third node. Run the command below.

sudo hostnamectl set-hostname (nameofnode)

Then,

ssh (nameofnode3)
Node1 changed to associate3.

Creating the Swarm using one master node and two worker nodes.

  1. Let’s open two other terminals.
  • Open the configure VS code. The image below shows what to click on next.
  • Then click on Connect to Host = Remote-SSH
  • select Docker.

It will open a new tab. Click on ‘terminal’ to open a new terminal.

Since node 1 is already open, let’s open node 2.

ssh (nameofnode)

Do the same for the last terminal. There are three different terminals in all.

2. Use the main node (node1) to do the following steps.

sudo docker swarm init 

Once a token has been successfully generated by the command, go ahead and copy the token.

Put the copied token in the “working node” The image below should be as it appears.

For the final node, repeat the process.

Go to the manager node now and execute this command.

sudo docker node ls

The result will appear as shown.

By executing the instruction, let’s now depart the swarm.

sudo docker swarm leave --force

Implement this for the two nodes.

Et voila, Done!

--

--