Liveness, readiness and startup probes
How Kubernetes knows your app is healthy.
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: 0de38c2dd3a863b8011d699b8dbece08c2afb61ad5221d6d3109436727f7b23cA 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:
apiVersion: v1kind: Podmetadata:name: weblabels:app: webspec:containers:- name: nginximage: nginx:1.30.6-alpine# create the two files the probes ask for, then start nginxcommand: ["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 ServicehttpGet:path: /ready.txtport: 80periodSeconds: 2failureThreshold: 2livenessProbe: # failing: the container is restartedhttpGet:path: /live.txtport: 80periodSeconds: 3failureThreshold: 2---apiVersion: v1kind: Servicemetadata:name: webspec:selector:app: webports:- port: 80
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:
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.
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.
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:
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:
# Two copies of an app that needs 20 s before it answers. Only the second has a startup probe.apiVersion: v1kind: Podmetadata:name: slow-nostartupspec:terminationGracePeriodSeconds: 1 # the shell ignores a polite stop, so do not wait 30 s for itcontainers:- name: nginximage: nginx:1.30.6-alpinecommand: ["sh", "-c", "sleep 20; echo ok > /usr/share/nginx/html/live.txt; exec nginx -g 'daemon off;'"]livenessProbe:httpGet:path: /live.txtport: 80periodSeconds: 3failureThreshold: 2---apiVersion: v1kind: Podmetadata:name: slow-startupspec:terminationGracePeriodSeconds: 1 # the shell ignores a polite stop, so do not wait 30 s for itcontainers:- name: nginximage: nginx:1.30.6-alpinecommand: ["sh", "-c", "sleep 20; echo ok > /usr/share/nginx/html/live.txt; exec nginx -g 'daemon off;'"]startupProbe: # liveness waits until this succeeds oncehttpGet:path: /live.txtport: 80periodSeconds: 3failureThreshold: 20 # up to 60 s to startlivenessProbe:httpGet:path: /live.txtport: 80periodSeconds: 3failureThreshold: 2
Apply it, then run kubectl get pods slow-nostartup slow-startup every few seconds for about half a minute:
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.
slow-startup reached Running with 0 restarts.