blog

12021-08-26

SEAN K.H. LIAO

kubernetes multiple containers

Usually you have 1 service per Deployment/StatefulSet which runs in a single container. If you have any sidecars that need to run with it, you add it to the list. But do you add it before or after your service?

docs

While the docs say that initContainers are run serially, no guarantee is made on how the main containers are run. There's an open issue to support dependencies between containers.

Right now, they're started serially, but there have already been attempts to start them in parallel.

implementation

The important parts are in kubelet/kuberuntime, specifically: getting list of containers that need to change and starting containers in order.

This means that you probably want to start your sidecars first if you main service depends on it, for example starting GCP's cloudsql-proxy first so the service has a DB to connect to.

postStart hook

If you look a bit deeper, you'll notice the postStart hook is run synchronously as part of starting the container, so if that edge of starting first isn't enough, you could craft a blocking postStart hook to wait until the container is ready.