Docker Swarm Tutorial: Orchestrate Containers for Scalable Apps. In this post, we introduce Docker Swarm and show you how to deploy scalable and highly available application using that tool.
Basically, a Docker Swarm is made up of a number of worker nodes and a minimum of one manager node, which is in charge of effectively administering and managing the worker nodes’ resources and overseeing the smooth operation of the system. In order to establish contact with the manager node, the worker node uses API over HTTP.
Let’s start the article Docker Swarm Tutorial: Orchestrate Containers for Scalable Apps.
Docker Swarm is a tool used by DevOps for Docker containers. Users run multiple files and images in the same host environment thanks to this useful clustering and scheduling tool. In fact, IT managers and developers even set up and control a cluster of Docker nodes as a single virtual system using it. Further, the deployment of containers may be automated using Swarm.
For container technology, clustering is a vital feature that assembles a cooperative group of systems to maintain smooth functioning. In order to enable Docker Swarm failover in the event that one or more nodes go down, clustering plays a key role. Machines that connect with the cluster are commonly referred to as nodes.
The ability to add or remove container iterations in response to shifting system needs is another benefit of a Docker Swarm cluster for administrators and developers. Additionally, the cluster manager or swarm manager provides high availability and assures that only one container may be executed on a node at a time.
It has a swarm manager responsible for coordinating and scheduling containers that helps IT administrators manage Swarm. In case the original instance fails, the swarm manager enables the user to establish numerous replica instances. Further, the user can instantly install management and worker nodes in Docker Engine’s swarm mode.
NodeClustering – In order to create a single virtual host, Docker Swarm groups together numerous Docker hosts. It allows you to even pool the resources of these hosts, whether they are physical or virtual machines, in order to execute containers more effectively.
Overlay Networking – Through the use of overlay networks, Swarm establishes a virtual network that connects all of the cluster’s servers, allowing communication between containers running on various hosts.
Load Balancing – A seamless and scalable user experience is provided by Swarm’s inbuilt load balancer, which intelligently splits up all the traffic that is coming across containers by running the same service.
Utilize the potential of containers – Docker swarm is a favorite among developers for it allows them to completely utilize the design advantages provided by containers. Developers can now deploy programs or services in independent virtual environments. Further, due to their architecture, containers consume computational power more effectively than virtual machines, making them a more lightweight alternative.
Guarantees High Service Availability – Redundancy improves application availability, which is one of Docker Swarms’ key advantages. A docker swarm needs a swarm manager who delegates work to worker nodes in order to operate. The system keeps running even if a single manager node fails because many managers have been implemented. For each cluster, Docker suggests using no more than seven manager nodes.
Docker Swarm offers an easy to use method for managing containerized applications at scale. A Docker Swarm typically consists of a number of worker nodes and at least one management node, which manages the worker nodes’ resources and makes sure the cluster runs smoothly. Any business that wishes to deploy or manage containers in a production environment must use Docker Swarm.
The built in service discovery offered by Swarm makes it possible for containers to communicate with one another across many hosts. It even comes with five filters, including affinity, port, health, dependency, and constraint.
Docker Swarm Tutorial: How to Orchestrate Containers for Scalable Applications
In this section, we set up a three node Docker Swarm cluster and deploy a scalable and highly available Spring Boot application using the Docker Swarm.
Prerequisites
Three servers running Linux operating systems.
A root password or a user with sudo privileges is set up on all servers.
Install Docker and Docker Compose
Before starting, you will need the Docker and Docker Compose software to be installed on all servers. It is always recommended to install both software from Docker’s official repository to get the latest version.
First, install the necessary dependencies using the following command.
At this point, Docker and Docker Compose are installed on all nodes. Now, you will need to initialize a Swarm cluster on the Master node.
Run the following command to initialize a Docker Swarm.
docker swarm init --advertise-addr master-node-ip
Once the cluster is initialized, you will get the following screen.
As you can see, Swarm mode has been initialized, and this node is now the manager.
Note: Please note down the above command. You will need it to add a worker node to the Swarm cluster.
Add Both Worker Nodes to Docker Swarm
Now, you will need to add both worker nodes to the Swarm cluster. If you forgot the tocket to join the Swarm cluster, you can retrieve it with the following command.
docker swarm join-token worker
You see the following output.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-1x1c0v6ndjq0jr3rvn41klx0329odl56hk1hbqk00x0xfyhojn-8d7qgx2bbnptwgbhltmf3b1tc 45.58.47.245:2377
Now, run the following command on both worker nodes to join them to the Docker Swarm.
With Docker Swarm, you can also deploy a highly available and scalable application on the cluster. Let’s creates a service named “helloworld” that uses the alpine Docker image and runs the command ping docker.com.
docker service create --replicas 1 --name helloworld alpine ping docker.com
You can verify the deployed service using the following command.
docker service ls
Output.
ID NAME MODE REPLICAS IMAGE PORTS
sz39jpoa7vcj helloworld replicated 1/1 alpine:latest
Now, scale the service in Docker Swarm with the following command.
docker service scale service-id=5
Please verify the scaled service with the following command.
docker service ls
Output.
ID NAME MODE REPLICAS IMAGE PORTS
sz39jpoa7vcj helloworld replicated 5/5 alpine:latest
Docker Swarm Visualizer is an open source application that allows you to view your Swarm cluster nodes via a graphical interface. Run the following command on the master node to deploy the Docker Swarm Visualizer in a container.
docker run -it -d -p 5000:8080 -v /var/run/docker.sock:/var/run/docker.sock dockersamples/visualizer
To verify the deployment, run the following command.
docker ps
You will see the following output.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5170835c5a5c dockersamples/visualizer "/sbin/tini -- node …" 42 seconds ago Up 39 seconds (healthy) 0.0.0.0:5000->8080/tcp, :::5000->8080/tcp amazing_mclaren
Now, open your web browser and access the Docker Swarm Visualizer web interface using the URL https://your-master-ip:5000. You should see all your nodes on the following screen.
Deploy a Spring Boot Application on Docker Swarm
To deploy an application in the production environment, Docker Compose provides the easiest way to combine all services in a single file and deploy them via a single command. Let’s create a Docker Compose file to deploy a Spring Boot application with Docker Swarm Visualizer.
nano docker-compose.yaml
Add the following configurations.
version: '3.7'
services:
# Spring boot app service.
appsvc:
# Spring boot app image from DockerHub
image: pasciano007/sample:1.0
deploy:
# Number of replicas
replicas: 6
ports:
# Expose container port 8080 to host on port 8080 (HOST:CONTAINER)
- '8080:8080'
# Open source docker-swarm-visualizer service
visualizersvc:
# Image of docker-swarm-visualizer app
image: dockersamples/visualizer
# This service will run only when appsvc has no failure
depends_on:
- appsvc
deploy:
placement:
# We want to deploy this service only in manager node (suppose we have only one)
constraints:
- 'node.role==manager'
ports:
# Host port : container port
- '5000:8080'
volumes:
# Bind volumne host to container
- 'https://net.cloudinfrastructureservices.co.uk/var/run/docker.sock:/var/run/docker.sock'
Save and close the file. Then, run the following command to deploy the services.
In this section, we show you how Docker Swarm handles the services in the event of node failure. To test it, let’s stop the Docker service in the first worker node using the following command.
systemctl stop docker
Now, go to the Docker Swarm Visualizer interface and refresh the page. You will see the that red color in Worker node 1 indicates that something is wrong on that node.
In this case, Docker restarts all three services running on Worker Node 1 and spread them across the cluster on the master node and Worker 1 node.
Docker Swarm Tutorial:Orchestrate Containers for Scalable Apps Conclusion
In this post, we have set up a Docker Swarm cluster and deployed a simple helloworld service on that cluster. We have then installed a Docker Swarm Visualizer to access all nodes via web UI. Next, we showed you how to use Docker Compose to deploy a Spring Boot application on the production environment and test the high availability. I hope you can now easily use Docker Swarm to deploy a scalable and highly available application on your server.
I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.
00votes
Article Rating
Subscribe
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.
DisagreeAgree
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.