Docker is an open-source platform that automates the deployment, scaling, and management of applications using containerization. Containers are lightweight, portable, and self-sufficient units that include everything needed to run a piece of software, including the code, runtime, libraries, and system tools. This ensures that the application runs consistently across different environments.

What is Docker Swarm?

Docker Swarm is a native clustering and orchestration solution for Docker. It allows you to create and manage a swarm of Docker nodes, which act as a single virtual Docker host. Swarm enables you to deploy and scale containerized applications across multiple machines, providing high availability and load balancing. It simplifies the management of containerized applications and ensures that they run consistently across different environments.Docker Swarm is a native clustering and orchestration solution for Docker. It allows you to create and manage a swarm of Docker nodes, which act as a single virtual Docker host. Swarm enables you to deploy and scale containerized applications across multiple machines, providing high availability and load balancing. It simplifies the management of containerized applications and ensures that they run consistently across different environments.

The image above is a comparison if we use standalone and swarm docker. and here is the comparison table.

| Docker Standalone                    | Docker Swarm                         |
|--------------------------------------|--------------------------------------|
| Runs on a single host                | Runs on multiple hosts               |
| Suitable for small-scale deployments | Suitable for large-scale deployments |
| Limited fault tolerance              | High fault tolerance                 |
| No built-in load balancing           | Built-in load balancing              |
| No automatic service discovery       | Automatic service discovery          |
| Limited scalability                  | High scalability                     |

Prerequisites

  • More than two Server / VM can communicate each other
  • Docker installed

One line docker install: https://tkjpedia.com/autoinstaller-docker-ubuntu/

Init the swarm manager

The docker swarm init command is used to initialize a Docker Swarm, turning your current Docker host into a Swarm manager node.

/ # docker swarm init
Swarm initialized: current node (y8knh75umbp3f8r5g9jpt8cae) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-2jnqrclmwgemftvcul3t9pcer0wc1yz1wih8b6dlkedxhcm1y3-dn2qggw3861uwgmqmsbr6mvv6 172.18.0.2:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

/ #

Add Worker node

This command is used to add a Docker node to an existing Swarm cluster.

docker swarm join --token SWMTKN-1-2jnqrclmwgemftvcul3t9pcer0wc1yz1wih8b6dlkedxhcm1y3-dn2qggw3861uwgmqmsbr6mvv6 172.18.0.2:2377

Verify the worker joined

docker node ls

The docker node ls command is used to list all nodes in a Docker Swarm. It only works when run on a Swarm manager node.

Testing

Create one service, example: nginx

docker service create --name nginx --publish 3000:80 nginx:latest

check the container readiness

try to scale out the container

the container will be deployed across all three nodes

Node id: 7a38852a24cb, 9f9991524faf, f979d690d36e

access the container

You can add loadbalancer infront of vm. using nginx vm or other loadbalancer like AWS ALB.

Next: High Availability Test