How to find out if a K8s job failed or succeeded using kubectl?

1/10/2019

I have a Kubernetes job that runs for some time, and I need to check if it failed or was successful.

I am checking this periodically:

kubectl describe job/myjob | grep "1 Succeeded"

This works but I am concerned that a change in kubernetes can break this; say, the message is changed to "1 completed with success" (stupid text but you know what I mean) and now my grep will not find what it is looking for.

Any suggestions? this is being done in a bash script.

-- Greg Balajewicz
kubectl
kubernetes
kubernetes-jobs

1 Answer

1/10/2019

You can get this information from the job using jsonpath filtering to select the .status.succeeded field of the job you are interested in. It will only return the value you are interested in.

from kubectl explain job.status.succeeded:

The number of pods which reached phase Succeeded.

This command will get you that field for the particular job specified:

kubectl get job <jobname> -o jsonpath={.status.succeeded}
-- Marcus
Source: StackOverflow