How to use docker node demote command to demote a swarm manager

DockerDockerBeginner
Practice Now

Introduction

In this lab, we will explore how to manage the roles of nodes within a Docker Swarm. Specifically, we will focus on using the docker node demote command to change a manager node's role to that of a worker.

The lab will guide you through the process of initializing a Docker Swarm, identifying the current manager nodes, executing the demotion command, and finally verifying the node's updated role to confirm the successful demotion. This hands-on experience will provide practical knowledge of Docker Swarm node management.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/SystemManagementGroup(["System Management"]) docker/ContainerOperationsGroup -.-> docker/ls("List Containers") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/exec("Execute Command in Container") docker/SystemManagementGroup -.-> docker/version("Show Docker Version") subgraph Lab Skills docker/ls -.-> lab-555180{{"How to use docker node demote command to demote a swarm manager"}} docker/ps -.-> lab-555180{{"How to use docker node demote command to demote a swarm manager"}} docker/exec -.-> lab-555180{{"How to use docker node demote command to demote a swarm manager"}} docker/version -.-> lab-555180{{"How to use docker node demote command to demote a swarm manager"}} end

Initialize a Docker Swarm

In this step, we will initialize a Docker Swarm. A Docker Swarm is a group of machines running Docker and joined into a cluster. After joining a Swarm, you can continue to run Docker commands you're used to, but they are now executed by a Swarm manager. The machines in a Swarm can be either managers or workers. Managers handle cluster management tasks, while workers execute the services.

Before initializing the swarm, let's check the current Docker version.

docker version

You should see output similar to this, indicating the Docker version installed on the LabEx VM:

Client: Docker Engine - Community
 Version:           20.10.21
 API version:       1.41
 Go version:        go1.16.20
 Git commit:        baeda1f
 Built:             Tue Oct 25 18:01:18 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.21
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.20
  Git commit:       363bd3c
  Built:            Tue Oct 25 17:59:50 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.10
  GitCommit:        b4bd5d2b3d85c5e9b15588d67616e19a2a3a495d
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Now, let's initialize the Docker Swarm on this machine. Since this is the first node in the swarm, it will automatically become a manager node. We will use the docker swarm init command.

docker swarm init

You should see output indicating that the swarm has been initialized and providing a command to join other nodes as workers. The output will look similar to this:

Swarm initialized: current node (xxxxxxxxxxxx) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 172.17.0.2:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

The output confirms that the swarm is initialized and the current node is a manager. The xxxxxxxxxxxx will be replaced by the actual node ID.

List swarm nodes to identify managers

In this step, we will list the nodes in the Docker Swarm to identify which nodes are managers and which are workers. Since we just initialized the swarm with a single node, we expect to see only one node listed, and its role should be "Manager".

To list the nodes in the swarm, we use the docker node ls command.

docker node ls

You should see output similar to this:

ID                            HOSTNAME            STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
xxxxxxxxxxxx *                labex-vm            Ready     Active         Leader           20.10.21

Let's break down the output:

  • ID: The unique ID of the node. The asterisk (*) next to the ID indicates the current node you are running the command on.
  • HOSTNAME: The hostname of the node. In this case, it's labex-vm.
  • STATUS: The status of the node. Ready means the node is healthy and ready to accept tasks.
  • AVAILABILITY: Indicates if the node is available to schedule tasks. Active means it is available.
  • MANAGER STATUS: Shows the role of the node in the swarm. Leader indicates that this node is the primary manager in the swarm. If there were other managers, they would show Reachable. Worker nodes would have this field empty.
  • ENGINE VERSION: The version of the Docker Engine running on the node.

As expected, we see our single node listed, and its MANAGER STATUS is Leader, confirming it is a manager node.

Demote a manager node

In this step, we will demote the current manager node to a worker node. Demoting a manager node means changing its role from managing the swarm to simply executing tasks as a worker. This is useful in scenarios where you need to reduce the number of managers or change the role of a specific node.

To demote a manager node, we use the docker node demote command followed by the node ID or hostname. From the previous step, we know the hostname is labex-vm.

docker node demote labex-vm

You should see output confirming the demotion:

Node labex-vm was demoted from a manager to a worker.

This output indicates that the node labex-vm has successfully been demoted from a manager to a worker role within the swarm.

Verify the node's role after demotion

In this final step, we will verify that the node's role has been successfully changed from manager to worker after the demotion operation. We will again use the docker node ls command to list the nodes in the swarm and check the MANAGER STATUS column for our node.

docker node ls

After running the command, you should see output similar to this:

ID                            HOSTNAME            STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
xxxxxxxxxxxx                  labex-vm            Ready     Active                          20.10.21

Notice that the MANAGER STATUS column for the labex-vm node is now empty. This indicates that the node is no longer a manager and is now acting as a worker node in the swarm. The asterisk (*) is still next to the ID because this is the node where you are running the command, but its role has changed.

This confirms that the demotion was successful.

Summary

In this lab, we learned how to manage Docker Swarm nodes by initializing a swarm and identifying manager nodes. We then practiced using the docker node demote command to change a manager node's role to a worker. Finally, we verified the successful demotion by listing the swarm nodes and observing the updated role.