Native Sidecar Containers in Kubernetes: Practical Guide

7 min read
KubernetesSidecarPod LifecycleDevOpsGKEJobs

Sidecar containers are a foundational pattern in Kubernetes. Platform teams rely on them to run service mesh proxies, OpenTelemetry collectors, Cloud SQL Auth Proxies, and secret-syncing agents alongside application workloads. Yet for years, Kubernetes had no first-class way to distinguish a helper sidecar from the primary application container.

Placing both the application and its helper into spec.containers forced teams to work around startup race conditions, premature proxy termination during rolling updates, and batch Jobs that never exited. Native sidecar containers—introduced in KEP-753 and graduated to General Availability in Kubernetes v1.33—solve these problems directly in the kubelet lifecycle, making zero-downtime Kubernetes upgrades and batch workloads far more predictable.

Why Legacy Sidecars Broke Pod Lifecycles

Before native sidecars, engineers had only two places to put a container in a Pod specification: spec.initContainers for short-lived setup tasks that run to completion, or spec.containers for long-running processes that start concurrently. Neither option fit a helper that needed to start before the app and stay alive until after the app exited.

  • Startup race conditions: Kubelet starts all spec.containers in parallel. An application often attempted outbound database or API calls before its Envoy or Cloud SQL proxy was listening.
  • Premature termination on shutdown: During Pod deletion, kubelet sends SIGTERM to all spec.containers simultaneously. If the network proxy exited first, the application's in-flight requests failed with connection resets.
  • Hanging Kubernetes Jobs: A Job Pod only marks itself Completed when every container in spec.containers exits with code 0. Long-running log shippers or mesh proxies kept Jobs in a Running state indefinitely.
  • Fragile lifecycle hacks: Teams had to write custom wrapper scripts, sleep loops, or HTTP /quitquitquit endpoints to coordinate container startup and teardown.

How Native Sidecar Containers Work

Native sidecar containers are defined inside spec.initContainers with a container-level restartPolicy set to Always. Unlike traditional init containers, which must run to completion and exit 0 before the next container starts, a restartable init container runs concurrently for the entire lifetime of the Pod.

Startup and Readiness Ordering

Kubelet processes spec.initContainers sequentially in the order they appear in the manifest. When it encounters an init container with restartPolicy: Always, it follows a strict readiness handoff:

  1. Kubelet starts the native sidecar container and waits for it to be marked started.
  2. If the sidecar defines a startupProbe, kubelet waits until that probe succeeds before moving on.
  3. Once started (and passing startupProbe), kubelet proceeds to the next initContainer or starts the main spec.containers without waiting for the sidecar to exit.
  4. If the sidecar defines a readinessProbe, its status is combined with the main containers to compute the overall Pod Ready condition.

Graceful Shutdown and Job Completion

During Pod termination, native sidecars reverse the startup order. Kubelet sends SIGTERM to the main application containers in spec.containers first and waits for all of them to terminate. Only after every main container has stopped does kubelet send SIGTERM to the native sidecars, shutting them down in the reverse order of their definition in spec.initContainers.

This ordering guarantees that telemetry agents can flush final traces and service mesh proxies can drain outbound connections while the application shuts down. For Kubernetes Jobs and CronJobs, once all main containers exit with code 0, kubelet terminates the native sidecars automatically and marks the Pod Succeeded.

Production Configuration Examples

A common production use case is pairing an application with an OpenTelemetry Collector sidecar. By defining the collector as a native sidecar with a startupProbe, you ensure the local OTLP endpoint is accepting spans and metrics before the application boots, complementing your broader OpenTelemetry on Kubernetes observability pipeline.

deployment-native-sidecar.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: checkout-api
  namespace: production
spec:
  replicas: 2
  selector:
    matchLabels:
      app: checkout-api
  template:
    metadata:
      labels:
        app: checkout-api
    spec:
      terminationGracePeriodSeconds: 45
      initContainers:
        - name: otel-collector
          image: otel/opentelemetry-collector-contrib:0.114.0
          restartPolicy: Always
          ports:
            - containerPort: 4317
              name: otlp-grpc
            - containerPort: 13133
              name: health
          startupProbe:
            httpGet:
              path: /
              port: health
            periodSeconds: 2
            failureThreshold: 15
          readinessProbe:
            httpGet:
              path: /
              port: health
            periodSeconds: 5
          resources:
            requests:
              cpu: 100m
              memory: 128Mi
            limits:
              memory: 256Mi
          securityContext:
            allowPrivilegeEscalation: false
            runAsNonRoot: true
            capabilities:
              drop: ["ALL"]
      containers:
        - name: api
          image: ghcr.io/example/checkout-api:2.4.1
          env:
            - name: OTEL_EXPORTER_OTLP_ENDPOINT
              value: http://127.0.0.1:4317
          ports:
            - containerPort: 8080
          readinessProbe:
            httpGet:
              path: /healthz
              port: 8080

Fixing Hanging Batch Jobs with Native Sidecars

Database migrations and batch analytics Jobs frequently need an authenticating proxy, such as a Cloud SQL Auth Proxy or mTLS tunnel, to reach private endpoints. With restartPolicy: Always on the proxy initContainer, the migration container never fails on startup due to a cold proxy, and the Job completes cleanly when the migration exits.

job-with-proxy-sidecar.yaml
apiVersion: batch/v1
kind: Job
metadata:
  name: schema-migration-20261130
  namespace: production
spec:
  backoffLimit: 2
  template:
    spec:
      restartPolicy: OnFailure
      initContainers:
        - name: db-proxy
          image: gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.14.1
          restartPolicy: Always
          args:
            - "--structured-logs"
            - "--health-check"
            - "--http-port=9090"
            - "project-id:europe-west1:prod-postgres"
          startupProbe:
            httpGet:
              path: /startup
              port: 9090
            periodSeconds: 2
            failureThreshold: 15
          resources:
            requests:
              cpu: 50m
              memory: 64Mi
            limits:
              memory: 128Mi
      containers:
        - name: migrate
          image: ghcr.io/example/db-migrator:1.9.0
          command: ["./migrate", "up"]

Resource Accounting, Probes, and GKE Caveats

Moving a container from spec.containers into spec.initContainers with restartPolicy: Always changes how kube-scheduler and kubelet calculate Pod resource requests. Because native sidecars run concurrently with main containers, their CPU and memory requests are added to the sum of main containers rather than taking the maximum across init containers.

  • Effective Pod requests: For each resource, Kubernetes computes max(sum(main + native sidecars), max(regular initContainers)). Size node pools and LimitRanges accordingly.
  • Liveness probes: A failing livenessProbe on a native sidecar restarts only that sidecar container, leaving the main application container running.
  • Security context alignment: Native sidecars must still comply with your cluster's Pod Security Standards, including non-root execution and dropped Linux capabilities.
  • GKE observability and billing: Ensure your GKE nodes run a supported patch version (1.32.4-gke.1106006 or newer) for full native sidecar request/limit metric reporting, and verify how GKE cost allocation attributes restartable init container usage in your billing exports.

Migration Checklist for Platform Teams

If your clusters run Kubernetes v1.29 or newer (where SidecarContainers reached Beta and is enabled by default) or v1.33+ (where it is GA), you can adopt native sidecars incrementally without disrupting existing workloads:

  1. Audit Helm charts and Kustomize overlays for legacy sidecars in spec.containers that use sleep loops or postStart hooks to wait for proxies.
  2. Move those helpers to spec.initContainers with restartPolicy: Always and add an explicit startupProbe so kubelet enforces readiness before launching the main app.
  3. Enable native sidecar mode in your service mesh (such as Istio's ambient or native sidecar injection configuration) to eliminate Job termination workarounds.
  4. Remove custom /quitquitquit wrapper scripts from batch Jobs and verify Pod completion status in staging.
verify-native-sidecars.sh
# Inspect initContainer restartPolicy and live startup status
kubectl get pod -n production -l app=checkout-api -o jsonpath='{.items[0].spec.initContainers[*].restartPolicy}{"\n"}'
kubectl get pod -n production -l app=checkout-api -o jsonpath='{range .items[0].status.initContainerStatuses[*]}{.name}{": ready="}{.ready}{" started="}{.started}{"\n"}{end}'

Native sidecar containers remove one of Kubernetes' longest-standing operational workarounds. By expressing helper dependencies declaratively in spec.initContainers, platform teams get deterministic startup ordering, cleaner rolling updates, and batch Jobs that exit reliably without custom glue scripts.

/ author

Pawel Bedynski

DevOps Engineer & Kubernetes Consultant. Building cloud-native infrastructure on GCP since 2019. 80+ production clusters deployed.

LinkedIn