Sub topic 9
Startup probes: protecting slow-starting applications
This chapter assumes you know Pods, Deployments, and Services, can read YAML, and have used kubectl describe. It also assumes you know what liveness and readiness probes do.
The problem: slow starts and liveness probes
A liveness probe starts running as soon as the container starts. If the application needs 90 seconds to load data or warm caches, the liveness probe fails during that time. After failureThreshold consecutive failures, the kubelet kills the container. The container restarts, starts slowly again, and gets killed again. The result is a restart loop in which the application never finishes starting.
You could avoid this by raising initialDelaySeconds on the liveness probe. That has a cost. The delay must cover your slowest start, so it also delays failure detection on every normal start. A fixed delay is a guess, and startup time varies with node load, image pulls, and dependencies.
A startup probe solves this by separating the two phases. It answers one question: has the application finished starting?
How a startup probe works
While a startup probe is configured and has not yet succeeded, the kubelet does not run the liveness or readiness probes. Once the startup probe succeeds, it stops running, and the other two probes begin. If the startup probe fails failureThreshold times, the kubelet kills the container, and the Pod's restart policy decides what happens next.
The maximum startup time is failureThreshold × periodSeconds. Startup probes use the same mechanisms as the other probes: httpGet, tcpSocket, exec, and grpc.
Configuring a startup probe
This Deployment gives the container up to 150 seconds to start, which is 30 failures at 5-second intervals.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: registry.example.com/web:1.0.0
ports:
- containerPort: 8080
startupProbe:
httpGet:
path: /healthz
port: 8080
periodSeconds: 5
failureThreshold: 30
livenessProbe:
httpGet:
path: /healthz
port: 8080
periodSeconds: 10
timeoutSeconds: 2
failureThreshold: 3
readinessProbe:
httpGet:
path: /ready
port: 8080
periodSeconds: 5
failureThreshold: 3Note the division of work:
- The startup probe polls every 5 seconds. A fast start is detected within about 5 seconds, so you do not wait out a fixed delay.
- After startup succeeds, the liveness probe detects a hung container in roughly 30 seconds (3 failures at 10-second intervals).
- The liveness probe can stay tight because it no longer has to tolerate slow starts.
- The readiness probe starts only after startup succeeds, so the Pod is not added to Service endpoints before then.
Choosing the budget
Set failureThreshold × periodSeconds above your worst observed startup time, with margin. Measure startup under realistic conditions, such as a loaded node and a cold cache, and not on your laptop.
Two trade-offs apply:
- A budget that is too small causes restart loops, which show up as
CrashLoopBackOff. - A budget that is too large delays detection of a container that is truly broken at startup. If the application crashes immediately, the process exits and the container restarts regardless. If it hangs during startup, you wait the full budget before the kubelet acts.
Prefer a small periodSeconds with a large failureThreshold. That gives a long budget and still detects a quick start promptly.
Common errors
- Budget too short. Events show
Startup probe failedfollowed by the container being killed and restarted. Checkkubectl describe podand raisefailureThreshold. - Wrong path or port. A probe that can never succeed always exhausts the budget. The startup probe does not make a broken endpoint pass.
- Expecting readiness during startup. Until the startup probe succeeds, the Pod is not ready. Rollouts of slow applications therefore take longer.
- Relying on
initialDelaySecondsalongside it. It still applies to the startup probe, but you rarely need it once you have a generous threshold.
kubectl describe pod -l app=web
kubectl logs deploy/web --previousThe first command shows probe failure events. The second prints logs from the previous container instance, which helps when the container was killed mid-startup.
Exercise
A Java service takes between 40 and 120 seconds to start. Its liveness probe uses periodSeconds: 10 and failureThreshold: 3. Design a startup probe with periodSeconds: 5 that tolerates the slowest start with a margin, and state the maximum startup time it allows.
Show answer
Use failureThreshold: 30 with periodSeconds: 5. The maximum startup time is 30 × 5 = 150 seconds, which covers the 120-second worst case with a 30-second margin. A fast start of 40 seconds is still detected within about 5 seconds of the application becoming healthy. The liveness probe can keep its tight settings because it only begins after the startup probe succeeds.
Exercise
A container has a startup probe, a liveness probe, and a readiness probe. During the first 60 seconds the startup probe is still failing but has not reached its failureThreshold. Which probes are the kubelet running, and is the Pod receiving Service traffic?
Show answer
Only the startup probe is running. The liveness and readiness probes are not run until the startup probe succeeds. The Pod is not ready, so it is not in the Service's endpoints and receives no traffic.