how to create docker swarm6 min read
HI All, this document deals with how to create docker swarm. I hope you read my previous blog introduction to docker swarm. If not, read the article to get to a clear understanding of docker swarm and microservices.
Setting up and managing docker swarm is easy. It not like any other orchestration tools like Kubernates etc.
* install docker engine in all the nodes.
* take 2 nodes for the cluster, where one will be the manager and another one is worker node. You can add as many nodes as per your requirement
* Docker image to run. Here I’m using a nodejs application. if you want the same image to run, check this document.
Now let’s create docker swarm and deploy containers to the clusters
Out of 2 servers, I’m choosing one server to act as the manager/leader node. In my case, 192.168.0.169 is manager node. So let’s initialize the docker swarm. Execute the below command.
Initialize Docker swarm
To add a manager to this swarm, run ‘docker swarm join-token manager’ and follow the instructions.
The above command initializes docker swarm. Parameter –advertise-addr advertise the address of the manager address.
So once you initialize the docker swarm you can see the output “Swarm initialized”
Now Join your other node(worker node) to manager node, you can do this by executing the below command, where you have the token and address to join the manager node.
Now Run the below command to see the number of the nodes joined to docker swarm, here Manager Status with “leader” is the manager node, Status shows the status of each node.
NOTE: You can execute the below command only manager node.
Now we are done the Docker swarm setup with two nodes, let’s create a service in docker swarm
Here I have docker image of nodejs application in my private Docker registry. Check the prerequisites section to create a docker image.
Execute the below command create a service of nodejs application.
docker service create –replicas 2 –name nodejs dockerreg:5000/swarm:latest
overall progress: 2 out of 2 tasks
1/2: running [==================================================>]
2/2: running [==================================================>]
verify: Service converged
– Create, create the docker swarm service
– replicas assign the docker manager to maintain 2 replicas(containers ) of nodejs application
– name, the name of the service
– dockerreg:5000/swarm:latest, my nodejs application docker image stored in docker private registry.
If you check in two docker nodes, using “docker ps” command you can check number for containers running on each node.
For example here the containers running on Manager node. Don’t get confused with registry container. It is my private Docker registry
For example here the containers running on the worker node.
So now you will have a question, what will happen when the worker node dies?
Whenever the manager node detects worker node is unresponsive, new containers will get created in the existing worker node/manager node in order to maintain the replicas.
Increase the scale of your service
Now you can increase the replicas of containers without disturbing the running service, execute the below command to perform this
Now check the number of containers in each node
Remove a service
To remove a service from docker swarm, execute the below command with the service name.
Publish port of the service in order to access from outside
In my example, my docker nodejs application is running on port number 8081. In order to access my application from the outside bind host port to the Docker container.
Once we do this application can be accessed from any node using host bound port number.
Here is the diagram of docker swarm cluster which gives an idea about swarm load balancer and how it containers are accessed from outside using the published port.
Containers in the swarm can be accessed using any nodes hosts published port.
You can use any external load balancer such as Nginx, Haproxy to route the requests to the docker swarm service. Nginx/Haproxy can have any type of load balancer or failover to connect the worker nodes.
Here is the sample diagram when configuring an external load balancer.
I hope you understood about docker swarm, subscribe our newsletters for more articles.
happy learning 🙂