Docker swarm cluster deployment
Information:
Docker Swarm is a native orchestration tool for Docker that provides several benefits, including: Scalability: Docker Swarm allows you to easily scale your application by adding or removing worker nodes as needed. High availability: With Swarm, you can ensure that your application is always running by creating multiple replicas of your service. Load balancing: Swarm automatically load balances incoming requests across all replicas of a service, ensuring that no single replica becomes a bottleneck. Self-healing: In case of a node failure, Swarm automatically reschedules the service on a healthy node, ensuring that your application stays up and running. Rolling updates: Swarm allows you to perform rolling updates on your services, which means that you can update your application without any downtime. Decentralized architecture: Swarm enables you to build a decentralized architecture, which allows your application to run on multiple hosts and/or cloud providers. Secure multi-tenancy: Swarm enables you to secure your application and infrastructure by separating the different environments and tenants. Easy to use: Swarm is simple to use, and it provides a simple and consistent command line interface across all your nodes.
Below are the steps and commands to set up a basic Docker Swarm with one manager and one worker node:
- Step 1: On the manager node, initialize the Swarm:
docker swarm init
Information:
On the manager node, initializing the Swarm creates a new Swarm and makes the current node the manager of the Swarm. This is the first step in creating a Docker Swarm cluster as it sets up the initial state of the Swarm and allows other nodes to join it.
The command to initialize a Swarm is
docker swarm init
. This command creates a new Swarm and generates a join token that other nodes can use to join the Swarm.When you run
docker swarm init
command, it will output a command to add worker nodes to the swarm, this command includes the token that the worker nodes will use to join the swarm.Additionally, it also starts the swarm manager, which is a background process that manages the state of the Swarm. The manager is responsible for maintaining the desired state of the Swarm, scheduling services, and responding to queries about the state of the Swarm.
In summary, initializing the Swarm on the manager node creates a new Swarm, makes the current node the manager, generates a join token for other nodes to join the Swarm, and starts the manager process which is responsible for maintaining the state of the Swarm.
- Step 2: On the worker node, join the Swarm as a worker:
docker swarm join --token <TOKEN> <MANAGER_IP>:2377
Information:
On the worker node, joining the Swarm as a worker makes the current node a part of the Swarm cluster, allowing it to run services and tasks that have been scheduled by the manager.
To join a Swarm as a worker, you need to use the
docker swarm join
command and provide the join token that was generated when the Swarm was initialized on the manager node. The join token is used to authenticate the worker node and ensure that it is joining the correct Swarm.Here is an example of the command to join a Swarm as a worker:
docker swarm join --token <TOKEN> <MANAGER_IP>:2377
Once the worker node joins the Swarm, it will receive instructions from the manager on which services and tasks to run. The manager will also automatically load-balance incoming requests across all replicas of a service, ensuring that no single replica becomes a bottleneck.
By adding worker nodes to a Swarm, you can easily scale your application and ensure high availability, since the manager will automatically reschedule tasks on healthy nodes if a worker node fails.
In summary, joining a worker node to a Swarm allows the node to run services and tasks scheduled by the manager, provides scalability and high availability, and it is done using the
docker swarm join
command and providing the join token generated on the manager node.
- Step 3: On the manager node, you can check the status of the Swarm by running below command:
docker node ls
Information:
On the manager node, checking the status of the Swarm by running the command
docker node ls
allows you to see the current state of the Swarm, including information about all the nodes that are currently part of the Swarm.The
docker node ls
command shows information about each node in the Swarm, such as the node’s hostname, its role (manager or worker), its status (ready or down), and the version of Docker that it is running. This information can be useful when troubleshooting issues with the Swarm or when you are trying to understand the current state of your cluster.The
docker node ls
command also allows you to see the availability of the nodes, if a node is marked as “down” it means that the node is disconnected from the swarm, it could be due to network issues or the node has been shutdown.Additionally, this command also allows you to see if there are any tasks running on a specific node, by using the flag
-q
it will show the task id running on each node.
Now your both nodes manager and worker are connected!