想通过 kubectl get pods 命令列出所有非 Running 状态的 pod,使用下面的命令
$ kubectl get pods --field-selector status.phase!=Running
输出却是
No resources found in production namespace.
实际有不少处于 CrashLoopBackOff 与 Terminating 状态的 pod,而使用 status.phase=Running 参数
$ kubectl get pods --field-selector status.phase=Running
却会列出所有 Running 状态与非 Running 状态的pod。
请问如何解决这个问题?
在博文 List all failed Pods in a namespace with kubectl 中找到了原因:
kubectl get pods says the Pod is in CrashLoopBackOff, but when manually printing out the field the Pod is running? It turns out that the .status.phase field actually describes the scheduling state, not the actual state.
与解决方法:
$ kubectl get pods -o custom-columns="POD:metadata.name,STATE:status.containerStatuses[*].state.waiting.reason" | grep -v none
回答中的命令不会列出处于 Terminating 状态的 pod,后来受 grep -v none 的启发(v=invert-match),找到了最简单的命令
kubectl get pods | grep -v Running