Course contents

14. Cloud and Infrastructure Lesson 4 of 8

Container Orchestration

See a scheduler fit pods by requests, packing versus spreading, self-healing after a node loss, and rolling, blue-green and canary releases of a broken build.

17 minIntermediate8 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • Schedulers place pods by requests against allocatable capacity, not by live usage.
  • Free capacity scattered over nodes is fragmentation: a pod fits only if one node has room for it.
  • Self-healing is a control loop: recreate what is missing wherever it still fits.
  • Rolling updates only check readiness; blue-green switches all at once; canaries expose a small share first.
  • Promote canaries in stages gated by metrics, and deploy dependencies before the code that needs them.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

Container orchestration is the automation that decides where containers run across a fleet of machines, keeps the right number of them running, and replaces them with new versions safely. Kubernetes is the most widely used orchestrator; Nomad, Amazon ECS and others solve the same problems. This lesson looks at the three jobs every orchestrator does: scheduling, self-healing and rollouts.

Why it exists

On one host you can start containers by hand. On fifty hosts with hundreds of services, someone has to decide which machine has room for which container, notice when a machine dies and restart its work elsewhere, and roll out releases several times a day without outages. Doing that by hand is slow and error-prone; an orchestrator does it continuously from a declared desired state.

How it works

Requests and limits. Each container declares requests (the CPU and memory it is guaranteed) and usually limits (the most it may use). CPU above the limit is throttled; memory above the limit gets the container OOM-killed. The scheduler places pods by comparing requests with each node's allocatable capacity, not with live usage.

Scheduling. For each pending pod the scheduler filters nodes that can host it (enough free requests, matching node selectors and affinity, tolerated taints, topology spread rules), then scores the survivors and binds the pod to the best one. Scoring strategies differ: least allocated spreads pods onto emptier nodes; most allocated packs them onto fuller ones (bin packing). A pod that fits on no node stays Pending, even if the cluster has enough free capacity in total spread across nodes (fragmentation). A cluster autoscaler can add nodes for Pending pods and remove nodes that packing has emptied. Bound pods are not moved again by the scheduler; tools such as the descheduler can evict pods so they are placed again.

Self-healing. Controllers compare desired state (a Deployment says 3 replicas) with actual state (2 ready pods) and act to close the gap: create a pod, which the scheduler then places. Container crashes are restarted locally by the node agent; node failures lead to replacement pods on other nodes once the failed node's pods are evicted.

Rollout strategies.

  • Rolling update: replace pods a few at a time. maxSurge (extra pods allowed) and maxUnavailable (pods that may be missing) set the pace. Cheap and gradual, but the only gate is readiness, so a release that is "ready" but wrong spreads to everyone.
  • Blue-green: run the new version (green) as a complete second environment next to the old (blue), then switch all traffic at once. Rollback is switching back. Needs double capacity during the release and exposes all users at the switch.
  • Canary: send a small share of traffic (by weight, header or user cohort) to the new version, compare its error rate and latency with the stable version, and either increase the share in stages or abort. Blast radius equals the canary's share; needs good metrics and automation.

Traffic shares come from replica counts (crude), weighted load balancing, an ingress or Gateway API route with weights, or a service mesh. Progressive delivery controllers (Argo Rollouts, Flagger) automate the shift-wait-analyse loop.

When to use it

  • More services or hosts than you can place and restart by hand.
  • Frequent releases that must not cause downtime.
  • Mixed workloads (web, batch, jobs) that should share machines efficiently.
  • Canary releases for anything where a bad release is expensive: payments, sign-in, core APIs.

When not to

  • One or two services on a couple of machines: a managed platform or simple VMs are easier.
  • Canary analysis without trustworthy metrics: a canary nobody watches is a slow rolling update.
  • Blue-green for stateful systems whose data cannot be shared or switched cleanly between two environments.

Trade-offs

Choice You gain You pay
Packing (most allocated) Fewer nodes, less fragmentation, room for big pods Larger blast radius per node, more contention
Spreading (least allocated) Headroom everywhere, smaller blast radius More nodes, fragmented free capacity
Rolling update No extra environment, gradual Readiness is the only gate; old and new run side by side
Blue-green Instant switch and rollback Double capacity; all users exposed at once
Canary Small blast radius, data-driven decisions Slower releases, needs metrics, routing and automation

Alternatives

  • Recreate: stop everything, start the new version. Simple, with downtime; acceptable for some internal or batch systems.
  • Feature flags: deploy code dark and release features separately from deploys; complements any rollout strategy.
  • Shadow (mirrored) traffic: send a copy of real requests to the new version and discard its responses; tests behaviour without user impact, but side effects need care.
  • Managed platforms with built-in traffic splitting (Cloud Run revisions, App Service slots).

Failure modes

  • Pending pods: requests too large for any node, or no headroom left after a node failure.
  • Requests too low: nodes are overcommitted and pods compete for CPU or are OOM-killed under load.
  • CPU limits too low: throttling and latency spikes while the node is idle.
  • Shallow readiness: a broken release passes readiness and a rolling update ships it to everyone.
  • Dependency order: new code needs a schema, API or config that is not deployed yet.
  • Canary too small or too short: not enough requests to see a difference; errors in rare paths slip through.
  • Blue-green with shared state: both versions write to the same database with incompatible schemas.

In production

Set requests from measured usage and memory limits close to them; be cautious with CPU limits. Keep spare capacity (or a cluster autoscaler) so replacements are not stuck Pending, and spread replicas across nodes and zones. Use readiness probes that reflect real ability to serve, PodDisruptionBudgets for maintenance, and canaries with automated analysis on error rate and latency for important services. Make schema and API changes backwards compatible (expand, migrate, contract), and deploy dependencies before the code that uses them.

Common misconceptions

  • "The scheduler looks at CPU usage." It looks at requests and allocatable capacity.
  • "Enough free CPU in the cluster means the pod will run." It must fit on one node.
  • "A rolling update is safe because it is gradual." It is gradual, but it only stops for readiness failures.
  • "Blue-green is always safer than canary." It rolls back faster but exposes every user the moment you switch.
  • "Restarting the pod fixes a bad release." A restart runs the same image with the same missing dependency.

Interview angle

Explain how a scheduler places work (requests, filter, score) and why a pod can be Pending in a half-empty cluster. Describe self-healing as reconciliation. Compare rolling, blue-green and canary on blast radius, rollback time and cost, and say which you would use for a payment service and why (canary with automated analysis, plus backwards-compatible schema changes). Being able to compute a canary's traffic share from weights, and to say what metric would abort it, is a strong signal.

Model assumptions

This lesson uses a simplified model, not a benchmark. Web pods are simulated servers with 4 workers and a 50 ms mean service time (lognormal, cv 0.35), about 80 req/s each; traffic is Poisson. The Service uses smooth weighted round robin; its health check stands for readiness (every 250 ms, 2 failures remove, 2 successes add). The broken release is modelled as v2 calling a dependency that is down, so every request it serves fails while its readiness probe passes. The scheduler, the batch and ml pods and the CPU arithmetic are a scripted illustration, not a simulated scheduler. Pod starts, node loss, drains and weight changes are timed actions on a compressed timeline. Utilisation is the fraction of busy workers.

The lesson as textEvery step of the simulation, written out.
  1. 1. The scheduler fits pods by their requests

    Every container declares requests (what it is guaranteed, used for placement) and usually limits (the most it may use: CPU above the limit is throttled, memory above it is OOM-killed). The scheduler only looks at requests and each node’s allocatable capacity, not at live usage.

    Pods arrive one by one: web a, b, c (1 CPU each), a batch pod (1 CPU), then an ml job asking for 3 CPU. The default scoring prefers the emptier node, so pods alternate between Node A and Node B. When the ml job arrives, each node has 2 CPU free: 4 free in total, but no node fits 3. It stays Pending (red). Meanwhile the three web pods serve 120 req/s.

    Scene: The scheduler places web pods a and c on Node A, web pod b and a batch pod on Node B. An ml job needing 3 CPU fits on neither node (2 CPU free on each) and stays Pending. The three web pods serve 120 requests per second.

    Key idea: Scheduling is bin packing on requests: a pod fits only if one node has room for all of it.

  2. 2. Spread or pack?

    Replay the last two arrivals with a bin-packing score (prefer the *fuller* node that still fits). The batch pod now goes to Node A, which is fuller, leaving Node B with 3 CPU free, and the ml job fits on Node B.

    Packing wastes less and lets a cluster autoscaler empty and remove whole nodes. Spreading leaves room on every node and limits how many pods one node failure takes down. Real schedulers combine both: hard rules (requests, affinity, topology spread across zones) then a weighted score.

    Scene: With a bin-packing score the batch pod goes to the fuller Node A, and the 3-CPU ml job now fits on Node B.

    Key idea: Packing reduces fragmentation and cost; spreading reduces blast radius. Choose per workload.

  3. 3. Self-healing: desired 3, actual 2

    At 0.6 s Node B fails, taking web b, the batch pod and the ml job with it. Requests sent to web b fail until readiness removes it.

    The Deployment still says 3 replicas. Its controller sees 2, creates a replacement, and the scheduler finds room for 1 CPU only on Node A (web d, ready at about 1.8 s). The ml job goes back to Pending, because 3 CPU fit nowhere now. Nobody was paged: the loop compared desired with actual and acted.

    Scene: Node B fails at 0.6 seconds. Requests to web b fail until it is removed; the scheduler places a replacement, web d, on Node A, which becomes ready at about 1.8 seconds. The ml job is Pending again.

    Key idea: Orchestrators heal by reconciling: they recreate what is missing wherever requests still fit.

  4. 4. A rolling update of a broken release

    Release v2 depends on Payments API v2, which nobody has deployed yet. Its readiness probe only checks that the process is up, so it passes. A rolling update (max surge 1, max unavailable 0) starts v2 pods on Node C one at a time and drains a v1 pod after each one becomes ready.

    Every v2 request fails, so the error rate climbs with each step: about a quarter after the first v2 pod joins, half after the second, and 100% once the last v1 pod is gone. The rollout itself saw nothing wrong: every pod was ready.

    Scene: v2 pods start one at a time on Node C and pass readiness, and a v1 pod is drained after each. Every request served by v2 fails because Payments API v2 is not deployed, so the error rate climbs to 100% as the rollout completes.

    Key idea: Readiness is not correctness: a rolling update spreads a bad release step by step to all users.

  5. 5. Blue-green: switch everything, switch back

    Restore v1 (blue) and keep the three v2 pods running idle (green): a full second copy of the service. At 1.0 s the Service switches all traffic to green at once.

    The broken release now fails every request immediately, so it is noticed in seconds, and the rollback is just as fast: at 2.2 s traffic switches back to blue, which never stopped running, and errors stop at once. The price is paying for two full environments during every release, and an all-or-nothing switch.

    Scene: Blue (v1) and green (v2) both run. At 1.0 seconds all traffic switches to green and nearly every request fails; at 2.2 seconds it switches back to blue and errors stop.

    Key idea: Blue-green makes switching and rolling back instant, but exposes all users at once and doubles capacity during the release.

  6. 6. Canary: a small slice first

    Now only one v2 pod runs, as a canary, with weight 1 against weight 6 for each v1 pod: about 5% of requests (1 in 19). Errors appear, but only on requests the canary served, so the overall error rate stays around 5% instead of 100%.

    An automated analysis compares the canary with the stable pods (here 100% errors against 0%) and, at 2.0 s, aborts: the canary’s weight goes to 0 and errors stop. A canary limits the blast radius in proportion to its traffic share, at the cost of a slower rollout and the need for good metrics.

    Scene: One v2 canary pod gets about 5% of traffic; only the requests it serves fail, so the overall error rate is about 5%. At 2.0 seconds the analysis aborts the canary and errors stop.

    Key idea: A canary exposes a small share of traffic to the new version and lets metrics, not luck, decide whether to continue.

  7. 7. Fix the cause, then ramp up in stages

    The real fix was deploying Payments API v2 first. With it running, the canary starts again at 5%, and the rollout ramps up in stages while metrics stay clean: 25% at 1.0 s, 50% at 2.0 s (the other two v2 pods have joined), 100% at 3.0 s. The v1 pods are stopped at 3.5 s.

    No request fails. Progressive delivery tools (Argo Rollouts, Flagger, or a mesh’s traffic splitting) automate exactly this loop: shift weight, wait, compare metrics, continue or roll back.

    Scene: Payments API v2 is deployed. The canary ramps from 5% to 25%, 50% and 100% of traffic while the other v2 pods join; the v1 pods are stopped at the end. No request fails.

    Key idea: Promote a canary in steps gated by metrics; deploy dependencies before the code that needs them.

  8. 8. What the orchestrator decides, and what you still decide

    The orchestrator places pods by their requests (spread or pack), heals by recreating what is missing where it fits, and rolls out new versions by shifting traffic between pod sets.

    You still choose the requests and limits, keep spare room so replacements are not stuck Pending, and pick a rollout strategy: rolling (cheap, gradual, but readiness is all it checks), blue-green (instant switch and rollback, double capacity, all users at once) or canary (small blast radius, needs metrics and automation).

    Scene: Three v2 pods on Node C serve all traffic through the Service; the v1 pods are stopped and Node B is gone.

    Key idea: Orchestration automates placement, healing and rollouts; safe releases still need the right strategy and real health signals.