The course is part of these learning paths
Docker has made great strides in advancing development and operational agility, portability, and cost savings by leveraging containers. You can see a lot of benefits even when you use a single Docker host. But when container applications reach a certain level of complexity or scale, you need to make use of several machines. Container orchestration products and tools allow you to manage multiple container hosts in concert. Docker swarm mode is one such tool. In this course, we’ll explain the architecture of Docker swarm mode, and go through lots of demos to perfect your swarm mode skills.
Learning Objectives
After completing this course, you will be able to:
- Describe what Docker swarm mode can accomplish.
- Explain the architecture of a swarm mode cluster.
- Use the Docker CLI to manage nodes in a swarm mode cluster.
- Use the Docker CLI to manage services in a swarm mode cluster.
- Deploy multi-service applications to a swarm using stacks.
Intended Audience
This course is for anyone interested in orchestrating distributed systems at any scale. This includes:
- DevOps Engineers
- Site Reliability Engineers
- Cloud Engineers
- Software Engineers
Prerequisites
This is an intermediate-level course that assumes:
- You have experience working with Docker and Docker Compose
We now have a 3-node swarm with one manager. This lesson will demonstrate how to perform swarm node management tasks. For example, promoting a worker to a manager, organizing nodes with labels, and preventing manager's from doing work.
Agenda
I'll give a brief overview of the swarm node management tasks we'll be going through. We'll spend most of the time at the command-line where we'll execute the tasks on the swarm we stood up.
Node Management
Promoting
The first task that we'll go through is promoting a worker node to a manager. You may want to do this to increase your fault-tolerance or in order to take an existing manager out of service without impacting the number of managers available. Remember that if you are going for an increase in fault-tolerance that you should increase the manager count up to the next odd number. For example, going from one manager to three.
Demoting
Demoting is the opposite of promoting. It takes a node that is currently in a manager role and demotes the node to a worker role.
Availability
The availability of a node refers to the ability to schedule tasks to the node. It isn't whether a node is up or down which one might logically guess. The availability of a node is configured by managers. The allowed availability states are: active, pause, and drain. Active means that work can be scheduled on a node, pause means no new work can be scheduled but existing work scheduled on the node won't be canceled, and drain means nothing can be scheduled and any running work is terminated. Setting availability to drain is useful for gracefully taking a node offline to perform maintenance. Draining managers is also useful to prevent them from having work scheduled to them, which is the default behavior.
Labeling
The final node management task that we'll demonstrate is labeling. Recall that labels are useful for influencing where services are placed in a swarm. For example, labels can be used to ensure that tasks are scheduled in different availability zones to provide service availability SLAs.
Demo
Now I'll hop over to the command-line and start with the demo
I'm connected into the vm1 swarm node which is currently the manager of the swarm. In docker, node management tasks are accomplished by using the docker node management command.
$ docker node --help
There are some standard commands that are available for most docker management commands: namely ls for listing nodes, ps for listing tasks scheduled to nodes, and rm for removing nodes from a swarm. Inspect is also a familiar docker command that lists detailed information about a node. To get a view of the swarm, I'll run the ls command
$ docker node ls
And we see the three nodes, that all three are available, and that only vm1 is a manager, and is therefore the leader.
To promote vm2 to the manager role in the swarm, I'll use the promote command:
$ docker node promote vm2
And easy as that vm2 is now a manager in the cluster
$ docker node ls
The manager status of reachable means the node is a manager and is participating in the Raft consensus quorum. The other possible manager status is unavailable, which indicates the manager has a problem communicating with the other managers.
To change vm2 back to the worker role, I'll use the demote command:
$ docker node demote vm2
$ docker node ls
Next up is modifying the availability of a node. You can use the update command for that
$ docker node update --help
The availability option does what we want. You can also see the label-add and label-rm options which add and remove labels from nodes. There's also the role option which promote and demote are a short form of updating a node to the role of either worker or manager. Let's say we don't want the manager to have any tasks scheduled to it, so I'll set the availability of vm1 to drain
$ docker node update --availability drain vm1
$ docker node ls
and the availability in the ls table reflects the change.
I actually want the manager to be able run tasks so I'll undo that by setting availability to active
$ docker node update --availability active vm1
To finish up I'll add a fictitious availability zone labels to each node using the label-add update option. I'll say vm1 is in zone 1, vm2 is in zone 2, and vm3 is in zone 3:
$ docker node update --label-add zone=1 vm1
$ docker node update --label-add zone=2 vm2
$ docker node update --label-add zone=3 vm3
To see node labels, you need to use the inspect command
$ docker node inspect vm3
Here is the labels property in the Spec. You can also filter out everything but the labels by using the format option with a Go template
$ docker node inspect -f '{{.Spec.Labels}}' vm3
Here again is the zone label key-value pair in the Labels map.
Recap
In this lesson we learned about the node management tasks that are part of managing a swarm. We understood the concepts and demonstrated how to promote and demote a node, set a node's availability, and label swarm nodes.
This slide shows the current state of our swarm. Each node is now labeled with a zone compared to where we began the lesson.
Closing
In the next lesson, we'll see how to schedule tasks onto the swarm by using services. If you are ready to see swarm mode in action, continue on to the next Lesson.
Logan has been involved in software development and research since 2007 and has been in the cloud since 2012. He is an AWS Certified DevOps Engineer - Professional, AWS Certified Solutions Architect - Professional, Microsoft Certified Azure Solutions Architect Expert, MCSE: Cloud Platform and Infrastructure, Google Cloud Certified Associate Cloud Engineer, Certified Kubernetes Security Specialist (CKS), Certified Kubernetes Administrator (CKA), Certified Kubernetes Application Developer (CKAD), and Certified OpenStack Administrator (COA). He earned his Ph.D. studying design automation and enjoys all things tech.