How to Run a Kubernetes Sidecar Container in a Pod

A Kubernetes sidecar container runs alongside your main container in the same pod, sharing localhost and any mounted volumes. This is how service meshes, log shippers, and config-sync agents attach to your app without touching its image.

Kubernetes Engineerkuberneteskubectlsidecar

What the sidecar pattern does

Every container in a pod shares the pod's network namespace - they all see localhost and the same port space. Sidecars exploit this: the helper container runs next to the app and can read its log files, proxy its traffic, or sync its config, all without modifying the app image.

Common real-world sidecars: Envoy (Istio/Linkerd mesh proxy), Fluent Bit (log forwarding), Vault Agent (secret injection), and CloudSQL Proxy (DB auth).

Adding a sidecar to a Deployment

Start from a single-container Deployment and add a second entry under containers. A shared emptyDir volume lets both containers exchange files:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: app
  template:
    metadata:
      labels:
        app: app
    spec:
      volumes:
      - name: shared
        emptyDir: {}
      containers:
      - name: app
        image: nginx:1.27-alpine
        ports:
        - containerPort: 80
        volumeMounts:
        - name: shared
          mountPath: /var/log/shared
      - name: sidecar
        image: busybox:1.36
        command: ["sh", "-c", "while true; do echo tick >> /var/log/shared/sidecar.log; sleep 10; done"]
        volumeMounts:
        - name: shared
          mountPath: /var/log/shared

Apply and verify both containers are running:

kubectl apply -f deployment.yaml
kubectl get pods
kubectl get pods -o jsonpath='{.items[0].spec.containers[*].name}'
# app sidecar

Inspecting containers in the same pod

Use -c to exec into a specific container:

# exec into the sidecar
kubectl exec -it <pod> -c sidecar -- sh

# read the log from the app container (they share the volume)
kubectl exec -it <pod> -c app -- cat /var/log/shared/sidecar.log

Key rules

Want to try it hands-on? HeyDevJob gives you this exact setup in a live cloud workspace in your browser - edit it, run it, and see it work. Free, nothing to install.

Try it in a workspace →

What you'll practice

FAQ

What is a Kubernetes sidecar container?

A sidecar is a second container inside the same pod as your main app. It shares the pod's network namespace (localhost) and can share volumes with the app. It runs for the pod's full lifetime alongside the main container - used for log forwarding, proxying, secret injection, and similar cross-cutting tasks.

How do containers in the same pod communicate?

They share localhost - any port the app listens on is reachable from the sidecar as localhost:<port>, and vice versa. No Service or ClusterIP needed. They can also share files via an emptyDir volume mounted in both containers.

What is the difference between an init container and a sidecar?

An init container runs to completion before any regular container starts - use it for one-time setup tasks. A sidecar runs concurrently with the main container for the pod's entire lifetime. Both are defined in the same pod spec; init containers go under initContainers, sidecars under containers.

What is a Kubernetes sidecar container?

A sidecar is a second container in the same pod that supports the main one, sharing its network and volumes. It is used for logging, proxies, and secret agents like Istio, Fluent Bit, and Vault Agent.

Keep learning

Fix a Kubernetes CrashLoopBackOffKubernetes projectHeal a Kubernetes Service With No EndpointsKubernetes projectFix a Kubernetes ImagePullBackOffKubernetes projectKubernetes roadmapStep by step to hiredKubernetes interview questionsSTAR answersAll Kubernetes projectsProjects hub

Learn it by doing. Open this in a live cloud workspace, make the change yourself, and keep a record of the work you can share.

Open the workspace →