Skip to the page

Kubernetes: scaling and running it well · senior

Init containers, sidecars and Jobs

Init containers run to completion before the app. A sidecar runs next to the app for its whole life. Native sidecars give the sidecar a start and stop order, and Jobs can finish.

The notes

What you must know

  • Init containers run one after another to completion before the app starts. Right for fetching a file, waiting for a dependency or a one-time setup.
  • A sidecar runs alongside the app for its whole life (log shipper, proxy, certificate refresher).
  • Native sidecars, stable since Kubernetes 1.33, are init containers with restartPolicy: Always. They start before the app, keep running, can have probes, and stop after the app on shutdown.
  • The old way (a sidecar as another normal container) had no ordering: the app could start before its proxy was ready, the log agent could die before the app flushed its last logs, and a Job never finished because the sidecar never exited.
  • Use a Job for work that must finish, and a CronJob for schedules. Set backoffLimit, activeDeadlineSeconds and ttlSecondsAfterFinished.
  • Database migrations as an init container run once per pod, so many pods race. A migration Job (or a pipeline step) runs once.
  • Init containers add start time, so keep them short.

The red pen

Common traps: mistake → fix

  • Mistake: A sidecar is just another container in the pod

    Fix: Without native sidecar support it has no start or stop order.

  • Mistake: A Job with a sidecar finishes when the main container exits

    Fix: With old sidecars the Job hangs forever. Native sidecars solve it.

  • Mistake: Migrations in an init container are safe

    Fix: Every pod runs them. Use one Job.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is the difference between an init container and a sidecar?
  2. What is a native sidecar?
  3. Why did a Job never finish when it had a proxy container?
  4. Where would you run database migrations: init container, Job or app start?
  5. Design the pod for an API with a log shipper and a database proxy.

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview