Skip to content

Instantly share code, notes, and snippets.

@mkfares
Created August 14, 2020 15:31
Show Gist options
  • Save mkfares/b0f8f6b13e5e5fdc22cf55aff29784d1 to your computer and use it in GitHub Desktop.
Save mkfares/b0f8f6b13e5e5fdc22cf55aff29784d1 to your computer and use it in GitHub Desktop.
Docker Swarm - Managing Nodes

Docker Swarm - Managing Nodes

Nodes are physical or virtual machines on which an instance of docker engine is installed. Nodes are the building block of a docker swarm.

Nodes are of two types: managers and workers. Managers receive configuration of an application then schedule and monitor the tasks to be executed on the worker nodes. The managers maintain the desired state of a swarm by comparing the current state with the defined state. To orchestrate the tasks in a swarm, managers choose a leader among themselves.

Worker nodes execute tasks/containers assigned by managers. By default, manager nodes are also worker nodes unless they are configured to run as manager-only nodes.The role of workers is alo to keep managers notified and updated about the status of the tasks they are running.

All the commands related to the swarm management should be run on manager nodes.

Join a node to a swarm

A swarm may be extended by adding new nodes. To join a node to swarm, you need to specify the role of the node as a worker or manager.

The node should be on the same network as the swarm and use a token, which acts like a password, that is generated by a swarm manager.

To print the command to join a worker node to the swarm:

$ docker swarm join-token worker

To print the command to join a manager node to the swarm:

$ docker swarm join-token manager

An example of command generated by the above two command is:

$ docker swarm join --token SWMTKN-1-3vvhf8dxb9aq21y77pfr1vej6ysmqpl5n3nqmsuqhel573x5yd-axw4hei9bstwiy1tq853whvak 192.168.65.3:2377

List nodes

The ls command lists all nodes participating in a swarm.

$ docker node ls

The command output includes the node id, hostname, status, availability, manager or worker, and the version of the docker engine installed on the node.

The node with an asterisk (*) is the current docker daemon.

Inspect a node

The inspect command displays detailed information about a node.

$ docker node inspect self

The name of the current node is also called self.

The output of the inspect command is displayed in JSON format. The option --pretty prints the output in a human readable format.

A specific information may be displayed using the --format option.

$ docker node inspect --format '{{.Spec.Role}}' self

List tasks running on a node

The ps command lists the tasks running on a node. If no node is specified, the ps shows the tasks running on the current node.

$ docker node ps [options] [node-name]
$ docker service create -p 8080:80 --name web --replicas 2 nginx:latest
$ docker node ps

Update node information

Metadata about a node such as the availability, labels and role can be updated using the "update" command.

$ docker node update [option] node-name

Options:
--availability : Changes the state of the node to active, pause, or drain.
--label-add : Adds a node label in the form key=value
--label-rm : Removes a node label by specifying the key
--role : Changes the role of the node to either manager or worker.

To following command changes the availability of a node to drain.

$ docker node update --availability=drain docker-desktop
$ docker node ls

Nodes may be labeled with key-value pairs. These labels serve as constrains when deploying services on nodes.

$ docker node update --label-add type=web docker-desktop
$ docker node inspect --format '{{.Spec.Labels.type}}' docker-desktop

Labels may be removed from a node as follows:

$ docker node update --label-rm type docker-desktop
docker node inspect --format '{{.Spec.Labels.type}}' docker-desktop

The option --role changes the role of a node to either manager or worker (See sections about promote and demote a node)

$ docker node update --role worker docker-desktop
$ docker node inspect --format '{{.Spec.Role}}' docker-desktop

The node in a single-node swarm has to be a manager.

Drain a node

When performing periodic maintenance on a node, you need to transfer all tasks running on this node to other nodes. In addition, the node should not accept tasks from managers. To put a node in this state, you need to update the metadata of a node by setting the availability to 'drain'.

$ docker node update --availability drain docker-desktop
$ docker node ls

After running the above command, all tasks on this node are stopped and started on nodes with availability as "active".

To make a node active again:

$ docker node update --availability active docker-desktop

Promote a worker node to manager

Worker nodes can be promoted to manager nodes using the following command.

$ docker node promote <node-name>

Demote a manager node to worker

Manager nodes can be demoted to worker nodes using the following command:

$ docker node demote <node-name>

Remove a node from a swarm

The rm command removes a worker node from a swarm.

$ docker node rm <node-name>

Manager nodes need to be demoted to workers before they can be removed from the swarm.

The -f option forces the removal of a node that is not accessible or has been compromised.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment