I'm looking for a way to tell (from within a script) when a Kubernetes Job has completed. I want to then get the logs out of the containers and perform cleanup.
What would be a good way to do this? Would the best way be to run kubectl describe job <job_name>
and grep for 1 Succeeded
or something of the sort?
You can visually watch a job's status with this command:
The
-w
option watches for changes. You are looking for theSUCCESSFUL
column to show1
.For waiting in a shell script, I'd use this command:
You can now do:
and you can also set a timeout:
You can use official
Python kubernetes-client
.https://github.com/kubernetes-client/python
Create new Python virtualenv:
virtualenv -p python3 kubernetes_venv
activate it withsource kubernetes_venv/bin/activate
and install kubernetes client with:
pip install kubernetes
Create new Python script and run:
Remember to set up your specific
kubeconfig
in~/.kube/config
and valid value for your job namespace ->'<YOUR-JOB-NAMESPACE>'