Sub topic 10
Resource requests, limits, and probes
This chapter assumes you can read a Deployment manifest, know that a Pod holds containers, and can run kubectl apply. It covers the part of the container spec that tells Kubernetes how much capacity a container needs and how to check that it is healthy.
Requests and limits
Each container can declare two numbers for CPU and memory.
- A request is the amount the scheduler reserves for the container. The scheduler only places a Pod on a node with enough unreserved capacity.
- A limit is the most the container may use at run time.
CPU is measured in cores, and 100m means 100 millicores, or 0.1 core. Memory is measured in bytes, and 128Mi means 128 mebibytes.
The two limits behave differently when a container reaches them:
| Resource | When usage reaches the limit |
|---|---|
| CPU | The container is throttled. It runs slower but keeps running. |
| Memory | The container is killed with an out-of-memory error (OOMKilled) and restarted. |
The Deployment from the earlier chapter sets a CPU request, a memory request, and a memory limit:
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
memory: 256MiIt has no CPU limit. Many teams do this on purpose, because a CPU limit can throttle a container even when the node has idle CPU. A memory limit is still useful, because memory cannot be reclaimed without killing the process.
Probes
A probe is a check that the kubelet (the agent on each node) runs against a container. Kubernetes has three kinds, and each triggers a different action.
| Probe | Question it answers | Action on failure |
|---|---|---|
| `startupProbe` | Has the application finished starting? | Restarts the container. Other probes wait until it succeeds. |
| `livenessProbe` | Is the process stuck? | Restarts the container. |
| `readinessProbe` | Can it serve traffic now? | Removes the Pod from Service endpoints. No restart. |
Readiness is the one that matters most for rolling updates. A new Pod receives traffic from a Service only after its readiness probe passes. The Deployment waits for that before it removes old Pods.
The earlier Deployment uses an HTTP probe:
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 5
periodSeconds: 10
failureThreshold: 3The timing fields control how patient the check is. initialDelaySeconds delays the first check, periodSeconds sets the interval, and failureThreshold sets how many consecutive failures count as a failure. If you leave them out, the defaults are 0 seconds, 10 seconds, and 3 failures.
Besides httpGet, a probe can use tcpSocket to check that a port accepts connections, or exec to run a command inside the container.
Common errors
- Pod stuck in Pending. The requests are larger than any node can offer. Run
kubectl describe pod <name>and read the Events for an "Insufficient cpu" or "Insufficient memory" message. - Container restarts with OOMKilled. The memory limit is too low for the workload. Check with
kubectl describe pod, then raise the limit or fix the leak. - Restart loop caused by a liveness probe. The probe fires before a slow application is ready. Add a
startupProbeinstead of making the liveness probe lenient. - Liveness probe that checks a dependency. If the probe fails because a database is down, every Pod restarts at once. Check only the process itself.
- Pods Running but never Ready. The probe path or port is wrong. The Service then has no endpoints.
Exercise
A Pod keeps restarting, and kubectl describe pod shows "Last State: Terminated, Reason: OOMKilled". Which setting do you adjust, and why does raising a CPU limit not help?
Show answer
Raise the memory limit, or reduce the application's memory use. OOMKilled means the container went over its memory limit and was killed. CPU limits cause throttling, not kills, so changing them does not stop the restarts.
Exercise
Your Deployment rolls out a new version, and the new Pods start but old Pods are never replaced. The Pods show Running with 0/1 Ready. Which probe is most likely failing, and where do you look first?
Show answer
The readiness probe is failing, so the Deployment never counts the new Pods as available. Run kubectl describe pod <name> and check the probe path, port, and Events. Also confirm the application actually responds on that path.