Thursday, June 27, 2024

How to Set Up and Manage a Docker Swarm: A Comprehensive Guide

 Set Up and Manage a Docker Swarm






Introduction

Docker Swarm is a powerful container orchestration tool that allows you to manage a cluster of Docker nodes as a single virtual system. In this guide, we will walk you through the steps to set up and manage a Docker Swarm, including initializing the Swarm, adding worker and manager nodes, troubleshooting connection issues, creating and scaling services, and rolling out updates.

Initializing Docker Swarm

To start using Docker Swarm, you need to initialize it on your primary machine, which will act as the manager node. Run the following command to initialize Docker Swarm:

command: docker swarm init --advertise-addr 192.168.67.9

This command sets up your machine as the manager and advertises its IP address for other nodes to join the Swarm.



Adding Worker Nodes

Worker nodes perform tasks assigned by the manager node. To add a worker node to your Swarm, you need a join token. Obtain this token by executing the following command on the manager node:

command: sudo docker swarm join-token worker

You will receive a command with a token that looks similar to the following:

command: docker swarm join --token SWMTKN-1-1cjqj7bvb19mgsi26mc4qfgmnrtturj959mb1ne9pu1im7a8vw-9hl3z4iwjgrucleh2vr986rrg 192.168.67.9:2377

Run this command on the worker node to join it to the Swarm.


Adding Manager Nodes (Optional)

To add more manager nodes for high availability and fault tolerance, obtain a join token specifically for manager nodes by executing:

command: sudo docker swarm join-token manager

Use the provided token to join the additional manager nodes. 


Troubleshooting Connection Issues

If you encounter connection issues while adding a worker node, it might be due to firewall settings. On the manager node, run the following commands to open the necessary port:

command:
sudo firewall-cmd --add-port=2377/tcp --permanent sudo firewall-cmd --reload

After adjusting the firewall settings, try adding the worker node again.




Verifying Node Addition

To verify that a worker node has been successfully added to the manager node, execute:
command: sudo docker node ls
This command lists all nodes in the Swarm along with their roles and status.


Additionally, you can check the Swarm section in the Docker info output by running:
command: sudo docker info
If the node was added successfully, it will be listed in the Swarm section.




Creating a Service

Docker Swarm allows you to deploy services across the cluster. For example, if you have an image named nodeapp:crud_exit, you can create a service with three replicas running on port 4000 using the following command:

command: docker service create --name nodeapp --replicas 3 -p 4000:4000 rizwanzafar/nodeapp:crud_exit

This command instructs Docker Swarm to run three instances of the nodeapp service, each listening on port 4000.

To see which nodes are running the replicas, use:

command: docker service ps nodeapp





Scaling the Service

You might need to scale your services based on demand. To increase or decrease the number of replicas of a running service, run:

command: docker service scale nodeapp=5

This command scales the nodeapp service to five replicas.



Rolling Out Updates

Updating a service in Docker Swarm is straightforward. If you need to deploy a different image or a new tag of the same image, use the following command:

command: docker service update --image rizwanzafar:01 nodeapp

This command updates the nodeapp service to use the new image rizwanzafar:01.



Node Availability (Optional)

if you you want my new created service should not be doploy to this specfic node then you can
dtrain the avilabilty of that node
command : docker node update --availability drain node-id check node status : docker node ls

and let now you want to revert back to availabel status you can run follwoing command
command : docker node update --availability active node-id
check node status : docker node ls

Managing Node Roles in Docker Swarm

Use following command to change a manager node to a worker node.

  • docker node demote <node_id>
Use following command to change a worker node to a manager node.

  • docker node promote <node_id>

Conclusion

Setting up and managing a Docker Swarm is an efficient way to handle containerized applications at scale. By following this guide, you can initialize a Swarm, add worker and manager nodes, troubleshoot issues, create and scale services, and roll out updates with ease. Docker Swarm provides robust features for container orchestration, making it an essential tool for modern DevOps practices.

Feel free to leave comments or questions below, and don't forget to share your experience with Docker Swarm!


Previous Post
Next Post

post written by:

0 comments: