Deploying services over docker swarm
Learn how to deploy app over the swarm cluster
What is a 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. Once a group of machines have been clustered together, you can still run the Docker commands that you’re used to, but they will now be carried out by the machines in your cluster. The activities of the cluster are controlled by a swarm manager, and machines that have joined the cluster are referred to as nodes.
What are Docker Swarm Nodes?
A docker swarm is comprised of a group of physical or virtual machines operating in a cluster. When a machine joins the cluster, it becomes a node in that swarm. The docker swarm function recognizes three different types of nodes, each with a different role within the docker swarm ecosystem:
The primary function of manager nodes is to assign tasks to worker nodes in the swarm. Manager nodes also help to carry out some of the managerial tasks needed to operate the swarm. Docker recommends a maximum of seven manager nodes for a swarm.
When a cluster is established, the Raft consensus algorithm is used to assign one of them as the “leader node”. The leader node makes all of the swarm management and task orchestration decisions for the swarm. If the leader node becomes unavailable due to an outage or failure, a new leader node can be elected using the Raft consensus algorithm.
In a docker swarm with numerous hosts, each worker node functions by receiving and executing the tasks that are allocated to it by manager nodes. By default, all manager modes are also worker nodes and are capable of executing tasks when they have the resources available to do so.
Docker installed on your system/server. You can check it using:
If you haven’t installed docker in your system yet. Please follow my previous article, “Deploy node.js web app on docker”.
NOTE: If you want to test swarm-cluster without purchasing any server, you have to visit following link:
Creating Swarm cluster
By default, the swarm is in inactive form. You have to run the following command to activate swarm.
- docker swarm init — advertise-addr <pub IP address>
After activating the swarm there is a prompt appear like the following:
root@jv-web-nw-1:~# docker swarm init --advertise-addr <public ipaddress>Swarm initialized: current node (m1v1gdp0m2q71qrxe6b0m5e58) is now a manager.To add a worker to this swarm, run the following command:docker swarm join --token SWMTKN-1-5zlnd6luxfpc0gmlal1em3c9b97nbn0pn5z1oa8l79qmcrq3bo-2waeh0jjh5vy61midsblqp52k <ip address>:2377To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.root@jv-web-nw-1:~#
SSH on another server and paste the above-highlighted text. Now, current server is worker.
docker swarm join --token SWMTKN-1-5zlnd6luxfpc0gmlal1em3c9b97nbn0pn5z1oa8l79qmcrq3bo-2waeh0jjh5vy61midsblqp52k <ip address>:2377
Update node2 as manager
docker node update --role manager node2
We can update the role of another node from a manager node using the “docker node update — role” command.
To add a manager to this swarm, run the following command
docker swarm join-token manager
root@jv-redis-r1:~# docker swarm join-token managerTo add a manager to this swarm, run the following command:docker swarm join --token SWMTKN-1-0lmu652pnv8qjw6qvhe11wlkb2vtvtd9m2v9le8kke0plthq9v-1yprt6w0akz0m21do4183yape 220.127.116.11:2377
SSH on the server, you are looking for manager swarm and paste the above-generated link
Make sure you have docker installed on that server
Now, go to the first node and see all nodes using the following command
docker node ls
Create service with 3 replicas
docker service create --replicas 3 alpine ping 18.104.22.168
You can verify on any manager node using
docker service ls
You can run service replicas as you need
docker service create — name search — replicas 3 -p 9200:9200 elasticsearch:2
You can build your own docker image as a service.
docker service create — name testingapp -p 80:80 --network frontend --replicas 2 <docker image>Ex- docker service create --name express_test -p 80:3000 --replicas 3 kdsharma/testing-node
Restart changes with service
docker service update — image <Dockerhub user>/<image> <service name>
Scale the service in the swarm
docker service scale <SERVICE-ID>=<NUMBER-OF-TASKS>