大网数据广告活动
服务器运维问答

`kubectl get pods -l job-name` returns multiple pods when a job was recently deleted

· 52CE 我爱测

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=…

问题原文

原帖链接 · 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!

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-07-31)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。