armb
armb

Reputation: 318

How can I have replicated pods with an init container that is run only once?

If I want to run multiple replicas of some container that requires a one off initialisation task, is there a standard or recommended practice?

Possibilities:

Upvotes: 14

Views: 6776

Answers (2)

armb
armb

Reputation: 318

We effectively ended up with a Job that does the initialization task and creates a secret that the Deployment replicas have mounted as a volume, blocking them until the Job has completed. We're using ArgoCD without sync waves. (There are complications with patching the Job name whenever its spec is updated because Jobs are immutable, but they aren't directly relevant to the original question.)

Upvotes: 2

mebius99
mebius99

Reputation: 2605

The fact that "replicas of some container" are dependent on "a one off initialisation task" means that the application architecture does not fit the Kubernetes paradigm well. That is why an involvement of a third-party manager on top of k8s like Helm has to be considered (as suggested by Eduardo Baitello and Matt).

To keep with pure Kubernetes approach, it'd be better to redesign your application so that get it components working as independent or loosely coupled microservices (including initialization tasks). A similar question has been discussed here recently.

As for the possibilities listed in the question, perhaps the first option with InitContainers and StatefulSets could be feasible in pure Kubernetes.

Upvotes: 2

Related Questions