Sub topic 10
Health checks with probes
This chapter assumes you can read a Pod manifest, know the Pod phases and container states, and understand restartPolicy. A running container is not always a healthy one. A process can deadlock, still be loading data, or be unable to reach its database. Kubernetes uses probes to find out.
The three probe types
A probe is a check the kubelet runs against a container on a schedule. The kubelet is the agent on each node. Each probe type triggers a different action.
| Probe | Question it answers | Action on failure |
|---|---|---|
| `startupProbe` | Has the app finished starting? | Kills the container, and `restartPolicy` decides what happens next |
| `livenessProbe` | Is the app still working? | Kills the container, and `restartPolicy` decides what happens next |
| `readinessProbe` | Can the app take traffic now? | Marks the Pod not Ready and removes it from Service endpoints. The container keeps running |
Liveness and startup failures cause restarts. Readiness failures only stop traffic. A Pod that is not Ready still has the Running phase.
Probe mechanisms
Each probe uses one mechanism:
httpGetsucceeds on any status code from 200 to 399.tcpSocketsucceeds if the port accepts a connection.execruns a command in the container and succeeds on exit code 0.grpccalls the standard gRPC health protocol.
A Pod with probes
This Pod from the earlier examples uses an init container to write a page, then serves it with nginx. It has both a readiness and a liveness probe.
apiVersion: v1
kind: Pod
metadata:
name: web-init
labels:
app: web
spec:
initContainers:
- name: prepare-content
image: busybox:1.36
command:
- sh
- -c
- echo '<h1>Hello from init</h1>' > /work/index.html
volumeMounts:
- name: html
mountPath: /work
containers:
- name: nginx
image: nginx:1.27
ports:
- containerPort: 80
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 2
periodSeconds: 5
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 10
periodSeconds: 10
resources:
requests:
cpu: 100m
memory: 64Mi
limits:
memory: 128Mi
volumes:
- name: html
emptyDir: {}The readiness probe starts after 2 seconds and runs every 5 seconds. The liveness probe waits 10 seconds, then runs every 10 seconds.
Timing fields
Every probe accepts the same tuning fields:
initialDelaySeconds: wait before the first check.periodSeconds: time between checks. The default is 10.timeoutSeconds: how long one check may take. The default is 1.failureThreshold: consecutive failures before the probe counts as failed. The default is 3.successThreshold: consecutive successes needed to recover. The default is 1.
With the defaults, a hung app is detected after about 30 seconds.
Startup probes for slow applications
Liveness probes can kill an app that is merely slow to boot. A startup probe fixes this. While it runs, the kubelet disables the liveness and readiness probes. Once it succeeds, the other probes take over.
startupProbe:
httpGet:
path: /
port: 80
periodSeconds: 5
failureThreshold: 30This allows up to 150 seconds (30 × 5) for startup. That is more reliable than a large initialDelaySeconds on the liveness probe, because the app is released as soon as it responds.
Common errors
- Liveness checks a dependency. If the probe calls the database and the database goes down, every replica restarts at once. Keep liveness checks limited to the process itself.
- Liveness probe too aggressive. A short timeout under load causes restarts that add more load. This can end in
CrashLoopBackOff. - No readiness probe. Traffic reaches the Pod as soon as the container starts, before the app can serve it.
- Wrong port or path. The probe fails forever.
kubectl describe podshows events likeLiveness probe failed: HTTP probe failed with statuscode: 404.
Exercise
An app needs up to 90 seconds to start. Afterward it should be restarted if it hangs for about 20 seconds. Which probe settings would you choose?
Show answer
Add a startupProbe with periodSeconds: 5 and failureThreshold: 18 (90 seconds, rounded up with margin, for example 20 for 100 seconds). Then add a livenessProbe with periodSeconds: 10 and failureThreshold: 2, which detects a hang in about 20 seconds. The liveness probe stays disabled until the startup probe succeeds.