TExcelTAD
Home › Docker Swarm › Managing Nodes

Managing Nodes

Once the swarm exists, nodes come and go: you patch them, replace them, add bigger ones, retire old ones, or reserve some for particular jobs. Everything happens through docker node commands on a manager. This lesson covers reading docker node ls, the three availability states, promoting and demoting, labels, and the important fact that Swarm does not rebalance on its own.

Reading docker node ls

bash
$ docker node ls
ID                            HOSTNAME    STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
k3x9v2m1q8w7e6r5t4y3u2i1o *   manager-1   Ready     Drain          Leader           29.0.1
m8n7b6v5c4x3z2l1k0j9h8g7f     manager-2   Ready     Drain          Reachable        29.0.1
q1w2e3r4t5y6u7i8o9p0a1s2d     manager-3   Down      Drain          Unreachable      29.0.1
z9x8c7v6b5n4m3l2k1j0h9g8f     worker-1    Ready     Active                          29.0.1
a1b2c3d4e5f6g7h8i9j0k1l2m     worker-2    Ready     Pause                           29.0.1

(sample output)

Column Values Meaning
STATUS Ready, Down, Unknown, Disconnected is the node's agent talking to the managers? Set by the swarm from heartbeats.
AVAILABILITY Active, Pause, Drain may the scheduler use this node? You set this.
MANAGER STATUS Leader, Reachable, Unreachable, empty Raft role; empty means worker

STATUS and AVAILABILITY answer different questions. STATUS is a fact (the node is down); AVAILABILITY is your instruction (do not put work here). In the sample, manager-3 is down and therefore Unreachable in Raft: with three managers, the cluster still has quorum, but one more failure would freeze it.

Availability: Active, Pause, Drain

bash
$ docker node update --availability drain worker-1
worker-1
$ docker node update --availability active worker-1
worker-1
Availability New tasks placed here Existing tasks
active yes keep running
pause no keep running
drain no shut down and rescheduled on other nodes (global-service tasks are just stopped)

Drain is the tool for maintenance: drain a node, wait until its tasks are running elsewhere, patch and reboot it, set it back to active. It is also the standard way to keep managers free of application containers: docker node update --availability drain manager-1 leaves the manager fully working as a manager while the scheduler stops sending it tasks.

Pause is useful when you suspect a node is misbehaving and want to stop it attracting new work without disrupting what runs there.

No automatic rebalancing

When a node comes back (after a reboot, or after you set it back to active), Swarm does not move existing tasks to it. Moving a healthy container means stopping it, which is a disruption, and the scheduler only acts when a task needs a home. The returning node gets new tasks as they are created (scaling, updates, failures elsewhere). To force a rebalance, recreate the service's tasks with a rolling update:

bash
$ docker service update --force web

--force performs an update even though nothing in the spec changed, so every task is replaced one batch at a time and the scheduler spreads the replacements over all available nodes. The model shows the whole cycle:

python · try it yourself
nodes = {"worker-1": "active", "worker-2": "active", "worker-3": "active"}
tasks = {"web.1": "worker-1", "web.2": "worker-2", "web.3": "worker-3",
         "web.4": "worker-1", "web.5": "worker-2", "web.6": "worker-3"}

def least_loaded():
    usable = [n for n, a in nodes.items() if a == "active"]
    return min(usable, key=lambda n: (list(tasks.values()).count(n), n))

def show(label):
    layout = {n: sorted(t for t, at in tasks.items() if at == n) for n in nodes}
    print(f"{label:<26}", {n: len(ts) for n, ts in layout.items()})

def set_availability(node, value):
    nodes[node] = value
    if value == "drain":
        for task, at in sorted(tasks.items()):
            if at == node:
                tasks[task] = least_loaded()      # replacement task, same slot
    show(f"{node} -> {value}")

def force_update():
    for task in sorted(tasks):                    # one at a time (parallelism 1)
        del tasks[task]
        tasks[task] = least_loaded()
    show("service update --force")

show("start")
set_availability("worker-3", "drain")
set_availability("worker-3", "active")
force_update()
start                      {'worker-1': 2, 'worker-2': 2, 'worker-3': 2}
worker-3 -> drain          {'worker-1': 3, 'worker-2': 3, 'worker-3': 0}
worker-3 -> active         {'worker-1': 3, 'worker-2': 3, 'worker-3': 0}
service update --force     {'worker-1': 2, 'worker-2': 2, 'worker-3': 2}

After the drain, worker-3 is empty and the other two carry three tasks each. Setting it back to active changes nothing until the forced update replaces the tasks and the spread evens out again.

Tip: --force restarts every task, so do it like any rolling update: with healthchecks and a sensible update_config (see Rolling Updates). For a large cluster it is often fine to just let new deploys rebalance over time.

Rolling maintenance

Patching every node one after the other is drain → patch → activate in a loop. Before draining, check that the other nodes have room for what is being moved, otherwise tasks stay pending (Reservations & Limits). A model of the capacity during such a round:

python · try it yourself
capacity = {"worker-1": 4, "worker-2": 4, "worker-3": 4}   # tasks each node can hold
replicas = 9

for node in capacity:
    usable = sum(c for n, c in capacity.items() if n != node)
    placed = min(replicas, usable)
    pending = replicas - placed
    state = "ok" if pending == 0 else f"{pending} task(s) pending"
    print(f"draining {node}: room for {usable} on the others -> {placed}/{replicas} running, {state}")

capacity["worker-4"] = 4
print("after adding worker-4:")
for node in capacity:
    usable = sum(c for n, c in capacity.items() if n != node)
    print(f"  draining {node}: {min(replicas, usable)}/{replicas} running")
draining worker-1: room for 8 on the others -> 8/9 running, 1 task(s) pending
draining worker-2: room for 8 on the others -> 8/9 running, 1 task(s) pending
draining worker-3: room for 8 on the others -> 8/9 running, 1 task(s) pending
after adding worker-4:
  draining worker-1: 9/9 running
  draining worker-2: 9/9 running
  draining worker-3: 9/9 running
  draining worker-4: 9/9 running

With three nodes of four slots, draining any one of them leaves room for only eight of nine tasks. The usual rule is N+1: keep enough spare capacity to lose (or drain) your largest node.

Promote and demote

bash
$ docker node promote worker-1      # becomes a manager (Reachable)
$ docker node demote manager-3      # becomes a worker; leaves the Raft group cleanly

Promotion and demotion change the quorum math, so keep the count odd. To replace a failed manager, promote a healthy node first and then remove the dead one (docker node demote + docker node rm --force), so the cluster never drops below the number of managers it needs. A swarm refuses to demote the last manager.

Inspecting a node

bash
$ docker node inspect --pretty worker-1
ID:                     z9x8c7v6b5n4m3l2k1j0h9g8f
Labels:
 - zone=eu-1a
 - disk=ssd
Hostname:               worker-1
Joined at:              2026-10-12 08:14:51.32 +0000 utc
Status:
 State:                 Ready
 Availability:          Active
 Address:               10.0.0.21
Platform:
 Operating System:      linux
 Architecture:          x86_64
Resources:
 CPUs:                  4
 Memory:                7.756GiB
Plugins:
 Log:                   awslogs, fluentd, gcplogs, gelf, journald, json-file, local, splunk, syslog
 Network:               bridge, host, ipvlan, macvlan, null, overlay
 Volume:                local
Engine Version:         29.0.1
TLS Info:
 ...

(sample output, trimmed)

docker node ps worker-1 lists the tasks on a node (docker node ps alone means the current node), which is the quickest way to see what a drain is about to move.

Labels

Nodes can carry labels that you set from a manager. They are the basis of Placement Constraints:

bash
$ docker node update --label-add zone=eu-1a --label-add disk=ssd worker-1
$ docker node update --label-rm disk worker-1

These node labels live in the swarm's state, so only managers can change them, and a compromised worker cannot relabel itself to attract sensitive workloads. Docker also has engine labels set in a node's own daemon.json; they can be used in constraints as engine.labels.*, but because the node controls them they are weaker for security-relevant placement. Prefer node labels.

Removing a node

A node that will not come back is removed in two steps: on the node, docker swarm leave (if it is still reachable), then on a manager docker node rm <node> (with --force if it is down). Until it is removed, a dead node stays in the list as Down. Its tasks were already rescheduled when it went down.

Learn more: Placement Constraints · High Availability · Kubernetes scheduling

Test yourself

What does docker node update --availability drain worker-2 do to tasks already running on worker-2?

  1. Nothing, they keep running
  2. They are shut down and replacement tasks are scheduled on other nodes
  3. They are paused
  4. They are moved together with their memory to another node

A node returns after a reboot. When do tasks start running on it again?

  1. Immediately: Swarm rebalances automatically
  2. Only when new tasks need a home (scaling, updates, failures) or after docker service update --force
  3. Never
  4. After the next leader election

Why prefer node labels (docker node update --label-add) over engine labels for placing sensitive services?

  1. They are faster
  2. Only managers can set node labels, while engine labels are configured on the node itself
  3. Engine labels are deprecated
  4. Node labels are encrypted