Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a post-init container in Kubernetes?

Tags:

kubernetes

I'm trying to create a redis cluster on K8s. I need a sidecar container to create the cluster after the required number of redis containers are online.

I've got 2 containers, redis and a sidecar. I'm running them in a statefulset with 6 replicas. I need the sidecar container to run just once for each replica then terminate. It's doing that, but K8s keeps rerunning the sidecar.

I've tried setting a restartPolicy at the container level, but it's invalid. It seems K8s only supports this at the pod level. I can't use this though because I want the redis container to be restarted, just not the sidecar.

Is there anything like a post-init container? My sidecar needs to run after the main redis container to make it join the cluster. So an init container is no use.

What's the best way to solve this with K8s 1.6?

like image 403
jbrown Avatar asked May 30 '17 13:05

jbrown


People also ask

How do I execute an init container?

Create a new pod manifest extracted from the init container manifest. Replace the command with sleep 6000 and execute the commands. This allows you to poke around.

What is Initcontainers?

Introduction. Init containers are special containers that run before main containers run in a pod. Init containers support many of the features of application containers. Sidecar containers are containers that run along with the main container in a pod.


1 Answers

I advise you to use Kubernetes Jobs:

https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/

This kind of Job will keep running until it is completed once. In this job you could try detecting if all the required nodes are available in order to form the cluster.

like image 157
Javier Salmeron Avatar answered Sep 25 '22 22:09

Javier Salmeron