Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Sidecar" containers in Kubernetes pods

Tags:

kubernetes

I'd like a multi-container pod with a couple of components:

  • A "main" container which contains a build job
  • A "sidecar" container which contains an HTTP proxy, used by the "main" container

This seems to fit well with the pod design philosophy as described in the Kubernetes documentation, but I believe so long as the "sidecar" runs, the pod is kept alive. In my case, the "main" container is not long-lived; once it exits, the "sidecar" should be terminated.

How can I achieve this?

like image 536
obeattie Avatar asked Jul 26 '16 22:07

obeattie


People also ask

What is init and sidecar container?

Init containers run before applications containers run in a pod, and sidecar containers run alongside application containers in a pod. One use for init containers is to bootstrap Appian with RDBMS/JDBC drivers not included in the Webapp Docker image (for example, MySQL or IBM Db2).

When should you not use sidecar?

When to Avoid Sidecar. Never use a Sidecar Pattern for synchronous activities that must complete prior to generating a user response. Doing so will add some delay to end-user response times. The above graphic indicates the coordination between just two services and the instances that comprise that service.

Can we run two containers in a pod?

A Pod is is the smallest deployable unit that can be deployed and managed by Kubernetes. In other words, if you need to run a single container in Kubernetes, then you need to create a Pod for that container. At the same time, a Pod can contain more than one container, if these containers are relatively tightly coupled.


2 Answers

A pod is running as long as one of the containers is running. If you need them to exit together, you have to arrange that the sidecar dies. We do not have a notion of "primary" vs "secondary" containers wrt lifecycle, though that's sort of interesting.

One option would be to use an emptyDir volume and write a file telling the sidecar "time to go". The sidecar would exit when it sees that file.

like image 112
Tim Hockin Avatar answered Oct 03 '22 11:10

Tim Hockin


For anyone still looking for an answer to this, the sidecar feature is being developed and should be out in v1.17 of Kubernetes, which will have this exact requested behaviour.

From the proposal:

One-line enhancement description: Containers can now be a marked as sidecars so that they startup before normal containers and shutdown after all other containers have terminated.

https://github.com/kubernetes/enhancements/issues/753

UPDATE: looks like it's planned for v1.18 now

like image 39
Jonas D Avatar answered Oct 03 '22 12:10

Jonas D