Docker Swarm Tutorial: Orchestrate Containers for Scalable Apps

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.

What is Docker Swarm?

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.

Advantages of Docker Swarm

  • Node Clustering – 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.

				
					apt install apt-transport-https ca-certificates curl software-properties-common
				
			

Then, download the Docker’s GPG key using the command given below.

				
					curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
				
			

Next, add the Docker repository path to the APT file.

				
					echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
				
			

Then, update the repository path using the following command.

				
					apt update
				
			

Now, install both Docker and Docker Compose packages using the following command.

				
					apt install docker-ce docker-compose -y
				
			

Once both packages are installed, verify the Docker version with the following command.

				
					docker --version
				
			

Output.

				
					Docker version 24.0.5, build ced0996
				
			

To verify the Docker Compose version, run the following command.

				
					docker-compose --version
				
			

Output.

				
					docker-compose version 1.29.2, build unknown
				
			

Initialize a Docker Swarm Cluster

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.

				
					docker swarm join --token SWMTKN-1-1x1c0v6ndjq0jr3rvn41klx0329odl56hk1hbqk00x0xfyhojn-8d7qgx2bbnptwgbhltmf3b1tc 45.58.47.245:2377
				
			

Next, go back to the master node and verify the Swarm cluster using the following command.

				
					docker node ls
				
			

You will see all nodes in the following screen.

Deploy and Scale Services on 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  
				
			

Deploy Docker Swarm Visualizer to View Nodes

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
				
			

You will see the following output.

				
					Unable to find image 'dockersamples/visualizer:latest' locally
latest: Pulling from dockersamples/visualizer
ddad3d7c1e96: Pull complete 
3a8370f05d5d: Pull complete 
71a8563b7fea: Pull complete 
119c7e14957d: Pull complete 
28bdf67d9c0d: Pull complete 
12571b9c0c9e: Pull complete 
e1bd03793962: Pull complete 
3ab99c5ebb8e: Pull complete 
94993ebc295c: Pull complete 
021a328e5f7b: Pull complete 
Digest: sha256:530c863672e7830d7560483df66beb4cbbcd375a9f3ec174ff5376616686a619
Status: Downloaded newer image for dockersamples/visualizer:latest
5170835c5a5c8baedf00d9282cf21e0c54b92e353c58bbdea79e10322bee62e8
				
			

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.

				
					docker stack deploy --compose-file docker-compose.yaml appstack
				
			

You will see the following output.

				
					Creating network appstack_default
Creating service appstack_appsvc
Creating service appstack_visualizersvc

				
			

Now, go back to the Docker Swarm Visualizer interface and refresh the page. You will see all deployed services on the following screen.

Verify Docker Swarm High-Availability

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.

Avatar for Hitesh Jethva
Hitesh Jethva

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.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x