前言
Pod在整个生命周期中被定义为各种状态,熟悉各种状态是很有必要的。
主要阶段
我们先用一张图来看看Pod状态机
下面,我们展开解释一下每个状态。
-
Pending
- API Server已经创建该Pod,但是一个或多个容器尚未创建
- 通常这个阶段是因为系统正在为Pod分配所需资源,如下载容器镜像或调度到特定的节点上。
-
Running
- 所有容器都被创建,并且至少有一个容器正在运行、正在启动或正在重启。
- 这个阶段,Pod已经被分配到一个节点上,并且所有的容器都已经开始运行。
-
Succeeded
- Pod中所有容器都正常运行并已经退出,且不会再重启。
- 这个阶段意味着Pod的任务已经成功完成。
-
Failed
- Pod中的所有容器都已终止,至少有一个容器是因为失败而终止的
- 失败可能是因为容器崩溃或容器健康检查失败。
-
Unknown
- 因为某些原因,Pod的状态无法被Kubernetes系统确定
- 这可能是由于与Pod所在节点的通信问题导致的。
这些状态就是Pod Phase。可以通过如下命令查看,看个案例 我现在有两个pod,执行kubectl get po -n nginx ,输出如下:
NAME READY STATUS RESTARTS AGE
nginx-deployment-6d4d684958-6x8b5 0/1 ImagePullBackOff 0 23d
tomcat-deployment-ddd48b668-hmds6 0/1 Pending 0 80d
可以看到通过get得到状态分别是ImagePullBackOff和Pending。
先来看看nginx-deployment-6d4d684958-6x8b5这个pod的详细信息,执行命令kubectl get po nginx-deployment-6d4d684958-6x8b5 -n nginx -oyaml
status:
conditions:
- lastProbeTime: null
lastTransitionTime: "2024-10-11T06:48:09Z"
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: "2024-10-11T06:48:09Z"
message: 'containers with unready status: [nginx]'
reason: ContainersNotReady
status: "False"
type: Ready
- lastProbeTime: null
lastTransitionTime: "2024-10-11T06:48:09Z"
message: 'containers with unready status: [nginx]'
reason: ContainersNotReady
status: "False"
type: ContainersReady
- lastProbeTime: null
lastTransitionTime: "2024-10-11T06:48:09Z"
status: "True"
type: PodScheduled
containerStatuses:
- image: nginx
imageID: ""
lastState: {}
name: nginx
ready: false
restartCount: 0
started: false
state:
waiting:
message: Back-off pulling image "nginx"
reason: ImagePullBackOff
hostIP: 10.0.0.102
phase: Pending
podIP: 10.244.2.106
podIPs:
- ip: 10.244.2.106
qosClass: Burstable
startTime: "2024-10-11T06:48:09Z"
可以看到虽然通过kubectl get po得到的状态为ImagePullBackOff ,但phase的状态为Pending.
phase和status conditions
kubectl get po得到的状态信息是由phase和status conditions共同决定的。
-
Pod Phase 是一个高级汇总,它提供了 Pod 当前状态的简单宏观概述。Pod 的 phase 值就是我们上面提到的5个状态。
-
Pod Conditions 提供了更详细的状态信息,解释了 Pod 为什么处于当前的 phase。Conditions 是一组布尔值,每个条件都有一个 type 和 status。type 描述了条件的名称,而 status 表明该条件是否为 True、False 或 Unknown。例如:
- PodScheduled:表明 Pod 是否已经被调度到某个节点。
- Initialized:表明 Pod 的所有初始化容器是否已经成功完成。
- ContainersReady:表明 Pod 中的所有容器是否已经就绪,但这并不意味着 Pod 已经准备好服务请求。
- Ready:表明 Pod 是否准备好接受流量,即所有容器都已就绪,并且 readiness probe 也已完成
参照表
| kubectl get po返回状态 | phase | conditions |
|---|---|---|
| Completed | Succeeded | |
| ContainerCreating | Pending | |
| CreateContainerConfigError | Pending | configmap "test" not found secret "my-secret" not found |
| ErrImagePull ImagePullBackOff Init:ImagePullBackOff InvalidImageName | Pending | Back-off pulling image |
| Init:0/1 | Pending | Init containers don't exit |
| Init:CrashLoopBackOff Init:Error | Pending | Init containers crashed(exit with not 1) |
| Error | Failed | restartPolicy:Never container exits with Error(not 0) |
| Evicted | Failed | message: 'Usage of EmptyDir volume "myworkdir" exceeds the limit "40Gi".' reason:Evicted |
| OutOfCpu/OutOfMemory | Failed | Scheduled, but it cannot pass kubelet admit |
| CrashLoopBackOff | Running | Container exits |
| OOMKilled | Running | Containers are OOMKilled |
| StartError | Running | Containers cannot be started |
| Unknown | Running | Node NotReady |
最后
要熟悉这些状态,看到不同状态和条件,就基本可以定位问题所在。