Kubernetes Probes, Requests and Limits Explained
Your pod says Running, yet users get errors for 30 seconds after every deploy while the app loads. Another day, a memory leak in one pod starves its neighbours. This lesson explains Kubernetes probes, which check that a container is alive and ready, and the requests and limits that decide where a pod runs and what it may use.
Liveness, readiness and startup probes
A probe is a health check that the kubelet, the node agent from lesson 8, runs on a container at a fixed interval, like the Compose health check in lesson 6. The three kinds differ in what a failure triggers.
A liveness probe asks "is the container still working?" After several failures in a row, the kubelet kills the container and starts it again, following the pod's restartPolicy from lesson 9; the pod keeps its name and IP. Use it for apps that can hang without exiting.
A readiness probe asks "should it get traffic now?" When it fails, the pod is marked not ready and its IP leaves the EndpointSlices of every Service that selects it, as in lesson 11, but nothing restarts. It runs for the container's whole life, so a pod can leave traffic and return.
A startup probe protects slow starters: until it passes, the other two do not run, so liveness cannot kill an app that is still loading. If it fails too often, the container is restarted.
A missing probe counts as passing, which is why the pods in lesson 10 were ready as soon as their containers ran. In the timeline, notice that requests arrive only after readiness passes, and that here readiness drops traffic before liveness restarts the hung container.
Probe types and timing settings
Each probe uses one mechanism, the way the kubelet checks:
httpGetrequests a path and port on the pod's IP; a status code from 200 to 399 passes.tcpSocketpasses if a TCP connection to the port opens.execruns a command in the container; exit code 0 passes.
A fourth, grpc, suits apps that implement gRPC health checking. These settings, with their defaults, decide how fast a probe reacts:
| Setting | Default | Meaning |
|---|---|---|
initialDelaySeconds | 0 | Wait before the first check |
periodSeconds | 10 | Seconds between checks |
timeoutSeconds | 1 | A slower check fails |
failureThreshold | 3 | Failures in a row before Kubernetes acts |
With the defaults, liveness acts within about 30 seconds of a hang. For a startup probe, failureThreshold × periodSeconds is the longest start allowed: 30 × 10 gives 300 seconds. Here is probe-demo.yaml, an nginx pod with all three probes; liveness only checks that port 80 accepts connections:
apiVersion: v1
kind: Pod
metadata:
name: probe-demo
spec:
containers:
- name: web
image: nginx:1.27-alpine
startupProbe:
httpGet:
path: /
port: 80
failureThreshold: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /
port: 80
periodSeconds: 5
livenessProbe:
tcpSocket:
port: 80
nginx starts quickly, so its startup probe passes on the first or second check; a slow Java app would get up to 300 seconds. You create a cluster in lesson 17, so read along for now. Apply the file and list the pod:
kubectl apply -f probe-demo.yaml
kubectl get pod probe-demo
Once the probes pass, you should see this; your age will differ:
pod/probe-demo created
NAME READY STATUS RESTARTS AGE
probe-demo 1/1 Running 0 15s
0/1 under READY means readiness has not passed yet; wait ten seconds and run it again.
To watch a failure, change the readiness path to /ready, a page nginx lacks. A running pod's probes cannot be edited, so run kubectl delete -f probe-demo.yaml and apply the file again. The pod stays 0/1 with no restarts, and kubectl describe pod probe-demo, which prints its details and recent events, ends like this (trimmed):
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning Unhealthy 2s (x8 over 37s) kubelet Readiness probe failed: HTTP probe failed with statuscode: 404
Give the liveness probe an httpGet for /ready instead, and RESTARTS climbs, with the event Container web failed liveness probe, will be restarted. The flowchart sums it up.
Keep liveness about the app itself: if it tests the database too, one outage restarts every replica and fixes nothing.
Requests: what the scheduler reserves
A resource request is the CPU and memory a container asks Kubernetes to set aside for it. The kube-scheduler from lesson 8 places a pod only on a node where its requests, plus those of the pods already there, fit within what the node offers.
It counts requests, not real use: a node of idle pods can be full, and a pod that requests nothing fits even on a busy node.
CPU is in cores: 1 is one physical or virtual core, and 250m, 250 millicores, is a quarter of one. Memory is in bytes: Mi and Gi are powers of 2, M and G powers of 10.
In the diagram, notice that the pods use under a quarter of the node's CPU, yet the new pod does not fit. It stays Pending with an Insufficient cpu event, which lesson 20 teaches you to read.
Limits: CPU is throttled, memory is OOMKilled
A resource limit is the most a container may use. The kubelet passes requests and limits to the container runtime, which turns them into settings of cgroups, the kernel feature from lesson 1. Add this to the web container in probe-demo.yaml, level with image:
resources:
requests:
cpu: 250m
memory: 64Mi
limits:
cpu: 500m
memory: 128Mi
The scheduler now reserves a quarter of a core and 64 MiB, and the container may use up to half a core and 128 MiB. If you set only a limit, Kubernetes copies it as the request.
CPU above the limit is throttled: the kernel lets the container run for only its share of each short period, then makes it wait. The app slows down, but it is never killed for using CPU.
Memory cannot be held back like that. A container that tries to use more than its memory limit is stopped by the kernel's out-of-memory (OOM) killer, and Kubernetes restarts it as the restart policy allows. kubectl describe pod then shows (trimmed):
Last State: Terminated
Reason: OOMKilled
Exit Code: 137
Restart Count: 5
kubectl get pods shows OOMKilled only briefly, then Running or CrashLoopBackOff, so check Last State. Memory between request and limit is allowed, but if the node runs short, the kubelet may evict pods, stopping them to protect the node, and only pods using more than they requested are candidates.
Quality of service classes
From its containers' requests and limits, each pod gets a quality of service (QoS) class, fixed at creation, which sets the eviction order:
| Class | When | Evicted |
|---|---|---|
| Guaranteed | Every container sets CPU and memory requests and limits, each request equal to its limit | Last |
| Burstable | Not Guaranteed, but some CPU or memory request or limit is set | Second |
| BestEffort | No CPU or memory requests or limits at all | First |
With requests below limits, probe-demo is Burstable. Requests matter again in lesson 22: the Horizontal Pod Autoscaler measures CPU use as a percentage of each container's request, and does nothing without one. So set requests on every container, close to real use.
Check yourself
A pod's database goes down and its readiness probe fails. Is the container restarted, and what happens to its traffic?
No restart: only liveness and startup failures restart a container. The pod is marked not ready and leaves the Service endpoints until the probe passes again.
A node's CPU is mostly idle, yet a new pod stays Pending with Insufficient cpu. How can both be true?
The scheduler compares requests, not real use. The pods there have reserved most of the CPU, so a larger request cannot be placed, however idle the node is.
One container goes over its CPU limit, another over its memory limit. What happens to each?
The first is throttled and runs slower, because the kernel can delay CPU time. The second is OOMKilled and restarted, because memory in use cannot be taken back without stopping the process.