Init container to be ran only once per deployment

4/4/2020

I use initContainers inside a deployment. An init container is created for each pods.

It is possible to get only one init container for the whole deployment ?

edit:

use case: need to run some db migration commands before creating the pods. So i have put these commands inside init pods.

problems: - each time a pod is created, the init container is created - on scale up init container is created

solution: I have finally found a good example for solving this problem in this article

-- Georges Petrov
kubernetes

2 Answers

4/4/2020

No, pods are identical so if you use an init container it has to be attached to every pod in your deployment.
You're welcome to comment and tell us more about your use case, because it sounds like the problem you are trying to solve is not the best fit for init containers.

-- Yaron Idan
Source: StackOverflow

4/5/2020

While it is not possible to have one init container shared by whole deployment (which by definition is impossible as the unit of workload scheduling in kube is a pod), there are things you can use to get a similar functionality easily.

One way to achieve this would be to have an init container on every pod waiting for a specific job to be successfully executed, and a job that would do the actual init logic.

Another one would be to use leader election among your init containers so only one executes and the rest just wait for the lock to be release before they exit successfully.

-- Radek 'Goblin' Pieczonka
Source: StackOverflow