Sidecar Containers
As of kubernetes 1.33, sidecar containers are natively supported. This allows sidecar containers to follow a defined lifecycle relative to the main containers in the pod, without requiring entrypoint hacks. https://kubernetes.io/docs/concepts/workloads/pods/sidecar-containers/
How Native Sidecars Work
-
Declared using the
restartPolicy: Alwaysfield inside theinitContainersblock. -
Kubernetes treats such containers as sidecars, ensuring they:
- Start before main containers.
- Run alongside them.
- Shut down after the main containers complete
-
Example sidecar manifest:
apiVersion: v1
kind: Pod
metadata:
name: sidecar-example
spec:
initContainers:
- name: sidecar-logger
image: busybox:1.31
restartPolicy: Always
command: ["sh", "-c", "while true; do echo Sidecar running; sleep 10; done"]
containers:
- name: main-app
image: busybox:1.31
command: ["sh", "-c", "echo Main app starting; sleep 60"]
- In this setup:
- The sidecar-logger container behaves like a sidecar, though declared in
initContainers. - It uses
restartPolicy: Alwaysto stay alive throughout the Pod lifecycle. - Kubernetes starts the sidecar first, waits for readiness, then starts the main app.
- The sidecar-logger container behaves like a sidecar, though declared in