Liveness, readiness and startup probes

How Kubernetes knows your app is healthy.

Beginner25 min · lesson 13 of 21
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (3 files, 1 KB): probes.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/k8s-fund/probes.tar.gz && tar -xzf probes.tar.gz, which creates ~/lab/probes/. SHA-256: 0de38c2dd3a863b8011d699b8dbece08c2afb61ad5221d6d3109436727f7b23c

A web server that has deadlocked still has a running process. To Kubernetes, which by default only checks that the process is alive, it looks fine. The Service keeps sending it requests, they all time out, and nothing restarts it. The same blind spot hits an app that needs a minute to load its data: it gets traffic the moment its process starts.

A probe is a check the kubelet runs against a container on a schedule, such as an HTTP request to a path. There are three kinds. A readiness probe decides whether the Pod should get traffic right now. A liveness probe decides whether the container is stuck and needs a restart. A startup probe holds the other two back until a slow app has started. Below, each one is broken on purpose.

A Pod with two probes

This nginx Pod creates two small files when it starts. The readiness probe asks for /ready.txt every 2 seconds, and the liveness probe asks for /live.txt every 3 seconds. Any HTTP status from 200 to 399 counts as success. Two failures in a row (failureThreshold: 2) count as failed. A Service sends traffic to the Pod:

web.yaml
apiVersion: v1
kind: Pod
metadata:
name: web
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.30.6-alpine
# create the two files the probes ask for, then start nginx
command: ["sh", "-c", "echo ok > /usr/share/nginx/html/ready.txt; echo ok > /usr/share/nginx/html/live.txt; exec nginx -g 'daemon off;'"]
readinessProbe: # failing: no traffic from the Service
httpGet:
path: /ready.txt
port: 80
periodSeconds: 2
failureThreshold: 2
livenessProbe: # failing: the container is restarted
httpGet:
path: /live.txt
port: 80
periodSeconds: 3
failureThreshold: 2
---
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 80
ubuntu@secopslog-k8s:~/lab/probes · Kubernetes 1.37
$ kubectl apply -f web.yaml && kubectl wait --for=condition=Ready pod/web --timeout=60s && kubectl get pod web
pod/web created service/web created pod/web condition met NAME READY STATUS RESTARTS AGE web 1/1 Running 0 5s
$ kubectl run client --image=busybox:1.38.0 -- sleep 1d && kubectl wait --for=condition=Ready pod/client --timeout=60s
pod/client created pod/client condition met
$ kubectl exec client -- wget -qO- -T 3 http://web/ready.txt
ok

READY 1/1 means the readiness probe passed. The client Pod is a small busybox Pod that stays running, so you can send requests to the Service from inside the cluster, the way lesson 8, Services, did. The request returns ok.

Break readiness

Delete the file the readiness probe asks for. nginx keeps running and keeps serving everything else. kubectl wait --for=condition=Ready=false returns as soon as the Pod is marked not ready. Each node's kube-proxy then needs a second or two to stop routing to it, so if your first request through the Service still gets an answer, send it again:

ubuntu@secopslog-k8s:~/lab/probes · Kubernetes 1.37
$ kubectl exec web -- rm /usr/share/nginx/html/ready.txt && kubectl wait --for=condition=Ready=false pod/web --timeout=30s && kubectl get pod web
pod/web condition met NAME READY STATUS RESTARTS AGE web 0/1 Running 0 19s
$ kubectl get endpointslice -l kubernetes.io/service-name=web -o jsonpath='{range .items[*].endpoints[*]}ready={.conditions.ready}{"\n"}{end}'
ready=false
$ kubectl exec client -- wget -qO- -T 3 http://web/
wget: can't connect to remote host (10.96.228.46): Connection refused command terminated with exit code 1

Within a few seconds the Pod is 0/1 but still Running with 0 restarts. Nothing was killed. The Service's EndpointSlice still lists the Pod, but with ready=false, and a Service only sends traffic to ready endpoints. So the request through the Service fails with Connection refused, even for the home page, which nginx would still serve. With one Pod that is a full outage; with several, the healthy ones would take the traffic. That is the point of readiness: a Pod that is busy, overloaded or still warming up stops getting requests without losing its state.

ubuntu@secopslog-k8s:~/lab/probes · Kubernetes 1.37
$ kubectl events --for pod/web | grep -i readiness
18s Warning Unhealthy Pod/web Readiness probe failed: Get "http://10.244.2.2:80/ready.txt": dial tcp 10.244.2.2:80: connect: connection refused 1s (x4 over 5s) Warning Unhealthy Pod/web Readiness probe failed: HTTP probe failed with statuscode: 404

The events show each failure. The newest line is the 404 for the missing file, repeated with a count. The older line, connection refused, comes from the moment the Pod started, before nginx was listening. Readiness failures right after a start are normal, and that is why a new Pod is never sent traffic until its probe passes.

ubuntu@secopslog-k8s:~/lab/probes · Kubernetes 1.37
$ kubectl exec web -- sh -c 'echo ok > /usr/share/nginx/html/ready.txt' && kubectl wait --for=condition=Ready pod/web --timeout=30s && kubectl get pod web
pod/web condition met NAME READY STATUS RESTARTS AGE web 1/1 Running 0 23s

Put the file back and the Pod is 1/1 again, still with 0 restarts. Readiness only switches traffic on and off.

Break liveness

Now delete the file the liveness probe asks for, then run kubectl get pod web every few seconds until RESTARTS shows 1:

ubuntu@secopslog-k8s:~/lab/probes · Kubernetes 1.37
$ kubectl exec web -- rm /usr/share/nginx/html/live.txt
$ kubectl get pod web
NAME READY STATUS RESTARTS AGE web 1/1 Running 1 (2s ago) 30s
$ kubectl events --for pod/web | grep -i -e liveness -e killing
26s Warning Unhealthy Pod/web Liveness probe failed: Get "http://10.244.2.2:80/live.txt": dial tcp 10.244.2.2:80: connect: connection refused 2s (x2 over 5s) Warning Unhealthy Pod/web Liveness probe failed: HTTP probe failed with statuscode: 404 2s Normal Killing Pod/web Container nginx failed liveness probe, will be restarted
$ kubectl wait --for=condition=Ready pod/web --timeout=60s && kubectl exec web -- ls /usr/share/nginx/html
pod/web condition met 50x.html index.html live.txt ready.txt

After two failed checks, about six seconds, RESTARTS became 1. The newest events give the reason: Liveness probe failed: HTTP probe failed with statuscode: 404, then Container nginx failed liveness probe, will be restarted. The older connection refused line is from the Pod's first second, before nginx was listening. The kubelet killed the container and started a new one from the image, and the start-up command created both files again, so the Pod is healthy. A restart fixes the problems that a restart can fix, such as a deadlock. It does nothing for a database that is down, which is why a liveness probe should check only the container itself, never something it depends on. Otherwise one slow database restarts every Pod at once.

Protect a slow start

Some apps need a long time to start, for example to load a large cache or run database migrations. A liveness probe does not know that. These two Pods both wait 20 seconds before nginx starts. Only the second has a startup probe, which holds back the liveness probe until it succeeds once:

slow.yaml
# Two copies of an app that needs 20 s before it answers. Only the second has a startup probe.
apiVersion: v1
kind: Pod
metadata:
name: slow-nostartup
spec:
terminationGracePeriodSeconds: 1 # the shell ignores a polite stop, so do not wait 30 s for it
containers:
- name: nginx
image: nginx:1.30.6-alpine
command: ["sh", "-c", "sleep 20; echo ok > /usr/share/nginx/html/live.txt; exec nginx -g 'daemon off;'"]
livenessProbe:
httpGet:
path: /live.txt
port: 80
periodSeconds: 3
failureThreshold: 2
---
apiVersion: v1
kind: Pod
metadata:
name: slow-startup
spec:
terminationGracePeriodSeconds: 1 # the shell ignores a polite stop, so do not wait 30 s for it
containers:
- name: nginx
image: nginx:1.30.6-alpine
command: ["sh", "-c", "sleep 20; echo ok > /usr/share/nginx/html/live.txt; exec nginx -g 'daemon off;'"]
startupProbe: # liveness waits until this succeeds once
httpGet:
path: /live.txt
port: 80
periodSeconds: 3
failureThreshold: 20 # up to 60 s to start
livenessProbe:
httpGet:
path: /live.txt
port: 80
periodSeconds: 3
failureThreshold: 2

Apply it, then run kubectl get pods slow-nostartup slow-startup every few seconds for about half a minute:

ubuntu@secopslog-k8s:~/lab/probes · Kubernetes 1.37
$ kubectl apply -f slow.yaml
pod/slow-nostartup created pod/slow-startup created
$ kubectl get pods slow-nostartup slow-startup
NAME READY STATUS RESTARTS AGE slow-nostartup 1/1 Running 2 (8s ago) 33s slow-startup 1/1 Running 0 33s
$ kubectl events --for pod/slow-nostartup --types=Warning && kubectl events --for pod/slow-startup --types=Warning
LAST SEEN TYPE REASON OBJECT MESSAGE 4s (x6 over 22s) Warning Unhealthy Pod/slow-nostartup Liveness probe failed: Get "http://10.244.1.3:80/live.txt": dial tcp 10.244.1.3:80: connect: connection refused 0s (x2 over 1s) Warning BackOff Pod/slow-nostartup Back-off restarting failed container nginx in pod slow-nostartup_default(960bace7-a93b-4984-b855-0017107a8fdf) LAST SEEN TYPE REASON OBJECT MESSAGE 4s (x7 over 22s) Warning Unhealthy Pod/slow-startup Startup probe failed: Get "http://10.244.2.3:80/live.txt": dial tcp 10.244.2.3:80: connect: connection refused

Both Pods are about half a minute old. slow-nostartup has already been restarted twice. Its liveness probe failed after about six seconds, long before the 20 second wait was over, so it was killed, started again, and killed again. It will never get to serve. Between kills it even shows 1/1 Running, because it has no readiness probe, and the BackOff event shows the kubelet now waiting longer before each restart; if you catch it at that moment, STATUS reads CrashLoopBackOff. slow-startup is Running and ready with 0 restarts. Its startup probe also failed six times while the app was waiting, but a failed startup probe only means "not yet". It allows failureThreshold times periodSeconds, here 20 times 3, so 60 seconds, before it gives up and restarts the container. Once it succeeded, the liveness probe took over.

A startup probe is better than a long initialDelaySeconds on the liveness probe. The delay is the same on every start, however fast the app is. A startup probe ends as soon as the app answers.

Hands-on

1. Break readiness again: kubectl exec web -- rm /usr/share/nginx/html/ready.txt.

2. Wait until the Pod is marked not ready, then look at it: kubectl wait --for=condition=Ready=false pod/web --timeout=30s, then kubectl get pod web.

3. Request the file through the Service: kubectl exec client -- wget -qO- -T 3 http://web/ready.txt. If it still answers, give kube-proxy a second and send it again.

4. Restore the file: kubectl exec web -- sh -c 'echo ok > /usr/share/nginx/html/ready.txt', then kubectl wait --for=condition=Ready pod/web --timeout=30s.

5. Send the same request as in step 3 again.

6. Clean up with kubectl delete -f web.yaml -f slow.yaml and kubectl delete pod client --now, then check with kubectl get pods.

What you should see: step 2 shows web as 0/1 and Running, with the one restart from the liveness test and no new ones. Step 3 fails with Connection refused and command terminated with exit code 1. Step 4 prints condition met, and step 5 prints ok. Step 6 removes four Pods and the Service, and kubectl get pods says No resources found in default namespace.

Remember

A failing readiness probe takes a Pod out of its Services without restarting it, while a failing liveness probe restarts the container. Give slow starters a startup probe, so liveness does not kill them before they are up.

Quick check
01A Pod's readiness probe starts failing, while its liveness probe still passes. What happens?
Incorrect — Restarts come from liveness. RESTARTS stayed at 0 while the Pod was not ready.
Correct — The Pod went to 0/1 but stayed Running, and requests through the Service stopped reaching it.
Incorrect — Nothing is replaced. The same Pod came back to 1/1 when the file returned.
Incorrect — Readiness acts on its own. The EndpointSlice marked the Pod ready=false.
02Your liveness probe calls the database to check that the app can reach it. The database goes down for a minute. What is the risk?
Incorrect — A probe checks whatever the endpoint checks. This one reaches the database.
Incorrect — That would be a readiness probe. This one is liveness.
Incorrect — The kubelet restarts the container whose probe failed, not the database.
Correct — Each Pod's liveness fails at once, and restarting them does not bring the database back.
03An app needs about 40 seconds to start. Its liveness probe fails after 6 seconds. What is the best fix?
Correct — Liveness waits until the startup probe succeeds; slow-startup reached Running with 0 restarts.
Incorrect — Then a stuck app is never restarted. A startup probe keeps both.
Incorrect — Liveness does not wait for readiness. It runs on its own schedule.
Incorrect — The probe still fails, and the container then never comes back.

Related