Skip to content
KALANBLON
Show contents

Chapter 4 of 7 · 20 min

Probes and resources

By the end of this chapter, you'll be able to

  • Tell liveness, readiness and startup probes apart
  • Write HTTP probes in a manifest
  • Set CPU and memory requests and limits
  • Explain how the scheduler places pods and what Pending means

Probes: checking that an application is healthy

A living process is not necessarily a working application. Kubernetes regularly questions each container with probes:

ProbeQuestion askedIf it fails…
readinessProbeCan the pod receive traffic right now?it is removed from the Service, without being restarted
livenessProbeIs the pod stuck?the container is restarted
startupProbeHas start-up finished?the other two probes wait; after too many failures, restart

The application often exposes two small endpoints: /ready (the database answers, the caches are loaded) and /alive (the process answers).

      containers:
        - name: api
          image: registry.example.com/payment-api:1.4.2
          readinessProbe:
            httpGet:
              path: /ready
              port: 8000
            periodSeconds: 5
          livenessProbe:
            httpGet:
              path: /alive
              port: 8000
            periodSeconds: 10
            failureThreshold: 3

With these settings, a stuck pod is restarted after 3 consecutive liveness failures, about 30 seconds, and a pod that loses its database stops receiving requests within a few seconds.

Requests and limits: reserving and capping

For each container, you declare:

  • requests: what must be reserved for it. The scheduler uses them to choose a node;
  • limits: the maximum it is allowed to use.

CPU is counted in millicores (500m = half a core), memory in bytes (512Mi = 512 mebibytes).

          resources:
            requests:
              cpu: 500m
              memory: 512Mi
            limits:
              memory: 512Mi

The two resources react differently when exceeded: a container that goes over its CPU limit is slowed down ("throttled"), a container that goes over its memory limit is killed by the kernel, with the reason OOMKilled (out of memory), then restarted.

The scheduler places the pods

The scheduler chooses a node for each new pod: a node whose not yet reserved resources cover the pod's requests. If it finds none, the pod waits, in the Pending state. Here is a simplified simulation that takes the first node that fits:

nodes = {"node-1": {"cpu": 2000, "memory": 4096}, "node-2": {"cpu": 1000, "memory": 2048}}
pods = [("api-a", 500, 512), ("api-b", 500, 512), ("db", 1000, 2048), ("worker", 800, 1024), ("report", 600, 1024)]

free = {n: dict(r) for n, r in nodes.items()}
for name, cpu, mem in pods:
    choice = None
    for n in free:
        if free[n]["cpu"] >= cpu and free[n]["memory"] >= mem:
            choice = n
            break
    if choice:
        free[choice]["cpu"] -= cpu
        free[choice]["memory"] -= mem
        print(f"{name} → {choice}")
    else:
        print(f"{name} → Pending (no node has enough room)")

print(free)
api-a → node-1
api-b → node-1
db → node-1
worker → node-2
report → Pending (no node has enough room)
{'node-1': {'cpu': 0, 'memory': 1024}, 'node-2': {'cpu': 200, 'memory': 1024}}

The report pod asks for 600 millicores: there are 1,024 MiB of memory left on each node, but not enough reservable CPU. It stays Pending until a pod frees some room or a node is added. The real scheduler is smarter (it scores nodes, takes affinities into account, spreads copies), but the principle is the same.

The pod is placed on node 2, whose reserves shrink accordingly.

Step 6 of 6
Text description of the animation
  1. A worker pod asks for 800 millicores and 1,024 MiB.
  2. We look at node 1 first.
  3. Node 1 has no reservable CPU left: it does not fit.
  4. We move on to node 2.
  5. Node 2 has enough CPU and memory.
  6. The pod is placed on node 2, whose reserves shrink accordingly.

Mini-lab: fill the clusterPython 3.13

Output

Click “Run”: Python will load in your browser (about 10 MB the first time).

Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Your turn

Worked exercise

Worked exercise: how many replicas fit?

A node offers 4,000 millicores and 8,192 MiB of allocatable memory. Write capacity(node, requests) that returns the maximum number of identical pods that fit on this node, and the resource that limits it.

node = {"cpu": 4000, "memory": 8192}
print(capacity(node, {"cpu": 500, "memory": 1536}))   # (5, 'memory')
Hint 1
For each resource, the integer division node[r] // requests[r] gives the number of pods it allows.
Hint 2
The number of pods is the smallest of these numbers; the limiting resource is the one giving that minimum. min(..., key=...) finds both at once.
Show the solution and common mistakes
def capacity(node, requests):
    possible = {r: node[r] // requests[r] for r in requests}
    limiting = min(possible, key=possible.get)
    return possible[limiting], limiting

node = {"cpu": 4000, "memory": 8192}
print(capacity(node, {"cpu": 500, "memory": 1536}))
print(capacity(node, {"cpu": 1000, "memory": 512}))
(5, 'memory')
(4, 'cpu')

With 1,536 MiB per pod, memory only allows 5 pods, while CPU would allow 8: memory is the limit. On a real node, part of the resources is reserved for the system and Kubernetes components: always reason on allocatable resources, not on the machine's raw size.

Common mistakes:

  • Using ordinary division (/): 5.33 pods makes no sense; only whole pods are placed.
  • Reasoning on limits instead of requests: the scheduler only looks at requests.

Summary

  1. 1The readiness probe decides whether a pod gets traffic; the liveness probe, whether to restart it; the startup probe protects slow starts.
  2. 2Liveness must only check the process itself, never its dependencies.
  3. 3Requests reserve resources and guide the scheduler; limits cap them.
  4. 4Beyond its limit, a container is slowed down (CPU) or killed with OOMKilled (memory).
  5. 5A pod stays Pending when no node has enough reservable resources.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.