Feedback

Chat Icon

Painless Docker - 2nd Edition

A Comprehensive Guide to Mastering Docker and its Ecosystem

Understanding How Docker Swarm Works
85%

Performing Operations on Nodes

Sometimes, you may need to perform certain operations on a node, such as:

  • Pausing a node
  • Draining a node
  • Removing a node
  • Promoting a node
  • Demoting a node

Let's explore how these operations can be performed and what they entail.

Pausing a Node

When a node is paused, Docker Swarm will halt the scheduling of new tasks on that node and instead reschedule any future tasks on other nodes.

To pause a node, execute the following command on the manager node:

# Export the hostname of the node you want to pause as an environment variable
# Change this to the hostname of the node you want to pause
export NODE_HOSTNAME=worker01

# Pause the node
docker node update --availability pause $NODE_HOSTNAME

Then launch a new service and see that the tasks aren't scheduled on the paused node.

docker service create \
    --name webserver-test \
    --replicas 5 \
    -p 8081:80 \
    nginx:1.29.5

Check where the tasks are running using the following command:

docker service ps webserver-test | \
    grep -i "Running" | \
    awk '{print $4}'

To check the status of the node, run the following command on the manager node:

docker node ls

You should see that the node is paused (AVAILABILITY column).

To resume the node, run the following command on the manager node:

docker node update --availability active $NODE_HOSTNAME

Try scaling the service and see that the tasks are scheduled on the node again.

# Scale the service to refresh the tasks 
docker service scale webserver-test=10

# Check where the tasks are running
docker service ps webserver-test | \
    grep -i "Running" | \
    awk '{print $4}'

Draining a Node

Draining a node in Docker Swarm means that new tasks will no longer be scheduled on the node. Additionally, any existing tasks on the node will be rescheduled to other nodes.

To drain a node, you need to run the following command on the manager node:

# Export the hostname of the node you want to drain as an environment variable
export NODE_HOSTNAME=worker01

# Drain the node
docker node update --availability drain $NODE_HOSTNAME

Now you can check the status of the node using the following command:

docker node ls

Check that all the tasks are running on all nodes except the drained node:

# Export the IDs of all services as an environment variable
services_ids=

Painless Docker - 2nd Edition

A Comprehensive Guide to Mastering Docker and its Ecosystem

Enroll now to unlock all content and receive all future updates for free.

Unlock now  $31.99$25.59

Hurry! This limited time offer ends in:

To redeem this offer, copy the coupon code below and apply it at checkout:

Learn More