2

I'm running a kubernetes job, deleting it, then starting it again in rapid succession.

I'm using the following command to get the pod related to my job so I can run kubectl logs $POD.

kubectl get pods -l job-name=myjob --output=jsonpath='{.items[*].metadata.name}'

But right after deleting a job the above command returns two pods, the one that has yet to be removed, and the new one.

kubectl describe jobs/myjob shows the correct pod name, I could parse it out of the text from there so I get the unique pod name for the most recently started job, but that seems hacky.

Is there a better way to get the pod name from a specific job?

davidparks21
  • 878
  • 1
  • 11
  • 25

1 Answers1

6

Items of Notice

Job names: They're for us! Humans, kubernetes instead tracks child objects against labels or annotations. In this case, controller-uid

Pulling Unique ID's: Quick way to do that, jsonpath from kubectl. Same syntax a jq, just wrap in {}. jsonpath={.metadata.labels.controller-uid}

You could always try using a jsonpath output against the job to get the controller-uid. That should match uniquely to a single pod, since it's generated on job creation, even if the job has the same name. Example:

Example

kubectl get po -l controller-uid=`kubectl get job $JOBNAME -o "jsonpath={.metadata.labels.controller-uid}"`



### Or for an unembedded way
JOBUUID=$(kubectl get job $JOBNAME -o "jsonpath={.metadata.labels.controller-uid}")
PODNAME=$(kubectl get po -l controller-uid=$JOBUUID -o name)

kubectl logs $PODNAME

I hope that helps, good luck!

darkdragn
  • 256
  • 1
  • 4