FIELD NOTESKUBERNETES

Native sidecar containers: what restartPolicy: Always fixes

A native sidecar is an init container with restartPolicy: Always. It starts before the app, stops after it and lets Jobs finish, but started is not ready, the grace period is shared, and many helpers do not belong in the Pod at all.

By Space Toucan4 min readPublished
ON THIS PAGE
A Pod with a proxy sidecar, declared in initContainers with restartPolicy: Always, beside the app container; the Service reaches the app through the proxy. Notes: the proxy starts first, stops last, and a Job still completes. Drawn in archhy.

A native sidecar is an init container with restartPolicy: Always, and that one field fixes the three things a second app container always got wrong: the helper now starts before your app, stops after it, and no longer keeps a finished Job alive. Use it for a helper that has to be there for the app's first request and its last. Do not make it the default home for every helper: plenty of them belong on the node, in a run-once init container, or nowhere at all.

Based on the article from Gazeek, our sister site for hands-on CKA and CKAD labs: Kubernetes sidecar containers: what they are and how to run one. The complete Pod manifest, the Docker equivalent and the debugging commands are there.

One field, three fixes#

For years a sidecar was simply a second entry under containers. Kubernetes had no way of knowing which container served which, so it started them together and stopped them together. Anyone who ran a service mesh met the results: the app's first outbound call failed because the proxy was not listening yet, in-flight requests died because the proxy went down at the same moment as the app, and batch Pods never finished because the proxy never exits. Teams wrote wrapper scripts to tell the proxy to quit.

The native form moves the helper into initContainers and marks it restartable:

YAML
initContainers:
- name: proxy
  image: example/proxy:1.4
  restartPolicy: Always
  startupProbe:
    tcpSocket:
      port: 15001
containers:
- name: app
  image: example/shop:2.0
  • Start: the kubelet starts the proxy in its place in the init order and moves on once it counts as started.
  • Stop: on termination the kubelet waits until the app containers have fully stopped, then stops sidecars in the reverse of the order they are listed.
  • Jobs: when the main containers exit, the Job's Pod is done and the sidecar is stopped rather than holding it open.

It also restarts on its own whatever the Pod's restart policy, and it may carry probes, which ordinary init containers may not. The feature is on by default since Kubernetes 1.29 and stable since 1.33.

Started is not ready#

The kubelet moves on to the next container when the sidecar is started, and without a startup probe that means only that its process exists. A proxy that is still loading its configuration has a process. So the ordering guarantee is exactly as strong as the startup probe you give the sidecar: leave it out and you are back to hoping the proxy wins the race. Our probes article covers what a startup probe should check.

Order inside initContainers now matters in a useful way too. A regular init container listed after a sidecar runs while that sidecar is up, so setup that has to go through the mesh, or ship its logs, finally can.

What it still costs#

  • The grace period is shared. Sidecars stop after the app, but inside the same terminationGracePeriodSeconds. If the app spends all of it, the sidecars get SIGTERM and SIGKILL almost together, and a log shipper loses its last lines. Leave headroom, and expect non-zero exit codes from sidecars at shutdown: the documentation calls them normal.
  • Requests add up. The scheduler adds a sidecar's requests to the app's, on every replica. A small request multiplied by every replica is capacity you pay for whether the helper is busy or idle.
  • Order changes the bill. Sidecars listed before an init container are counted alongside it, so moving one up or down the list can change what the Pod requests.

When you do not need a sidecar#

Ask one question of any helper: does it need this Pod's network namespace or volumes for the whole life of the Pod? If not, it does not belong in the Pod.

  • Logs that go to stdout are already on the node. One collector per node, run as a DaemonSet, beats one per Pod.
  • Work that finishes, such as rendering a config file or fixing volume permissions, is a plain init container. Init containers covers what that guarantee is and where it bites.
  • A mesh proxy per Pod is no longer the only way to run a mesh. Istio ambient versus sidecars weighs moving the proxy out of the Pod altogether.
  • A single-language service may be better served by a library than a second process to monitor and patch.

Seeing the start and stop order for yourself makes this stick. Gazeek's CKAD Pods module has multi-container Pod labs on a real cluster.

The diagram#

One Pod with a native sidecar: the proxy declared in initContainers runs beside the app, and the notes give the order: the proxy starts first and stops last. Drawn in archhy. Open it in archhy and press Make a copy to adapt it.

Go deeper#

Gazeek's full article has a working log-shipper Pod, the older two-container form you still meet in exam tasks, the Docker version and the commands for debugging a sidecar that will not start.