Bookmarks

Sidecar container

A helper container that lives in the same Pod as the main application and takes over its supporting tasks: proxying traffic, collecting logs and refreshing secrets.

Also: sidecar pattern, sidecar, sidecar proxy

checked 8 Nov

All terms

One process is rarely enough for an application: logs have to be shipped somewhere, secrets refreshed, traffic inspected. Instead of baking these chores into the code, a sidecar is run alongside — a separate container that lives in the same Pod as the application and does only its own supporting work. The name comes from a motorcycle’s sidecar: the sidecar is “hitched” to the main application and shares its fate.

What a sidecar is for

The idea is separation of responsibilities: the team owns the business logic, while the supporting tasks are handled by a separate image that is reused across projects and updated independently. Both containers run on the same node and share the network namespace and storage — they talk to each other over localhost and exchange files through shared volumes. Their life cycle is shared too: the sidecar is created and removed together with the application, but inside the Pod it can be restarted on its own.

  • a sidecar proxy passes all of the application’s traffic through itself: mTLS encryption between services, retries on failures, metrics — this is how the Istio and Linkerd service meshes work
  • a log shipper reads logs from a shared volume and sends them to a store such as Loki or Elasticsearch
  • a secrets agent pulls fresh certificates and passwords and passes them to the application
  • a backup agent periodically uploads the application’s data to external storage

Sidecars in Kubernetes

A Pod is designed for several tightly coupled containers, so the sidecar is built into the Kubernetes model. It is declared as an init container with restartPolicy: Always: such a container starts before the main ones and runs for the whole lifetime of the Pod. Native support arrived in Kubernetes 1.28, was enabled by default from version 1.29 and became stable in 1.33.

yaml
            spec:              initContainers:                - name: log-shipper                  image: grafana/alloy                  restartPolicy: Always                  volumeMounts:                    - name: logs                      mountPath: /var/log/app            

The order matters in practice: the containers in initContainers start one after another, and kubelet waits for the sidecar to start before starting the next one. When a Pod shuts down, kubelet keeps the sidecar running until the main application has finished and stops it last — the proxy closes connections right to the end. Before that, a sidecar was declared as a regular container, and the start-up and shutdown order had to be arranged by hand.

Costs and limitations

Isolation is paid for in resources: a sidecar proxy in every Pod consumes memory and CPU even where there is almost no traffic, and in a cluster with thousands of Pods the overhead is noticeable. Debugging is harder: a failure of the helper container can look like a failure of the application. So before opting for a sidecar, it is worth checking the alternatives the documentation suggests: an in-process library, a separate service or an ordinary daemon on the node.

Sources

the term is written from these pages

Verified 8 Nov

Not to be confused with

what it is mixed up with and how it differs

  • service mesh

    A service mesh is an entire layer for managing traffic in a cluster; a sidecar proxy is its building block, a small proxy in every Pod.

  • reverse proxy

    A reverse proxy usually sits at the entrance to the system, while a sidecar shares a Pod with the application and serves only that application’s traffic.

In depth in the references

where the topic is covered in detail