问题原文
原帖链接 · davidparks21 · CC BY-SA 4.0
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?
已采纳解答
原帖链接 · darkdragn · CC BY-SA 4.0
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!
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
