Govur University Logo
--> --> --> -->
...

Before performing maintenance on a Docker Swarm worker node, what command should be executed to gracefully prevent new tasks from being scheduled on it and move existing tasks elsewhere?



The command that should be executed before performing maintenance on a Docker Swarm worker node to gracefully prevent new tasks from being scheduled on it and move existing tasks elsewhere is `docker node update --availability drain <NODE_ID_OR_HOSTNAME>`. Docker Swarm is a native clustering and orchestration solution that allows you to manage a cluster of Docker engines called a Swarm, where services are deployed and scaled across multiple nodes. A worker node is a machine within this Swarm that runs the application containers, known as tasks, scheduled by the manager nodes. Maintenance refers to activities like operating system updates or hardware replacement that require a node to be temporarily taken out of service. The `docker node update` command is used to modify the properties of a specific node in the Swarm. The `--availability drain` flag sets the availability status of the target node to "drain". When a node's availability is set to `drain`, the Swarm manager automatically stops scheduling any new tasks onto that node, effectively preventing new containers from starting there. Concurrently, it gracefully shuts down all existing tasks (containers) that are currently running on the draining node. For services configured with multiple replicas, the Swarm manager then reschedules these stopped tasks onto other available and healthy `active` worker nodes within the Swarm. This process ensures that the application's services continue to operate without interruption while the target node is prepared for maintenance, demonstrating a graceful transition. The placeholder `<NODE_ID_OR_HOSTNAME>` must be replaced with the actual ID or hostname of the worker node, which can be identified using the `docker node ls` command. For example, to drain a node named `worker-01`, the command would be `docker node update --availability drain worker-01`. After the maintenance is complete, the node's availability must be reset to `active` using `docker node update --availability active <NODE_ID_OR_HOSTNAME>` to allow it to resume accepting and running tasks.