Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Init container to be ran only once per deployment

Tags:

kubernetes

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

like image 303
Georges Petrov Avatar asked Apr 04 '20 17:04

Georges Petrov


Video Answer


2 Answers

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.

like image 85
Radek 'Goblin' Pieczonka Avatar answered Oct 20 '22 22:10

Radek 'Goblin' Pieczonka


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.

like image 24
Yaron Idan Avatar answered Oct 20 '22 20:10

Yaron Idan