Back to Practice
Lab

Native Sidecars and Graceful Shutdown

Run a logging sidecar the modern way — a native sidecar (an init container with restartPolicy: Always) sharing an emptyDir with the app — then make the app shut down cleanly with a preStop hook and a termination grace period.

Your mission

A sidecar is a helper container that runs alongside your app in the same Pod — shipping logs, refreshing certs, proxying traffic. For years people faked sidecars with an ordinary second container, which caused two headaches: the sidecar didn't start before the app, and at shutdown it could die first, so the app lost its log shipper mid-drain.

Kubernetes 1.29+ makes sidecars first-class. A native sidecar is an init container with restartPolicy: Always. Because it's an init container it starts (and becomes Ready) before the app containers; because its restartPolicy is Always it keeps running for the life of the Pod instead of exiting like a normal init container. You get ordering and lifetime, for free.

The two containers talk through a shared emptyDir volume — an empty scratch directory created with the Pod and mounted into both, so the app writes logs and the sidecar reads them.

The second half of this lab is graceful shutdown. When a Pod is told to stop, Kubernetes sends SIGTERM and waits terminationGracePeriodSeconds before SIGKILL. A preStop hook runs before SIGTERM — the place to drain connections or flush buffers. A grace period without a preStop hook just delays the kill; a preStop hook without enough grace gets cut off. You want both.

Useful aliases

k is aliased to kubectl and completion is configured. The jumpbox also has k9s, jq and yq.

Skills you'll put into practice

podssidecarworkloads

Make it your own.

Use this topic as a starting point for a fresh custom scenario.

Create a variation