Search code examples
kuberneteskubectl

Prevent container in pod to restart after successful job


I have several containers in my pod which are executing specific tasks. Two of the containers act as daemons and if they fail they should be restarted. On the other side, the 3 other containers are meant to execute specific tasks only once.

My intuitive approach was to make them explicitly quit with exit 0 but this doesn't seem to prevent them to restart and do the job again and again. How should I proceed?


Solution

  • solution #1

    Assuming you are using some sort of package manager for k8s deployment. You can use similar feature like helm hooks https://helm.sh/docs/topics/charts_hooks/ to execute specific tasks at specific lifecycle of pods.

    solution #2

    You can configure restartPolicy for those containers to Never as explained here https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy

    solution #3

    Depending on your requirement, you can also use lifecycle handlers of a POD as explained here to execute jobs:

    https://kubernetes.io/docs/tasks/configure-pod-container/attach-handler-lifecycle-event/