Updated March 17, 2023
Introduction to Docker Swarm Architecture
Docker Swarm is a clustering and Orchestration tool for Containers which is inbuilt in Docker Engine for the distributed system which involves hundreds of containers. Docker Node, Docker Services, and Docker tasks are key components of Docker swarm architecture.
- Docker Node: It is the Docker Engine instance included in Docker swarm, has two kinds:
- Manager Node: Responsible for all orchestration and container management tasks required to maintain the system in the desired state such as maintaining the cluster state, scheduling the services and servicing the swarm mode HTTP endpoints.
- Docker Service: It is the task definition that needs to be executed.
Typical Docker Swarm Architecture
Below are the points for typical docker swarm architecture:
- Node is the key member of docker swarm architecture, a swarm architecture can have more than one manager nodes lead by a single manager node elected using the Raft algorithm, a manager node can also work as a worker node but can be configured to work as manager node too.
- Manager nodes use the Raft Consensus Algorithm to internally manage the cluster state. This is to ensure that all manager nodes that are scheduling and controlling tasks in the cluster maintain/store consistent state.
- A swarm is a cluster of Docker Engines or nodes, where we deploy our services. At the early stage, Docker came up with a cluster management system with a communication protocol known as Beam. Later on, they added more API’s and renamed it to swarm. The first generation swarm is called ‘swarm v1’.
Work Flow of Docker Swarm
The workflow of docker swarm consists of understanding how nodes and services work in a docker swarm architecture:
Step 1: The first step is to create a set of docker-machine that will act as nodes in the docker swarm, one among these nodes will be the manager, the remaining node\s will act as worker nodes. To create a docker-machine use the command on Windows, launch the docker terminal.
docker-machine create –driver hyperv manager1
- Where ‘manager1’ is the docker-machine name, to list the machines and it’s run respectively.
docker-machine ls
docker-machine ip manager1
Step 2: The second step is to create worker machines, use below command to create as much as worker machines you want, here let’s create 3 workers.
docker-machine create –driver hyperv worker1
docker-machine create –driver hyperv worker2
docker-machine create –driver hyperv worker3
- We can connect to the manager or worker using the below command, which will get you to inside the machine.
docker-machine ssh manager1/worker1
Step 3: Step three is to initialize the docker swarm, we have to run this command on the machine which we want work as a manager, that is how we make node manager, we can add more managers as well.
docker-machine ssh manager1
docker swarm init –advertise-and Manager1 IP
Step 4: Step four is to join the worker nodes to swarm, get the command to join node as a worker using below command on manager machine and run the command you have got on the worker machine you want to make a worker.
Docker swarm join-token worker1
- To check whether the worker is joined to swarm or not go to the manager machine and run command, it will list the added worker machine with manager details.
docker-machine ls
Step 5: Step five is to run the docker standard commands on the manager.
docker info (fetches details about docker swarm)
docker swarm (commands that can run on docker swarm such as join,leave,update,init and unlock etc)
Step 6: Step six is to run the docker containers on docker swarm, we can create any service and it’s replicas. Go to ub.docker.com, login and go to explore depositaries we can see different images which are engines, for example, nginx(it runs on webserver) creates a service and it’s replica’s using below command on the manager.
docker service create –replicas 3 -p 80:80 –name sample nginx
- Where ‘sample’ is the service name and 80 is the port that is being exposed to check the status of the service run below command on the manager node.
docker service ls
docker service ps sample
Check the service running on all nodes and also check on the browser by giving IP for all nodes.
Step 7: Step seven, now we can see the actual use of docker swarm where we scale the service up and down. To scale a service run the below command on manager machine
docker service scale sample=4
- The above command will make the service sample to run on 4 nodes, even though all nodes are busy it will create another service on manager or worker. Similar way we can scale down the service.
docker service scale sample=2
- We can also inspect the node to get the details like how many and which service are running on a node by running the below commands on the manager node
docker node inspect worker1/manager1
docker node inspect self
Step 8: Step eight is to update service when service is running on multiple machines and if we want to update the service it very is and simple, for example, if we want to update the version of the service nginx.
docker service update –image nginx:1.14.0 sample
Step 9: Shutdown/stop/remove.
- To shut down any particular node use the below command, which changes the status of the node to ‘drain’.
docker node update –availability drain worker1
- To remove service from all machines.
docker service rm sample
- To leave the swarm, which changes the status of the ‘down’.
docker swarm leave
- To stop a machine (Run from docker terminal, not in manager or worker machine).
docker-machine stop worker1
docker-machine rm worker1
Benefits of Docker Swarm Architecture
Below are the points shows the benefits of Docker Swarm Architecture:
- Decentralized design: we can manage swarm clusters through swarm command, It gives single p[oint of access to build entire swarm.
- It is very simple compared to Kubernetes.
- High Availability: Among the nodes available in swarm if master fails another worker node will take upcharge.
- Desired state Reconciliation: The swarm manager keeps track of the cluster state so that the desired and actual state is always the same.
- When we specify an over network to connect to your services, the swarm manager assigns addresses to the container on overlay network once we create /update the containers
- Rolling Updates: Service updates can be done incrementally swarm manager allows you to specify the delay between every update without any downtime.
Conclusion
Docker swarm is a simple tool in place of big tools that accomplish the task, Docker with swarm provides orchestration that we need, it’s easy to set up and ships the native with docker engine which we are going to install and use anyway.
Recommended Articles
This is a guide to Docker Swarm Architecture. Here we discuss how nodes and services work in a docker swarm architecture with their benefits. You can also go through our other related articles to learn more –