Debug School

rakesh kumar
rakesh kumar

Posted on

Explain the docker swarm Architecture and real time application

Define Docker swarm
How Does Docker Swarm Work?
What is Docker Swarm Used For?
Different Modes of Docker Swarm
What is Stack in Docker Swarm?
Swarm Mode CLI Commands
How to implement docker swarm
How Service consists of multiple containers of the same image
Difference between docker container and docker swarm
Difference between docker swarm and docker kubernets
Listout real time appliaction of dockerswarm

Define Docker swarm

A Docker Swarm is 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

  1. A Docker Swarm is 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.
  2. The activities of the cluster are controlled by a swarm manager, and machines that have joined the cluster are referred to as nodes.
  3. One of the key benefits associated with the operation of a docker swarm is the high level of availability offered for applications.
  4. Docker Swarm lets you connect containers to multiple hosts similar to Kubernetes.
  5. Docker Swarm has two types of services: replicated and global .

How Does Docker Swarm Work?

When you want to deploy a container in the swarm first, you have to launch services. Service consists of multiple containers of the same image. These services are deployed inside a node so to deploy a swarm at least one node has to be deployed. As you see below diagram the manager node is responsible for the allocation of the task, dispatch the tasks, and schedule the tasks. API in the manager is the medium between the manager node and the worker node to communicate with each other by using the HTTP protocol. The service of one cluster can be used by the other. All the execution of the task is performed by the worker node.

There are two types of nodes in Docker Swarm:

Manager node: Carries out and oversees cluster-level duties.
Worker node: Receives and completes the tasks set by the manager node.

Image description

Image description

Image description

A single manager node can be created but the worker node can not be created without a manager node. The ideal number for the count of the manager node is seven. Increasing the number of the manager node does not mean that the

Different Modes of Docker Swarm

Docker Swarm mainly consists of two modes they are:

Global Mode: In this mode, Docker Swarm will maintain containers in all the slave nodes and master nodes. It will maintain the replicas of containers in all the nodes which are available in the cluster.
Replicated Mode: In this mode, Docker Swarm will deploy the containers based on the no.of replicas required for you. If you required 3 replicas it will deploy the containers based on the node availability.

What is Stack in Docker Swarm

?
A stack is nothing but a collection of one or more services deployed as a single unit. The stack is deployed by using compose file in which you can mention the service of the stack and all the required configurations to deploy the stack.

With the help of stack, it is very easy to deploy and maintain complex containers like multi-containers in the Docker swarm. We can deploy with the help of a single file called Docker Compose. yaml we can define all the necessary configurations. You can assure that they deployed and scaled together.

If we deploy the new version of the application and the end users find any bugs while using it you can roll back to the previous version of the application by using Docker Swarm.

Note: Stack is mainly used to deploy the multi-container application with ease.

Features of Docker Swarm

Cluster management:- To create Swarm you can use the Docker engine CLI where you can deploy the applications. Additional orchestration software is not required to manage a swarm.
Multi-host networking:- Swarm can contain multiple overlay networks so while deploying the service you can specify the network on which you want to deploy your service. The swarm manager automatically assigns addresses to the containers on the overlay network when it initializes or updates the application.
Load balancing:- While deploying any service on a particular port the swarm automatically balances the load of these ports.
Scaling:- When you scale up or down, the swarm manager automatically adapts by adding or removing tasks to maintain the desired state.

Swarm Mode Key Concepts

Node: A Node is an instance of a Docker engine that connects to the Swarm. You can run one or more nodes on a single physical computer or cloud server. Nodes can be either a manager or workers. The manager node dispatches units of work called tasks to worker nodes. Worker nodes receive and execute tasks dispatched from manager nodes.
Services: A service is a high-level concept relating to a collection of tasks to be executed by workers. An example of a service is an HTTP Server running as a Docker Container on three nodes.
Load Balancing: Docker includes a load balancer to process requests across all containers in the service.
Docker Swarm Filters
Constraints: Based on conditions, users are restricted from creating containers on particular Docker hosts.
Drain Node: If we applied Drain Nodes on any node then Docker swarm will not assign any replicas to that node.
Port: Avoids the port conflicts between the application by deploying the same port applications in two different nodes.

Swarm Mode CLI Commands

**: This command is used to initialize the swarm.

docker swarm init [OPTIONS]
Enter fullscreen mode Exit fullscreen mode

docker swarm join: By using this command you can join a node to a swarm. The node joins as a manager node or worker node based on the token you pass with the –token flag.

docker swarm join [OPTIONS] HOST:PORT
Enter fullscreen mode Exit fullscreen mode

docker service creates: This is a cluster management command, and must be executed on a Swarm manager node.

docker service create [OPTIONS] IMAGE [COMMAND] [ARG...]
Enter fullscreen mode Exit fullscreen mode

docker service inspects: This command is used to inspect the particular service and all the details will display in JSON format.

docker service inspect [OPTIONS] SERVICE [SERVICE...]
Enter fullscreen mode Exit fullscreen mode

docker service ls: This command is used to see the complete list of all the services in that network.

docker service ls [OPTIONS]
Enter fullscreen mode Exit fullscreen mode

docker service rm: This command is used to remove the specific service you want to remove.

docker service rm SERVICE [SERVICE...]
Enter fullscreen mode Exit fullscreen mode

How to implement docker swarm

how to implement Docker Swarm using commands. This example assumes you have a few machines or virtual machines that you can use as Docker nodes.

Step 1: Initialize Docker Swarm on the Manager Node
On the machine you want to designate as the manager node, open a terminal and run:

docker swarm init --advertise-addr <manager-node-ip>
Enter fullscreen mode Exit fullscreen mode

Replace with the actual IP address of your manager node.

Step 2: Join Worker Nodes to the Swarm
On each machine you want to join as a worker node, open a terminal and run the command provided by the output of the docker swarm init command on the manager node. It will look something like this:

docker swarm join --token <token> <manager-node-ip>:<manager-port>
Replace <token>, <manager-node-ip>, and <manager-port>
Enter fullscreen mode Exit fullscreen mode

with the actual values from the manager node's output.

Step 3: Verify Swarm Status on Manager Node
On the manager node, run the following command to verify the status of the swarm:

docker node ls
Enter fullscreen mode Exit fullscreen mode

You should see the manager node and worker nodes listed with their statuses.

Step 4: Deploy a Service
Now, let's deploy a simple service. In this example, we'll use the Nginx image and expose port 80:

docker service create --replicas 3 -p 80:80 --name web nginx:latest
Enter fullscreen mode Exit fullscreen mode

This command creates a service named "web" with three replicas, exposing port 80.

Step 5: Scale the Service
Scale the "web" service to have five replicas:

docker service scale web=5
Enter fullscreen mode Exit fullscreen mode

Step 6: Update the Service
Update the "web" service to use a different version of the Nginx image:

docker service update --image nginx:1.19 web
Enter fullscreen mode Exit fullscreen mode

Step 7: Inspect the Service
Inspect the details of the "web" service:

docker service inspect --pretty web
Enter fullscreen mode Exit fullscreen mode

Step 8: Remove the Service
Remove the "web" service:

docker service rm web
Enter fullscreen mode Exit fullscreen mode

Step 9: Leave the Swarm
If you want to leave the swarm (either as a worker or manager), use the following commands:

Worker Node:

docker swarm leave
Enter fullscreen mode Exit fullscreen mode

Manager Node:

docker swarm leave --force
Enter fullscreen mode Exit fullscreen mode

These steps provide a basic example of setting up a Docker Swarm, deploying a service, scaling it, updating it, and inspecting the service. You can adapt these commands for more complex scenarios based on your specific use case.

Difference between docker container and docker swarm

Image description

Image description

Difference between Docker Swarm and Kubernetes

Image description

Image description

Image description

Image description

How Service consists of multiple containers of the same image

Create a file named docker-compose.yml with the following content:

version: '3'

services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
    deploy:
      replicas: 3
Enter fullscreen mode Exit fullscreen mode

In this Docker Compose file:

  1. The web service is based on the Nginx image.
  2. It exposes port 80 on the host, mapping it to port 80 on each container.
  3. The deploy section is used to configure deployment-related settings, including the number of replicas.
  4. Save the docker-compose.yml file .

Open a terminal in the directory where the docker-compose.yml file is located.

Run the following command to start the services:

docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

This command tells Docker Compose to start the services in detached mode (-d), allowing them to run in the background.

Check the running containers:

docker-compose ps
Enter fullscreen mode Exit fullscreen mode

You should see three containers running as part of the web service.

Access the Nginx web server in a web browser by navigating to http://localhost. You should see the default Nginx welcome page.
This example demonstrates how to use Docker Compose to create a service with multiple containers based on the same image. The replicas option in the deploy section allows you to specify the desired number of container replicas for the service.

Listout real time appliaction of dockerswarm

Below is a checklist with real-time applications of Docker Swarm, along with corresponding examples, commands, and expected outputs.

1. Initialize Docker Swarm:
Example:

docker swarm init --advertise-addr <manager-node-ip>
Enter fullscreen mode Exit fullscreen mode

Command Output:

Swarm initialized: current node is now a manager.
2. Join Worker Nodes to the Swarm:
Example:

docker swarm join --token <token> <manager-node-ip>:<manager-port>
Enter fullscreen mode Exit fullscreen mode

Command Output:

This node joined a swarm as a worker.
3. Deploy a Service:
Example:

docker service create --replicas 3 -p 80:80 --name web nginx:latest
Enter fullscreen mode Exit fullscreen mode

Command Output:

ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
<service-id>        web                 replicated          3/3                 nginx:latest        *:80->80/tcp
Enter fullscreen mode Exit fullscreen mode

4. Scale the Service:
Example:

docker service scale web=5
Enter fullscreen mode Exit fullscreen mode

Command Output:

web scaled to 5 replicas
Enter fullscreen mode Exit fullscreen mode

5. Update the Service:
Example:

docker service update --image nginx:1.19 web
Enter fullscreen mode Exit fullscreen mode

Command Output:

6. Inspect the Service:
Example:

docker service inspect --pretty web
Enter fullscreen mode Exit fullscreen mode

Command Output:

... (detailed service information)
7. Remove the Service:
Example:

docker service rm web
Enter fullscreen mode Exit fullscreen mode

Command Output:

8. Leave the Swarm:
Example (Worker Node):

docker swarm leave
Enter fullscreen mode Exit fullscreen mode

Example (Manager Node):

docker swarm leave --force
Enter fullscreen mode Exit fullscreen mode

Command Output:

Node left the swarm.
9. List Nodes in the Swarm:
Example:

docker node ls
Enter fullscreen mode Exit fullscreen mode

Command Output:

ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
<node-id>                     worker-1            Ready               Active                                  20.10.3
<node-id>                     worker-2            Ready               Active                                  20.10.3
<node-id> *                   manager-1           Ready               Active              Leader              20.10.3
Enter fullscreen mode Exit fullscreen mode

10. Swarm Visualizer:
Example:

docker service create --name=viz --publish=8080:8080/tcp --constraint=node.role==manager --mount=type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock dockersamples/visualizer
Enter fullscreen mode Exit fullscreen mode

Command Output:

ID                  NAME                MODE                REPLICAS            IMAGE                                PORTS
<service-id>        viz                 replicated          1/1                 dockersamples/visualizer:latest      *:8080->8080/tcp
Enter fullscreen mode Exit fullscreen mode

Access Visualizer: Open a web browser and navigate to http://:8080 to visualize the Docker Swarm.

These examples demonstrate common operations and real-world use cases of Docker Swarm along with the corresponding commands and expected outputs.

Refrences
Refrences

Top comments (0)