Jiří Baum
Jiří Baum

Reputation: 6940

Recreate container on exit in k8s

I have a Deployment in k8s which contains a single container, which exits when it completes its work. By default, k8s then restarts the container.

I would like to recreate the container (or the whole Pod) on exit instead. The work involves a number of temporary files and other changes in the container, which should be discarded when it exits and a fresh container created from the image.

How can I configure the Deployment (or another workload) so that an exited container is either recreated, or causes the whole pod to exit and be recreated?

There is a restartPolicy setting, but it's forced to Always for Deployments. There appears to be a maxRetries setting, but I can't find the documentation or any examples for it. I'm not sure what else to search for.

Upvotes: 0

Views: 1131

Answers (1)

Fabrice Jammes
Fabrice Jammes

Reputation: 3205

Using Job API Object may help. If you use restartPolicy=Never in a Job then the Pods managed by the Job will restart each time the process running in the Pod's container exits with error code. Using restartPolicy=Always will only restart/recreate the container without recreating the pod.

In addition, when a container restart in Kubernetes, it means it is re-created.: all the file created in the container are removed, except the ones which where created on attached persistent storage (for example PersistenceVolume). There is no equivalent to docker start/stop in Kubernetes.

Upvotes: 2

Related Questions