Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

kubernetes

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:

  • Use a StatefulSet even if it isn't necessary after initialisation, and have init containers which check to see if they are on the first pod in the set and do nothing otherwise. (If a StatefulSet is needed for other reasons anyway, this is almost certainly the simplest answer.)
  • Use init containers which use leader election or some similar method to pick only one of them to do the initialisation.
  • Use init containers, and make sure that multiple copies can safely run in parallel. Probably ideal, but not always simple to arrange. (Especially in the case where a pod fails randomly during a rolling update, and a replacement old pod runs its init at the same time as a new pod is being started.)
  • Use a separate Job (or a separate Deployment) with a single replica. Might make the initialisation easy, but makes managing the dependencies between it and the main containers in a CI/CD pipeline harder (we're not using Helm, but this would be something roughly comparable to a post-install/post-upgrade hook).
like image 729
armb Avatar asked Aug 29 '19 17:08

armb


People also ask

Can a pod have multiple init containers?

A Pod can have multiple containers running apps within it, but it can also have one or more init containers, which are run before the app containers are started.

How many replicas should I have Kubernetes?

In general, if you only have one replica of the application, any abnormal termination of it will result in downtime. In other words, you must have at least two running replicas of the application.

Does replica set replicates deployment?

A ReplicaSet ensures that a specified number of pod replicas are running at any given time. However, a Deployment is a higher-level concept that manages ReplicaSets and provides declarative updates to Pods along with a lot of other useful features.

What is the difference between a replica set and a replication controller?

The major difference between a replication controller and replica set is that the rolling-update command works with Replication Controllers, but won't work with a Replica Set.


1 Answers

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.

like image 195
mebius99 Avatar answered Oct 06 '22 23:10

mebius99