Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 38 ยท Projects and Practice

Project: A Swarm Capacity Planner

Build a runnable capacity planner that answers how many nodes a Swarm needs, what happens when a node fails, and where reservations are too high, then apply it to real stacks.

"How many servers do we need?" is the question every Swarm owner faces. In this project you build a small planner that takes your services (replicas, CPU and memory reservations) and your nodes, then answers three questions: does everything fit, does it still fit when a node fails, and how much headroom is left?

The planner is written in Python and runs here. It is a stand-in model of the scheduler's reservation bookkeeping, not Docker; real placement also depends on constraints, preferences and current load. The point is to make the arithmetic visible before you spend money.

The model

Inputs:

  • Services: name, replicas, CPU reservation, memory reservation, optional max_replicas_per_node.
  • Nodes: name, CPUs, memory, and a reserved share for the operating system and the Docker daemon (say 10% CPU and 1 GB of memory).

The scheduler counts reservations, not actual usage (Resource Limits). So the planner does the same: it places tasks one by one on the node with the most free capacity that still fits.

flowchart TD
  I["Services + nodes"] --> P["Place every task"]
  P --> F{"All placed?"}
  F -->|no| X["Report pending tasks"]
  F -->|yes| N["Simulate each single node failure"]
  N --> R["Report headroom per node and risks"]

The planner answers "fits?", "survives one failure?" and "how much slack?".

Step 1: place tasks

Python Capacity planner, part 1: place the tasks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Step 2: survive a node failure

The question that matters in production is not "does it fit today?" but "does it fit while a node is down, being patched or being drained?" Re-run the placement once per node with that node removed. Tasks of a failed node are rescheduled onto the survivors, so the survivors must have room for them.

Python Capacity planner, part 2: single node failure

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Compare the two reports. With the number of nodes you tried first, the cluster may fit when healthy but leave tasks pending after a failure; an extra node typically removes that risk. That is the whole point of N+1 capacity: provision enough that losing any one node (or draining one for an upgrade) still places everything.

Step 3: read the result

Look for:

  • Pending tasks in the healthy case: you need more nodes or smaller reservations.
  • Pending tasks after a node loss: you have no failure tolerance; add capacity or reduce reservations.
  • Memory far more constrained than CPU (or the opposite): pick a different node shape, for example more RAM per CPU.
  • A max_replicas_per_node cap that makes a service unplaceable on a small cluster.
  • Reservations well above real usage: compare with measured data (docker stats, your monitoring). Over-reserving wastes money; under-reserving causes overload (Monitoring and Logging).

Extensions

  1. Add constraints: only role == worker nodes for the API, and a labelled node for the database.
  2. Count managers separately: three small managers that run no application tasks.
  3. Add a cost column (price per node) and search for the cheapest node count that survives one failure.
  4. Support two simultaneous failures for critical services.
  5. Feed the planner with real reservations parsed from docker service inspect output on your own cluster.

Common mistakes

  • Planning against average usage instead of reservations and peaks.
  • Ignoring the memory needed by the OS, Docker and monitoring agents.
  • Sizing for the healthy case only.
  • Forgetting that a rolling update with start-first needs temporary extra capacity (Rolling Updates).

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. Why does the planner subtract a share of CPU and memory from every node?

    Never plan to use 100% of a node.
  2. 2. What does N+1 capacity mean?

    The cluster must still place all tasks while one node is down or drained.
  3. 3. A rolling update uses start-first. What does that mean for capacity planning?

    New tasks start before old ones stop, so peak usage is higher temporarily.

Summary

  • Capacity planning for Swarm is reservation arithmetic: place every task, then repeat with each node removed.
  • Plan for N+1, leave room for the OS, and add headroom for rolling updates.
  • Compare reservations with measured usage and adjust both over time.
  • A stand-in model is a thinking tool: confirm real placement with docker service ps on your cluster.

Next: Swarm Practice Questions and Exercises