Kubernetes check if "silent" pod finished executing

3/14/2019

Let's say that I defined a pod that simply runs a few pieces of code and exits afterwards. I need to make sure that this pod exits before allowing other pods to run. What is the best way to implement this?

I used to check whether a pod is ready by performing network requests, e.g. once ready, some webapps pods will block and listen to pre-defined ports, hence I can have the waiting pods performing netcat requests to them. But in this particular case the pod does not need to open any port, hence this approach does not work. Can anyone suggest an alternative?

Thanks

-- João Matos
kubernetes

1 Answer

3/14/2019

If the "pieces of code" needs to be run in every pod start/restart, you maybe are looking for an Init Container implementation:

Init Containers are specialized Containers that run before app Containers and can contain utilities or setup scripts not present in an app image.

If your code is a dependency for multiple pods and needs to be run once (e.g., on every new deploy), you may consider using a Job Controller, and implement a logic to check if it's completed before deploying new pods/containers. (You can use commands like kubectl wait --for=condition=complete job/myjob on your deploy script).

If you are using helm to deploy, the best option is to use k8s job combined with pre-install and pre-upgrade helm hooks.

-- Eduardo Baitello
Source: StackOverflow