Configuring Sidecar Containers

1. Understanding Sidecar Pattern

Sidecar RoleExamples
LoggingFluent Bit, Vector tailing app logs
ProxyEnvoy in service mesh (Istio, Linkerd)
Secrets agentVault agent, AWS secrets-store sidecar
Metrics exporterstatsd-exporter, nginx-exporter
Syncgit-sync, file syncer
Note: Native sidecar containers (init container with restartPolicy: Always) are stable since Kubernetes 1.29.

2. Creating Multi-Container Pods

Example: Native sidecar (1.29+)

spec:
  initContainers:
  - name: logger
    image: fluent/fluent-bit
    restartPolicy: Always       # makes this a sidecar
    volumeMounts: [{ name: logs, mountPath: /var/log/app }]
  containers:
  - name: app
    image: myapp:1.0
    volumeMounts: [{ name: logs, mountPath: /var/log/app }]
  volumes: [{ name: logs, emptyDir: {} }]
Sidecar TypeStart OrderLifecycle
Native (init w/ Always)Before app containersRuns alongside; SIGTERM after app
Plain containerParallel with appPod terminates when any exits (with Job)

3. Sharing Volumes Between Containers

SharedMechanism
FilesystememptyDir, PVC mounted in multiple containers
Networklocalhost — containers share network namespace
IPCshareProcessNamespace: true

4. Implementing Logging Sidecars

Example: Stream log file to stdout

- name: log-streamer
  image: busybox:1.36
  command: ["sh","-c","tail -F /var/log/app/access.log"]
  volumeMounts: [{ name: logs, mountPath: /var/log/app }]
StrategyWhen
Stream to stdoutUse cluster log collector (DaemonSet)
Ship via Fluent Bit sidecarMulti-line logs, custom parsing per app

5. Implementing Proxy Sidecars

ProxyFunction
Envoy (Istio)mTLS, retries, traffic shifting
Linkerd-proxyLightweight Rust proxy, mTLS
Cloud SQL Auth ProxySecure DB tunneling
OAuth2-proxyAuth gateway sidecar

6. Using Ambassador Pattern

Sidecar acts as a proxy to the outside world, hiding remote-service complexity.

Use CaseExample
DB proxyCloud SQL Auth Proxy on localhost:5432
Multi-region routingSidecar picks nearest backend
Shard routerHash-based shard selection

7. Using Adapter Pattern

Sidecar normalizes the main app's output to a standard external interface.

AdapterRole
Prometheus exporterConvert app metrics → Prometheus format
Log reformatterParse app logs → JSON
Health adapterWrap legacy app with standard /healthz

8. Setting Container Lifecycle

HookWhen
postStartRight after container start (no guarantees vs ENTRYPOINT)
preStopBefore SIGTERM during termination
lifecycle:
  preStop:
    exec: { command: ["/bin/sh","-c","nginx -s quit; sleep 5"] }

9. Configuring Shared Network Namespace

FieldEffect
DefaultContainers share IP + ports; talk via localhost
hostNetwork: truePod uses node's network namespace
shareProcessNamespace: trueContainers see each other's PIDs
Warning: Port conflicts between sidecars — each container needs unique ports within the pod.

10. Debugging Sidecar Containers

CommandUse
kubectl logs POD -c sidecarSidecar logs
kubectl exec -it POD -c sidecar -- shShell in sidecar
kubectl describe pod PODState of each container
kubectl debug -it POD --image=busybox --target=appEphemeral debug container