How to use docker node update command to manage swarm nodes

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to manage Docker Swarm nodes using the docker node update command. You will explore various functionalities of this command, including updating node availability to control task placement, adding and removing labels to categorize nodes for task scheduling constraints, and changing a node's role within the swarm.

Through hands-on exercises, you will practice pausing a node to prevent new tasks, adding single and multiple labels to nodes, removing existing labels, and promoting a worker node to a manager role. These steps will provide you with practical experience in managing the state and attributes of nodes in a Docker Swarm cluster.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker/ContainerOperationsGroup -.-> docker/ls("List Containers") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/exec("Execute Command in Container") docker/ContainerOperationsGroup -.-> docker/inspect("Inspect Container") subgraph Lab Skills docker/ls -.-> lab-555186{{"How to use docker node update command to manage swarm nodes"}} docker/ps -.-> lab-555186{{"How to use docker node update command to manage swarm nodes"}} docker/exec -.-> lab-555186{{"How to use docker node update command to manage swarm nodes"}} docker/inspect -.-> lab-555186{{"How to use docker node update command to manage swarm nodes"}} end

Update node availability to pause

In this step, we will learn how to update the availability of a node in a Docker Swarm. Node availability determines whether a node can receive new tasks. The possible availability states are active, pause, and drain.

  • active: The node can receive new tasks. This is the default state.
  • pause: The node will not receive new tasks, but existing tasks will continue to run.
  • drain: The node will not receive new tasks, and existing tasks will be stopped and rescheduled on other nodes.

We will change the availability of the current node to pause. First, we need to find the ID of the current node. We can use the docker node ls command to list the nodes in the swarm.

docker node ls

The output will show a list of nodes, including their ID, hostname, status, availability, and manager status. Identify the ID of the node where you are currently working.

Now, use the docker node update command with the --availability pause flag and the node ID to change the node's availability. Replace <node_id> with the actual ID of your node.

docker node update --availability pause <node_id>

After executing the command, you can verify the change by running docker node ls again. The availability of your node should now be pause.

docker node ls

Add a label to a node

In this step, we will learn how to add a label to a node in a Docker Swarm. Labels are key-value pairs that you can attach to nodes. They are useful for organizing and selecting nodes based on specific criteria, which can be used for task placement constraints.

We will add a label named env with the value dev to the current node. First, make sure you have the node ID from the previous step. If not, you can get it again using docker node ls.

docker node ls

Now, use the docker node update command with the --label-add flag, the label in the format key=value, and the node ID. Replace <node_id> with the actual ID of your node.

docker node update --label-add env=dev <node_id>

After adding the label, you can inspect the node to see the added label. Use the docker node inspect command followed by the node ID.

docker node inspect <node_id>

Look for the Labels section in the output. You should see the env: dev label listed there.

Add multiple labels to a node

In this step, we will learn how to add multiple labels to a node simultaneously. You can add multiple labels by repeating the --label-add flag for each label you want to add.

We will add two new labels to the current node: tier with the value frontend and region with the value us-east. Make sure you have the node ID from the previous steps.

Use the docker node update command with multiple --label-add flags and the node ID. Replace <node_id> with the actual ID of your node.

docker node update --label-add tier=frontend --label-add region=us-east <node_id>

After adding the labels, inspect the node again to verify that all labels have been added correctly.

docker node inspect <node_id>

In the Labels section of the output, you should now see env: dev, tier: frontend, and region: us-east.

Remove a label from a node

In this step, we will learn how to remove a label from a node in a Docker Swarm. You can remove a label using the docker node update command with the --label-rm flag followed by the label key.

We will remove the region label that we added in the previous step. Make sure you have the node ID.

Use the docker node update command with the --label-rm flag and the label key (region), followed by the node ID. Replace <node_id> with the actual ID of your node.

docker node update --label-rm region <node_id>

After removing the label, inspect the node again to verify that the label has been removed.

docker node inspect <node_id>

In the Labels section of the output, you should now see env: dev and tier: frontend, but the region: us-east label should be gone.

Update node role to manager

In this step, we will learn how to update the role of a node in a Docker Swarm. A node can have one of two roles: manager or worker. Manager nodes handle swarm management tasks, while worker nodes run services.

We will promote the current node to a manager role. Make sure you have the node ID.

Use the docker node update command with the --role manager flag and the node ID. Replace <node_id> with the actual ID of your node.

docker node update --role manager <node_id>

After updating the role, verify the change by listing the nodes.

docker node ls

The output should now show the role of your node as Manager.

Summary

In this lab, we learned how to manage Docker Swarm nodes using the docker node update command. We covered several key operations, including updating a node's availability to pause to prevent it from receiving new tasks while allowing existing tasks to continue running.

Furthermore, we explored how to add and remove labels from nodes. We learned to add a single label using --label-add key=value and multiple labels simultaneously. We also practiced removing a specific label using --label-rm key. Finally, we demonstrated how to change a node's role to manager, highlighting the flexibility of the docker node update command in managing the state and configuration of nodes within a Docker Swarm.