gpt4 book ai didi

kubernetes - kubectl status.phase=运行返回错误结果

转载 作者:行者123 更新时间:2023-12-04 16:24:05 25 4
gpt4 key购买 nike

当我运行时:

kubectl get pods --field-selector=status.phase=Running
我懂了:
NAME          READY   STATUS    RESTARTS   AGE
k8s-fbd7b 2/2 Running 0 5m5s
testm-45gfg 1/2 Error 0 22h
我不明白为什么这个命令会给我处于错误状态的 pod?
根据 K8S api ,没有这种东西 STATUS=Error .
如何仅获取处于此错误状态的 Pod?
当我运行时:
kubectl get pods --field-selector=status.phase=Failed
它告诉我没有处于该状态的 Pod。

最佳答案

使用 kubectl get pods --field-selector=status.phase=Failed命令可以显示 Failed 中的所有 Pod阶段。Failed表示 Pod 中的所有容器都已终止,并且至少有一个容器因失败而终止(参见:Pod phase):

Failed - All containers in the Pod have terminated, and at least one container has terminated in failure. That is, the container either exited with non-zero status or was terminated by the system.


在您的示例中,两个 Pod 都在 Running 中阶段,因为至少有一个容器仍在这些 Pod 中运行。:

Running - The Pod has been bound to a node, and all of the containers have been created. At least one container is still running, or is in the process of starting or restarting.


您可以使用以下命令检查 Pod 的当前阶段:
$ kubectl get pod -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.phase}{"\n"}{end}'
让我们检查一下这个命令是如何工作的:
$ kubectl get pods
NAME READY STATUS
app-1 1/2 Error
app-2 0/1 Error

$ kubectl get pod -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.phase}{"\n"}{end}'
app-1 Running
app-2 Failed
如您所见,只有 app-2 Pod 在 Failed阶段。 app-1中还有一个容器在运行Pod,所以这个 Pod 在 Running阶段。
列出所有带有 Error 的 Pod状态,您可以简单地使用:
$ kubectl get pods -A | grep Error
default app-1 1/2 Error
default app-2 0/1 Error
此外,值得一提的是,您可以检查 Pod 中所有容器的状态:
$ kubectl get pod -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.containerStatuses[*].state}{"\n"}{end}'
app-1 {"terminated":{"containerID":"containerd://f208e2a1ff08c5ce2acf3a33da05603c1947107e398d2f5fbf6f35d8b273ac71","exitCode":2,"finishedAt":"2021-08-11T14:07:21Z","reason":"Error","startedAt":"2021-08-11T14:07:21Z"}} {"running":{"startedAt":"2021-08-11T14:07:21Z"}}
app-2 {"terminated":{"containerID":"containerd://7a66cbbf73985efaaf348ec2f7a14d8e5bf22f891bd655c4b64692005eb0439b","exitCode":2,"finishedAt":"2021-08-11T14:08:50Z","reason":"Error","startedAt":"2021-08-11T14:08:50Z"}}

关于kubernetes - kubectl status.phase=运行返回错误结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68740515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com