Course contents · Lesson 15 of 22

Kubernetes Probes, Requests and Limits Explained

Kubernetes Concepts · Lesson 15 of 22 · 11 min Engineer

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.

Timeline with four lanes, startup probe, readiness probe, liveness probe and Service traffic, over four phases. Phase 1, starting: the startup probe fails while the app loads, then passes; readiness and liveness wait for it and there is no traffic yet. Phase 2, ready and serving: readiness and liveness checks pass, and once readiness passes the Service sends requests. Phase 3, the app hangs: readiness fails three times, the pod is not ready and traffic stops; liveness then fails three times and the kubelet restarts the container. Phase 4, restarted: the startup probe runs again while the others wait. Timing is illustrative
Traffic waits for readiness, and a liveness failure restarts the container, so the sequence begins again.

Probe types and timing settings

Each probe uses one mechanism, the way the kubelet checks:

  • httpGet requests a path and port on the pod's IP; a status code from 200 to 399 passes.
  • tcpSocket passes if a TCP connection to the port opens.
  • exec runs 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:

SettingDefaultMeaning
initialDelaySeconds0Wait before the first check
periodSeconds10Seconds between checks
timeoutSeconds1A slower check fails
failureThreshold3Failures 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.

Flowchart. A probe check fails. Has it failed several times in a row, failureThreshold, 3 by default? No: do nothing yet and check again next period. Yes: which probe failed? startupProbe, the app never finished starting, or livenessProbe, the app is stuck: the kubelet kills the container, and it restarts per restartPolicy in the same pod with the same IP, with RESTARTS up by 1. readinessProbe: the pod is marked not ready while the container keeps running; it is taken out of Service endpoints and put back when the probe passes. Footer: restarts that keep failing back off, shown as CrashLoopBackOff
Startup and liveness failures restart the container; a readiness failure only takes the pod out of traffic.

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.

Bar diagram of a node with 4 CPUs for pods. Three pods' requests stack up: api requests 1.5 CPUs and uses 0.4, web requests 1 and uses 0.2, worker requests 1 and uses 0.3, leaving 0.5 free. A new pod, report, requests 1 CPU, which is more than the 0.5 left, so it does not fit and stays Pending with the event FailedScheduling: Insufficient cpu. Footer: real use is only 0.9 CPU, yet 3.5 CPUs are reserved. Numbers are illustrative
Requests, not real use, fill a node: 0.5 CPU is left, so a pod requesting 1 CPU waits.

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:

ClassWhenEvicted
GuaranteedEvery container sets CPU and memory requests and limits, each request equal to its limitLast
BurstableNot Guaranteed, but some CPU or memory request or limit is setSecond
BestEffortNo CPU or memory requests or limits at allFirst

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.