当有其他 pods 终止时如何获取 运行 pod 名称?

How to get the running pod name when there are other pods terminating?

我正在使用 kubernetes 集群 运行 为我自己和其他开发人员开发环境。我已经编写了一些 shell 函数来帮助每个人处理他们的 pods 而无需手动输入长长的 kubectl 命令。例如,要在 pods 之一上获得提示,我的函数使用以下

kubectl exec -it $(kubectl get pods --selector=run=${service} --field-selector=status.phase=Running -o jsonpath="{.items[*].metadata.name}") -- bash;

其中 $service 设置为我想要访问的服务标签,例如 postgres 或 redis 或 uwsgi。

由于这些是开发环境,因此每种类型 pods 总是各有其一。我遇到的问题是,如果我删除一个 pod 以使其拉出新图像(所有 pods 都由部署管理,所以如果我删除一个 pod,它将创建一个新的),有一段时间两个 pods,一个在 kubectl get pods 输出中显示为终止,另一个显示为 运行ning。我想确保上面的命令选择了 运行ning 的 pod,而不是终止的 pod。我以为 --field-selector=status.phase=Running flag 可以做到,但事实并非如此。显然,即使 pod 正在终止,它仍然会在 status.phase 字段中报告 运行 状态。我可以使用什么来过滤终止 pods?

用这个

$ kubectl exec -it $(kubectl get pods --selector=run=${service} | grep "running" | awk '{print }') -- bash;

$ kubectl exec -it $(kubectl get pods --selector=run=${service} -o=jsonpath='{.items[?(@.status.phase==“Running”)].metadata.name}') -- bash;

参考:https://kubernetes.io/docs/reference/kubectl/jsonpath/