Managing Docker Swarm : #4

Managing Docker Swarm : #4

Creating a Manager, Adding a Worker, Assigning a Task, and Removing the Worker

In this tutorial, we'll walk through the process of creating a Docker Swarm manager, adding a worker node to the cluster, assigning a task to the worker node, and then removing the worker node from the Swarm.

Step 1: Creating a Docker Swarm Manager

To create a Docker Swarm manager, use the docker swarm init command on the desired host:

docker swarm init --advertise-addr <manager_ip_address>

This command initializes a new Swarm and designates the current host as the manager node.

Step 2: Adding a Worker Node

To add a worker node to the Swarm, use the docker swarm join command on the node you want to add:

docker swarm join --token <worker_token> <manager_ip_address>:<manager_port>

Replace <worker_token> with the token provided during the initialization of the manager node and <manager_ip_address>:<manager_port> with the IP address and port of the manager node.

Step 3: Assigning a Task

Now that we have a manager and a worker node in the Swarm, let's assign a task. For example, let's deploy a simple NGINX service:

docker service create --name my-nginx --replicas 1 -p 80:80 nginx:latest

This command creates a service named my-nginx with one replica, mapping port 80 on the host to port 80 on the NGINX container.

Step 4: Verifying Task Assignment

To verify that the NGINX service is running and assigned to the worker node, use the docker service ps command:

docker service ps my-nginx

This command displays the tasks associated with the my-nginx service, including the node on which each task is running.

Step 5: Removing the Worker Node

Finally, let's remove the worker node from the Swarm. First, we need to take the node offline:

docker node update --availability drain <worker_node_id>

Replace <worker_node_id> with the ID of the worker node you want to remove.

After draining the node, we can safely remove it from the Swarm:

docker node rm <worker_node_id>

Replace <worker_node_id> with the ID of the drained worker node.

Conclusion

In this tutorial, we've covered the process of creating a Docker Swarm manager, adding a worker node, assigning a task to the worker node, and removing the worker node from the Swarm. Docker Swarm provides a powerful platform for orchestrating containerized applications, enabling efficient management of resources and tasks across a cluster of nodes.