Docker Swarm
Docker swarm is an orchestration service within the docker service that allows us to manage multiple containers at a time.
it is a group of servers that runs the docker application.
it is used to manage multiple containers on multiple servers.
this can be implemented by cluster.
The activities of the cluster are controlled by a swarm manager, and the machines that have joined the cluster is called swarm worker/node.
Worker nodes receive and execute tasks dispatched from manager nodes.
There are mainly worker nodes and manager nodes.
So any scaling or update needs to be done first in the manager node.
From the manager node, all the things will go to the worker node.
Manager nodes are used to divide the work among the worker nodes.
Docker swarm components
Service: A service is the definition of the tasks to execute on the manager or worker nodes.
Task: A task carries a Docker container and the commands to run inside the container.
Manager: This manages the work among the different nodes.
Worker/Node: Works for a specific purpose of the service.
Create a swarm
Below command is used to join our machine as a manager node in the cluster.
docker swarm init --advertise-addr <MANAGER-IP>
[root@ip-172-31-41-218 ~]# docker swarm init --advertise-addr 172.31.41.218
Swarm initialized: current node (kqvubid48y15q5nevduonz3py) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-44rze4esrebi70h50j26bvtxydrn0pk8yx4icpczqrh7t2swzh-c5wxhay5w40ew2tdfo99a42o7 172.31.41.218:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
above command will generate a token to which we can add our worker nodes in the cluster.
We have to install docker and start service in the slave/worker server and then add it as a worker using the above-generated token.
docker swarm join --token SWMTKN-1-44rze4esrebi70h50j26bvtxydrn0pk8yx4icpczqrh7t2swzh-c5wxhay5w40ew2tdfo99a42o7 172.31.41.218:2377
To get the token of the manager/worker
docker swarm join-token manager/worker
we can view the total number of nodes using
docker node ls
To remove a node from the cluster firstly we have to get that node to downstate.
docker swarm leave
we need to run the above command in the slave server.
then we can remove it from the cluster.
To do so we have to run the below command in the manager/master server
docker node rm <<node_id>>
Note: without getting it to the down state we can't remove it from the cluster.
Swarm Service
we can create a service using
docker service create --name <name> image_name
we can create replicas so that containers will be deployed in worker nodes.
docker service create --name <name> --replicas <no_of_replicas> <image_name>
the pattern of distribution will be like which node contains less no of the running containers then it'll deploy one replica into that node then again check for the no of running containers.
We can create a replica in worker nodes for publicly available images only.
If we create replicas for our local images it creates all replicas in the manager node only.
docker service create --name react-app --replicas 2 --publish 8088:80 simple-app

both replicas running on same server.
To overcome this we've to push our image to dockerhub.
To update our service
docker service update --image image_name service_name
To Rollback
docker service rollback service_name