Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to delete a Pod after a Kubernetes Job completes

I have a situation in which I run a Kubernetes Job and then want to delete the pod in which the Job's containers run when the job completes. The Kubernetes documentation says the it is the responsibility of the user to delete the pod.

However, my goal is to run a CPU intensive job by spinning up a big honking pod, let the job run, and then upon job completion, automagically nuke the pod. My thinking is that paying for an expensive, resource intensive pod is worth it when the revenue producing job runs. Once the job is over, the revenue stream ends and I need to cut the expense, hence nuking the pod.

I am thinking of having my code in the job container send a out a message upon program completion to a publisher that is picked up by a subscriber. The subscriber knows how to run the command, kubectl delete jobs/myContainer to nuke the pod.

But, maybe there is a better way. Thus, I ask.

Thanks in advance.

like image 212
reselbob Avatar asked Dec 06 '17 01:12

reselbob


2 Answers

Why would you need to delete the pod at all ? If the job completed successfully (exit code 0) then the pod will be completed, it will no longer use cpu/memory resurces, and should be garbage collected when needed.

like image 130
Radek 'Goblin' Pieczonka Avatar answered Oct 02 '22 16:10

Radek 'Goblin' Pieczonka


There is a new API that delete jobs after completion and all the dependent elements. The TTLAfterFinished.

You can define it in the .spec.ttlSecondsAfterFinished property of your job. The cleanup will happen X seconds after the job is completed or failed.

As of 16, March, 2019, this API is in alpha, with version 1.12 and can only be used with alpha features enabled.

like image 33
Benoît Latinier Avatar answered Oct 02 '22 14:10

Benoît Latinier