Tell when Job is Complete

拟墨画扇 提交于 2019-11-26 12:42:32

问题


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?


回答1:


Since version 1.11, you can do:

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

and you can also set a timeout:

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



回答2:


You can visually watch a job's status with this command:

kubectl get jobs myjob -w

The -w option watches for changes. You are looking for the SUCCESSFUL column to show 1.

For waiting in a shell script, I'd use this command:

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



回答3:


You can use official Python kubernetes-client.

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

Create new Python virtualenv:

virtualenv -p python3 kubernetes_venv activate it with

source kubernetes_venv/bin/activate

and install kubernetes client with:

pip install kubernetes

Create new Python script and run:

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)

Remember to set up your specific kubeconfig in ~/.kube/config and valid value for your job namespace -> '<YOUR-JOB-NAMESPACE>'




回答4:


I would use -w or --watch:

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


来源:https://stackoverflow.com/questions/44686568/tell-when-job-is-complete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!