Tell me when the work is done.

I am looking for a way to find out (from within the script) when the Kubernet task is completed. I want to then pull the logs out of the containers and do the cleanup.

What would be a good way to do this? Would it be better to run kubectl describe job <job_name>grep for 1 Succeededor something like this?

+38
source share
4 answers

Starting with version 1.11, you can do:

kubectl wait --for=condition=complete job/myjob

and you can also set the timeout:

kubectl wait --for=condition=complete --timeout=30s job/myjob
+62
source

You can visually view the status of a task with this command:

kubectl get jobs myjob -w

The option -wmonitors changes. You are looking for a SUCCESSFULcolumn to show 1.

:

until kubectl get jobs myjob -o jsonpath='{.status.conditions[? 
    (@.type=="Complete")].status}' | grep True ; do sleep 1 ; done
+22

Python kubernetes-client.

https://github.com/kubernetes-client/python

Python virtualenv:

virtualenv -p python3 kubernetes_venv

source kubernetes_venv/bin/activate

kubernetes :

pip install kubernetes

Python :

from kubernetes import client, config

config.load_kube_config()

v1 = client.BatchV1Api()
ret = v1.list_namespaced_job(namespace='<YOUR-JOB-NAMESPACE>', watch=False)
for i in ret.items:
    print(i.status.succeeded)

kubeconfig ~/.kube/config ~/.kube/config'<YOUR-JOB-NAMESPACE>'

+3

-w --watch:

$ kubectl get jobs.batch --watch
NAME     COMPLETIONS   DURATION   AGE
python   0/1           3m4s       3m4s
0

All Articles