Getting Started with Docker Swarm
Docker Swarm orchestrates containers, utilizing Docker applications in a clustered setup. The cluster, governed by a swarm manager, comprises nodes—machines integrated into the system. This tutorial demystifies Swarm operations: initializing Docker Engines in swarm mode, expanding nodes, deploying services, and overseeing the swarm's management. Utilizing Docker Engine CLI commands via terminal, this guide serves as a primer for newcomers, directing them to essential Docker insights if unfamiliar with the platform.
Benefits of Container Orchestration System?
Swarm makes it very easy for teams to access and manage the environment in a decentralized manner. By increasing the security of the environment, any communication between the manager and client nodes within the docker swarm is highly secure by design. The load balancing feature of docker swarm lets you build highly scalable applications, and docker swarm also allows you to roll back environments to previous safe environments easily.
Set Up
Let's see the steps involved in setting up a docker swarm cluster.
Three Networked Host Machines
To set up a docker swarm cluster, we will need three Linux hosts that have Docker installed and can communicate with each other over a network.
One of these machines will be designated as the manager (manager1), and the remaining two machines will be workers (worker1 and worker2). These can be physical machines, virtual machines, Amazon EC2 instances, or hosted in some other way.
The IP Address of the Manager Machine
The IP address must be assigned to a network interface that is available to the host operating system to ensure that all nodes in the Swarm can connect to the manager. A fixed IP address will simplify the process and make it easier for other nodes to contact the manager.
For our purposes, we will use 192.168.99.100 as the IP address for the manager.
Open Protocols and Ports between the Hosts
The following ports must be available and open by default on some systems:
- TCP port 2377 for cluster management communications
- TCP and UDP port 7946 for communication among nodes
- UDP port 4789 for overlay network traffic
Initialize the Cluster
-
Run this command to initialize docker swarm on the nodes - docker swarm init --advertise-addr=<external ip>
-
To add a worker to this Swarm, run docker swarm join — token SWMTKN-1–9afc18ehuc5xxxxxxxxxx-xxxxxxxxxxx x.x.x.x:xxxx
-
To add a manager to this Swarm, run docker swarm join-token manager and follow the instructions
-
Check the status and availability of nodes by running - docker node ls
How to Create a Service?
Follow these steps to understand the creation of a service -
- Run the following command on the manager node to create an Nginx service with two replicas-
- Check the status of the service -
How to Access the Service?
You can access the service by requesting any of the manager or worker nodes. You can curl to any of the Docker Machine IPs or hit the URL here in the browser. The standard NGINX Homepage should appear.
How to Scale Up and Down?
You can increase or decrease the number of containers for a particular service at any time by using the following command.
Some Useful Commands
List All the Services Running in the Docker Swarm
List All the Running Containers in the Docker Swarm
Inspect Nodes in the Docker Swarm Cluster
Remove a Particular Service in the Docker Swarm
Inspect Service Running in Docker Swarm
See All Containers and Images (running, stopped, or dead once) Inside the Docker Swarm
Remove Unused Images, Containers, etc., in the Docker Swarm
Remove All Images being Used in the Docker Swarm
Features of Docker Swarm
The major features which make Docker swarm unique and beneficial for application developers are -
- Decentralized access
- High security
- Autoload balancing
- High scalability
- Easy Rollbacks
Swarm Mode Key Concepts
One of the key concepts of swarm mode over standalone containers is that you can modify a service's configuration, including the networks and volumes it is connected to, without needing to restart the service manually. Docker will update the configuration, stop the service tasks with the out-of-date configuration, and create new ones matching the desired configuration.
How Does Docker Swarm Work?
The working mechanism of Docker Swarm can be challenging to understand. But the basics of docker swarm's working are as follows -
- Docker Swarm is a group of Docker hosts that are networked together as a cluster to deliver specified tasks. Each Docker host in this cluster is a node, also called a worker node.
- For tasks to be distributed evenly, the docker swarm requires a manager node. A Docker Swarm mode initialization starts with the manager node, and subsequent nodes become workers.
- As the user, you only need to interact with the manager node, which passes instructions to the workers. The worker nodes receive task allocation from the manager node and execute them accordingly.
- Since nodes do fail from time to time, the manager node is responsible for monitoring the state of each worker node. If a node happens to fail, the manager node will activate a fail-tolerant mechanism to reschedule the task to another working node.
- Docker swarm's working has been designed in such a way that even if the manager node fails - the cluster would continue to function.
Kubernetes vs Docker Swarm
The primary distinction between the two platforms comes down to your application's complexity. Kubernetes is the right platform for you if you have a more complicated app. On the other hand, if you're looking for something easier to use, Docker Swarm is a better choice for simpler applications.
Demo On Docker Swarm For Beginners
Let's understand docker swarms working with a visualizer.
Swarm Visualizer is a fancy tool that visualizes the Swarm Cluster setup. It displays containers running on each node in the form of visuals. Try it on -
Conclusion
Docker swarm is a great tool for easily managing multiple containers and providing failover and high availability. This article covered important concepts related to the docker swarm and explained some of the most useful commands.