sidecar vs init container in kubernetes

11/15/2020

I am having trouble distinguishing between a sidecar and an init container. So far, I understand that the real app containers wait for init container to do something. However, sidecar could do the same thing , could it not? And vice versa, init containers don't die off, so also run "on the side". Hence , my confusion.

Thanks for the help.

-- Mamun
containers
google-kubernetes-engine
kubernetes
sidecar

1 Answer

11/15/2020

Init-containers are used to initialize something inside your Pod. The init-containers will run and exit. After every init container which exits with a code 0, your main containers will start.

Examples for init-containers are:

  • Moving some file into your application containers, e.g. Themes or Configuration. This example is also described in the Kubernetes docs.

Kubernetes itself does not know anything about sidecars. Sidecar-Containers are a pattern to solve some use-cases. Usually, Kubernetes distinguishes between Init-Containers and Containers running inside your Pod.

Typically, we call Sidecars all containers, that do not provide a user-focused service. For example, this could be a proxy or something for easier database access. If you're running a Java-App you could use a sidecar to export JVM metrics in Prometheus format.

The difference here is, that your sidecar-containers must run all the time. If one of your not-init-containers exits, kubernetes will restart the whole pod.

And that's the difference.

  • Init containers run and exit before your main application starts
  • Sidecars run side-by-side with your main container(s) and provide some kind of service for them.
-- alexzimmer96
Source: StackOverflow