Sub topic 9
Sidecar startup, probes, and termination order
This chapter assumes you can read Pod manifests and use kubectl. It also assumes you know the previous chapter's idea: a native sidecar is an init container with restartPolicy: Always. Here you will see how the kubelet orders sidecars against other containers at startup, how probes affect them, and how they stop.
Startup order
Native sidecars live in initContainers, so they follow the init sequence. The kubelet walks the list in order. A normal init container must finish before the next one starts. A sidecar only has to be started before the next one starts.
A sidecar counts as started in one of two ways:
- With no
startupProbe, the container is running and itspostStarthook, if any, has completed. - With a
startupProbe, the probe has succeeded.
After every init container is done or started, the kubelet starts the regular containers. A sidecar with a startup probe therefore holds back the app until the sidecar can serve traffic.
Probes on sidecars
Sidecars support all three probe types:
| Probe | Effect on a sidecar |
|---|---|
| `startupProbe` | Delays the start of later containers until it succeeds. |
| `readinessProbe` | Counts toward Pod readiness. A failing sidecar makes the whole Pod NotReady and removes it from Service endpoints. |
| `livenessProbe` | Restarts only the sidecar when it fails. |
Regular init containers cannot have probes. Only sidecars can.
Example: a sidecar the app depends on
This Pod runs a small HTTP server as a sidecar. The startup probe keeps the app from starting until the server answers. The app then calls the sidecar on localhost, because containers in a Pod share one network namespace.
apiVersion: v1
kind: Pod
metadata:
name: sidecar-order
spec:
terminationGracePeriodSeconds: 20
initContainers:
- name: local-server
image: busybox:1.36
restartPolicy: Always
command:
- sh
- -c
- |
trap 'echo sidecar stopping; exit 0' TERM
mkdir -p /www && echo ok > /www/healthz
httpd -f -p 8080 -h /www &
wait
startupProbe:
httpGet:
path: /healthz
port: 8080
periodSeconds: 1
failureThreshold: 30
readinessProbe:
httpGet:
path: /healthz
port: 8080
periodSeconds: 5
containers:
- name: app
image: busybox:1.36
command:
- sh
- -c
- |
trap 'echo app stopping; exit 0' TERM
wget -qO- http://localhost:8080/healthz && echo sidecar reachable
sleep 3600 &
waitThe trap lines matter. A shell running as PID 1 ignores SIGTERM unless it has a handler, so without them each container would run out the full grace period before being killed.
Termination order
Shutdown reverses the startup logic:
- The kubelet sends
SIGTERMto the regular containers, after theirpreStophooks run. - It waits for all regular containers to exit.
- It then stops the sidecars, in reverse order of their position in
initContainers.
A sidecar therefore keeps working while the app drains its last requests. A logging sidecar can still ship the app's final lines. A proxy sidecar can still carry the app's final outbound calls.
The whole sequence shares one terminationGracePeriodSeconds budget. If the app takes 15 of the 20 seconds above, the sidecars have only 5 seconds left before SIGKILL.
Checking the order
Apply the Pod and read the logs of both containers. Use --previous or kubectl delete pod with -w in another terminal to watch shutdown.
kubectl apply -f sidecar-order.yaml
kubectl logs sidecar-order -c app
kubectl delete pod sidecar-orderThe first logs command should print sidecar reachable. That shows the app started only after the sidecar was serving. In kubectl describe pod, the events list the sidecar's Started event before the app's.
Common mistakes
- Missing startup probe. Without one, "started" means only "process running". The app can start before the sidecar listens and fail its first calls.
- A sidecar that ignores
SIGTERM. Shutdown waits out the full grace period, then ends inSIGKILL. - A short grace period. The app and sidecars share the budget, so a slow drain starves the sidecars.
- A tight liveness probe. It can restart a sidecar that is merely busy, which breaks whatever depends on it.
Exercise
A Pod has terminationGracePeriodSeconds: 30 and one native sidecar. When the Pod is deleted, the app needs 25 seconds to drain, and the sidecar needs 10 seconds to flush its buffers after receiving SIGTERM. What goes wrong, and how do you fix it?
Show answer
The kubelet stops the sidecar only after the app exits, and both share the 30-second budget. After 25 seconds only 5 remain, so the sidecar is killed with SIGKILL before it finishes flushing. Raise terminationGracePeriodSeconds to at least 35, for example 40 for margin, or shorten the app's drain time.