Sidecar Definition

Difficulty: Intermediate

Question

What is the sidecar pattern in Kubernetes, how are sidecars implemented, and what problems do they solve?

Answer

A sidecar is a helper container that runs in the same Pod as the main application container to add a supporting capability, such as proxying traffic, shipping logs or syncing configuration, without changing the application code. Because containers in a Pod share the network namespace and can share volumes, the sidecar can talk to the app over localhost and read the same files.

What containers in a Pod share

  • Network: one IP and port space. A proxy sidecar on port 15001 and the app on port 8080 talk over localhost.
  • Volumes: an emptyDir mounted in both lets the app write logs that a shipper reads.
  • Lifecycle and scheduling: they are scheduled to the same node together, and scale together.

They do not share a filesystem root or (by default) a process namespace; you can enable shareProcessNamespace: true if a sidecar needs to see the app's processes.

Typical sidecars in real systems

  • Service mesh proxies: Envoy in Istio or the Linkerd proxy handle mTLS, retries and metrics for every service.
  • Log shipping: Fluent Bit tails files from a shared volume and forwards them to Elasticsearch or Loki, useful for legacy apps that cannot log to stdout.
  • Secrets and config: the Vault Agent injector or a git-sync container keeps files fresh on a shared volume.
  • Database proxies: Cloud SQL Auth Proxy lets an app connect to localhost:5432 while the proxy handles IAM auth and TLS.

Native sidecar containers

For years, sidecars were just extra entries in containers, which caused ordering problems: the app could start before the proxy was ready, and Jobs never completed because the sidecar kept running after the main container exited. Kubernetes fixed this with native sidecars: an entry in initContainers with restartPolicy: Always. This feature has been on by default since 1.29 and is stable since 1.33. Native sidecars:

  • Start before the regular containers, and can use a startup probe to delay the app until the sidecar is ready
  • Keep running for the life of the Pod and are restarted if they crash
  • Are stopped after the main containers during shutdown, so the proxy is still there while the app drains requests
  • Do not block Job completion

Trade-offs

  • Resource cost: a proxy in each of 500 Pods adds up. Each sidecar needs its own requests and limits.
  • Operational coupling: upgrading the sidecar means restarting every Pod.
  • Alternatives: node-level DaemonSets (one log agent per node) or Istio's ambient mode, which moves the proxy out of the Pod, avoid per-Pod overhead.

Related patterns include the ambassador (a sidecar that proxies outbound calls, like the Cloud SQL proxy) and the adapter (a sidecar that converts output into a standard format, such as a metrics exporter).

Classic vs native sidecar containers. Native sidecars fix startup order, shutdown order and Job completion.
Regular container sidecarNative sidecar (initContainers + restartPolicy: Always)
Startup orderStarts alongside the app, no guaranteeStarts and can be ready before the app
Shutdown orderCan stop before the app finishesStops after the main containers
JobsKeeps the Job from completingJob completes when the main container exits
AvailabilityAll versionsOn by default since 1.29, stable in 1.33

Code examples

Native sidecar for log shipping

apiVersion: v1
kind: Pod
metadata:
  name: legacy-billing
spec:
  initContainers:
    - name: log-shipper
      image: fluent/fluent-bit:3.2
      restartPolicy: Always          # makes it a native sidecar
      volumeMounts:
        - name: logs
          mountPath: /var/log/app
          readOnly: true
  containers:
    - name: app
      image: ghcr.io/acme/billing:4.0
      volumeMounts:
        - name: logs
          mountPath: /var/log/app      # app writes files here
  volumes:
    - name: logs
      emptyDir: {}

The shipper starts first, shares the logs volume with the app, and is stopped only after the app exits.

Key points

  • A sidecar is a helper container in the same Pod as the app
  • Shares the Pod's network (localhost) and any mounted volumes
  • Native sidecars are initContainers with restartPolicy: Always (stable since 1.33)
  • Common uses: mesh proxies, log shippers, secret or config sync, DB auth proxies
  • Costs: extra resources per Pod and coupled upgrades

Common follow-up questions

  • Why did Jobs with sidecars never complete before native sidecars?
  • When would you use a DaemonSet instead of a sidecar for logging?
  • How does Istio inject its sidecar automatically?
  • What are the ambassador and adapter patterns?

Interview tips

  • Explain why the helper belongs in the same Pod: shared localhost and volumes
  • Mention native sidecars and the problems they fix; it shows you are current
  • Discuss the per-Pod resource cost and alternatives like ambient mesh

Concepts covered

Sidecar Pattern, Native Sidecar Containers, Shared Network Namespace, Service Mesh Proxy, Log Shipping

More Kubernetes & Orchestration questions