简体   繁体   English

在 kubectl 推出状态后获取新的 pod 名称之一 | kubectl get pods --field-selector 返回终止 pods

[英]get one of new pod name after kubectl rollout status | kubectl get pods --field-selector returns Terminating pods

The problem is that after deploying new deployment like问题是在部署新部署之后

kubectl apply -f deployment.yml

(let's say deployment with one replica) (假设使用一个副本进行部署)

Kubernetes will create second pod and shutdown the previous one - ok so far. Kubernetes 将创建第二个吊舱并关闭前一个吊舱 - 到目前为止还可以。

But immediately after kubectl apply I would like to detect in CI/CD if deployment was successful and in any case (no matter if rollout succeeded or failed) fetch log from one of newly deployed pod in order to provide as much as possible information in CI/CD log to determine what went wrong.但是在 kubectl apply 之后,我想立即在 CI/CD 中检测部署是否成功,并且无论如何(无论推出成功还是失败)从新部署的 pod 之一获取日志,以便在 CI 中提供尽可能多的信息/CD 日志以确定哪里出了问题。

So I'm using所以我正在使用

kubectl rollout status deployment deployment-name

which is waiting for deployment to rollout.这是等待部署推出。 Immediately after though you will end up with two pods, one in status "Running" and another "Terminating".紧接着你会得到两个 pod,一个处于“正在运行”状态,另一个处于“终止”状态。

Now the problematic part: Normally I would use method like现在有问题的部分:通常我会使用类似的方法

kubectl get pods --selector=app=deployment-name --output=jsonpath='{.items[*].metadata.name}' --field-selector=status.phase=Running

but unfortunately it will return names of both pods ("Running" and "Terminating") separated with space.但不幸的是,它将返回两个 pod 的名称(“运行”和“终止”),用空格分隔。

Now I've tried also现在我也试过了

kubectl get pods --selector=app=deployment-name --output=jsonpath='{.items[*].metadata.name}' --field-selector=status.phase=Running,status.phase!=Terminating

according to documentation:根据文档:

but for some reason this will return exactly the same result, both pods running and terminating.但由于某种原因,这将返回完全相同的结果,两个 pod 都在运行和终止。

The quesiton is:问题是:

How to properly exclude TERMINATING pods from the result?如何从结果中正确排除 TERMINATING pod?

Known issue since 2018. Havent resolved yet.自 2018 年以来的已知问题。尚未解决。

Please refer to Kubectl returns pods in various states when only those in Running state are selected via --field-selector, or -o jsonpath for more details.请参考Kubectl 在通过 --field-selector 或 -o jsonpath 选择仅运行 state 时返回各种状态的 Pod以了解更多详细信息。

In short: There is NO normal, SHORT, adequate one-line command that would do what you want.简而言之:没有正常的、简短的、足够的单行命令可以满足您的需求。

Reasons behind that : 背后的原因

This looks like not a bug.这看起来不是一个错误。 kubectl get pods output STATUS column not shows status.phase. kubectl get pods output STATUS 列未显示 status.phase。 kubectl get pods displays table format of PodList object and uses status.containerStatuses states to display STATUS column data. kubectl get pods 显示 PodList object 的表格格式,并使用 status.containerStatuses 状态显示 STATUS 列数据。

Pod phase valid states are pending, running, succeeded, failed and unknown. Pod 阶段有效状态为挂起、运行、成功、失败和未知。 From this document 'Running' state depends on 'restartPolicy'.从此文档“运行”state 取决于“restartPolicy”。

Jsonpath applies on 'PodList' object, not on kubectl output. Jsonpath 适用于“PodList”object,不适用于 kubectl output。 status.containerStatuses gives whether pod containers running or not. status.containerStatuses 给出 pod 容器是否运行。 Field selectors vary by Kubernetes resource types.字段选择器因 Kubernetes 资源类型而异。 status.containerStatuses not supported in pods field selectors. pod 字段选择器不支持 status.containerStatuses。

Workarounds:解决方法:

1. kubectl get pods | grep Running 1. kubectl get pods | grep Running kubectl get pods | grep Running

2. kubectl get pods -o jsonpath='{.items[*].status.containerStatuses[*].state.running},{.items[*].metadata.name}' --field-selector=status.phase==Running | sed 's/ /\n/' | grep startedAt | awk -F',' '{print $2}' 2. kubectl get pods -o jsonpath='{.items[*].status.containerStatuses[*].state.running},{.items[*].metadata.name}' --field-selector=status.phase==Running | sed 's/ /\n/' | grep startedAt | awk -F',' '{print $2}' kubectl get pods -o jsonpath='{.items[*].status.containerStatuses[*].state.running},{.items[*].metadata.name}' --field-selector=status.phase==Running | sed 's/ /\n/' | grep startedAt | awk -F',' '{print $2}'

3. ( source ) kubectl get pods --field-selector=status.phase=Running --template {{range.items}}{{ if not.metadata.deletionTimestamp }}{{.metadata.name}}{{"\n"}}{{end}}{{end}} 3. ( source ) kubectl get pods --field-selector=status.phase=Running --template {{range.items}}{{ if not.metadata.deletionTimestamp }}{{.metadata.name}}{{"\n"}}{{end}}{{end}}

from the command line you can use commands like从命令行你可以使用类似的命令

grep

to catch what you like or you can do what I do and just look at all the pods with抓住你喜欢的东西,或者你可以做我做的事情,然后看看所有的豆荚

watch 

command to see the changes and look at the errors from a separate terminal.命令以查看更改并从单独的终端查看错误。

What I use for these kind of situations:我在这些情况下使用什么:

watch -n0.5 " kubectl get pods | grep -v 'Running' "

to catch pods with non Running status (Completed, OOMKilled, Pending, etc.) then from a separate terminal然后从单独的终端捕获具有运行状态(已完成、OOMKilled、待处理等)的 pod

kubectl logs podName && kubectl describe pods podName

I hope I was a little help.我希望我能帮上一点忙。

Even for getting the latest revision number I use即使是为了获取我使用的最新版本号

kubectl rollout history deployment.v1.apps/deploymentName | tail -2 | head -1 | awk '{print $1}'

But maybe you can use但也许你可以使用

kubectl get pods  --sort-by=.metadata.creationTimestamp 

and then take the last one so finally maybe, you can use然后拿最后一个,所以最后也许,你可以使用

kubectl logs -c [containerName] $(kubectl get pods  --sort-by=.metadata.creationTimestamp | tail -1 | awk '{print$1}') 

this can give me the latest created pod and its logs这可以给我最新创建的 pod 及其日志

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM