在kubectl推出状态后获取新的吊舱名称之一|kubectl get pods --field-selector返回终止pods [英] get one of new pod name after kubectl rollout status | kubectl get pods --field-selector returns Terminating pods

查看:297
本文介绍了在kubectl推出状态后获取新的吊舱名称之一|kubectl get pods --field-selector返回终止pods的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题在于,在部署新的部署后,例如

The problem is that after deploying new deployment like

kubectl apply -f deployment.yml

(假设有一个副本进行部署)

(let's say deployment with one replica)

Kubernetes将创建第二个Pod并关闭前一个Pod-到目前为止,还可以.

Kubernetes will create second pod and shutdown the previous one - ok so far.

但是在kubectl应用后,我想立即在CI/CD中检测部署是否成功,以及在任何情况下(无论部署成功还是失败)都从新部署的Pod中获取日志,以便提供尽可能多的服务CI/CD日志中的信息,以确定出了什么问题.

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 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".

现在有问题的部分:通常我会使用类似的方法

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

但不幸的是,它将返回两个以空格分隔的Pod的名称(运行中"和终止中").

but unfortunately it will return names of both pods ("Running" and "Terminating") separated with space.

现在我也尝试过

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

根据文档:

但是由于某种原因,这两个Pod都将运行并终止,它们返回的结果将完全相同.

but for some reason this will return exactly the same result, both pods running and terminating.

问题是:

如何从结果中正确排除TERMINATING吊舱?

How to properly exclude TERMINATING pods from the result?

推荐答案

自2018年以来已知的问题.Havent解决了.

Known issue since 2018. Havent resolved yet.

请参考当通过-仅选择处于运行"状态的Pod时,Kubectl返回处于各种状态的Pod-field-selector或-o jsonpath 了解更多详细信息.

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.

简而言之:没有正常的,简短的,足够的单行命令可以执行您想要的操作.

In short: There is NO normal, SHORT, adequate one-line command that would do what you want.

背后的原因:

这似乎不是错误.kubectl获取荚输出状态列不显示status.phase.kubectl get pods显示PodList的表格格式对象并使用status.containerStatuses状态显示STATUS列数据.

This looks like not a bug. kubectl get pods output STATUS column not shows status.phase. kubectl get pods displays table format of PodList object and uses status.containerStatuses states to display STATUS column data.

Pod阶段有效状态为挂起,运行,成功,失败和未知.在此文档中,运行"状态取决于"restartPolicy".

Pod phase valid states are pending, running, succeeded, failed and unknown. From this document 'Running' state depends on 'restartPolicy'.

Jsonpath适用于"PodList"对象,而不适用于kubectl输出.status.containerStatuses给出Pod容器是否正在运行.字段选择器因Kubernetes资源类型而异.pod字段选择器不支持status.containerStatus.

Jsonpath applies on 'PodList' object, not on kubectl output. status.containerStatuses gives whether pod containers running or not. Field selectors vary by Kubernetes resource types. status.containerStatuses not supported in pods field selectors.

解决方法:

1.. kubectl获取广告连播|grep正在运行

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

3.() kubectl get pods --field-selector = status.phase = Running --template {{range .items}} {{如果不是.metadata.deletionTimestamp}} {{.metadata.name}} {{\ n"}} {{end}} {{end}}

这篇关于在kubectl推出状态后获取新的吊舱名称之一|kubectl get pods --field-selector返回终止pods的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆