pod從創(chuàng)建到最后的創(chuàng)建成功會分別處于不同的階段,在源碼中用PodPhase來表示不同的階段:

運行中(PodRunning):該 Pod 已經(jīng)綁定到了一個節(jié)點上,Pod 中所有的容器都已被創(chuàng)建。至少有一個容器正在運行,或者正處于啟動或重啟狀態(tài)。
等待中(PodPending): Pod 已被 Kubernetes 系統(tǒng)接受,但有一個或者多個容器鏡像尚未創(chuàng)建。等待時間包括調(diào)度 Pod 的時間和通過網(wǎng)絡(luò)下載鏡像的時間,這可能需要花點時間。
創(chuàng)建pod的請求已經(jīng)被k8s接受,但是容器并沒有啟動成功,可能處在:寫數(shù)據(jù)到etcd,調(diào)度,pull鏡像,啟動容器這四個階段中的任何一個階段,pending伴隨的事件通常會有:ADDED, Modified這兩個事件的產(chǎn)生。
正常終止(PodSucceeded):pod中的所有的容器已經(jīng)正常的自行退出,并且k8s永遠(yuǎn)不會自動重啟這些容器,一般會是在部署job的時候會出現(xiàn)。
異常停止(PodEailed):Pod 中的所有容器都已終止了,并且至少有一個容器是因為失敗終止。也就是說,容器以非0狀態(tài)退出或者被系統(tǒng)終止。
未知狀態(tài)(PodUnkonwn):出于某種原因,無法獲得Pod的狀態(tài),通常是由于與Pod主機(jī)通信時出錯。

一個pod的完整創(chuàng)建,通常會伴隨著各種事件的產(chǎn)生,k8s種事件的種類總共只有4種:
Added EventType = "ADDED"
Modified EventType = "MODIFIED"
Deleted EventType = "DELETED"
Error EventType = "ERROR"
PodStatus 有一組PodConditions。?PodCondition中的ConditionStatus,它代表了當(dāng)前pod是否處于某一個階段(PodScheduled,Ready,Initialized,Unschedulable),“true” 表示處于,“false”表示不處于。
PodCondition數(shù)組的每個元素都有一個類型字段和一個狀態(tài)字段。
類型字段 PodConditionType? 是一個字符串,可能的值是
PodScheduled:pod正處于調(diào)度中,剛開始調(diào)度的時候,hostip還沒綁定上,持續(xù)調(diào)度之后,有合適的節(jié)點就會綁定hostip,然后更新etcd數(shù)據(jù)
Ready: pod 已經(jīng)可以開始服務(wù),譬如被加到負(fù)載均衡里面
Initialized:所有pod 中的初始化容器已經(jīng)完成了
Unschedulable:限制不能被調(diào)度,譬如現(xiàn)在資源不足
狀態(tài)字段 ConditionStatus? 是一個字符串,可能的值為True,F(xiàn)alse和Unknown


容器的狀態(tài):
????ContainerStateCreated ContainerState = "created"
????ContainerStateRunning ContainerState = "running"
????ContainerStateExited? ContainerState = "exited"
????// This unknown encompasses all the states that we currently don't care.
????ContainerStateUnknown ContainerState = "unknown"


舉一個例子來說明一下情況
kubectl apply -f busybox.yaml
1.?寫入數(shù)據(jù)到etcd
event type: ADDED event object:
{
? ? "phase": "Pending",
? ? "qosClass": "BestEffort"
}
2.?開始被調(diào)度,但是還未調(diào)度到具體node上,請注意:PodScheduled的status=“true”
event type: MODIFIED
{
????"phase": "Pending",
????"conditions": [
????{
????????"type": "PodScheduled",
????????"status": "True",
????????"lastProbeTime": null,
????????"lastTransitionTime": "2017-06-06T07:57:06Z"
? ?} ],
????"qosClass": "BestEffort"
}
3.? 被調(diào)度到了具體的node上hostip綁定了,并且被所有初始化容器已經(jīng)啟動完畢(注意busybox.yaml中pod沒有指定init container,所以這里很快就被設(shè)置成true),被調(diào)度到的節(jié)點watch到并開始創(chuàng)建容器(此階段是在拉去鏡像)然后創(chuàng)建容器 ,而此時Ready的status是false,仔細(xì)看會發(fā)現(xiàn),containerStatus的狀態(tài)為waiting
event type:MODIFIED{
? "phase": "Pending",
? "conditions": [
? ? {
? ? ? "type": "Initialized",
? ? ? "status": "True",
? ? ? "lastProbeTime": null,
? ? ? "lastTransitionTime": "2017-06-06T07:57:06Z"? ? },
? ? {
? ? ? "type": "Ready",
? ? ? "status": "False",
? ? ? "lastProbeTime": null,
? ? ? "lastTransitionTime": "2017-06-06T07:57:06Z",
? ? ? "reason": "ContainersNotReady",
? ? ? "message": "containers with unready status: [busybox]"? ? },
? ? {
? ? ? "type": "PodScheduled",
? ? ? "status": "True",
? ? ? "lastProbeTime": null,
? ? ? "lastTransitionTime": "2017-06-06T07:57:06Z"? ? }
? ],
? "hostIP": "10.39.1.35",
? "startTime": "2017-06-06T07:57:06Z",
? "containerStatuses": [
? ? {
? ? ? "name": "busybox",
? ? ? "state": {
? ? ? ? "waiting": {
? ? ? ? ? "reason": "ContainerCreating"? ? ? ? }
? ? ? },
? ? ? "lastState": {},
? ? ? "ready": false,
? ? ? "restartCount": 0,
? ? ? "image": "busybox",
? ? ? "imageID": ""? ? }
? ],
? "qosClass": "BestEffort"
}
4.?容器創(chuàng)建成功,Ready的status=“true”,此時容器的status也為running,這個時候,對應(yīng)的pod的PodPhase也應(yīng)該為running
event type:MODIFIED{
? "phase": "Running",
? "conditions": [
? ? {
? ? ? "type": "Initialized",
? ? ? "status": "True",
? ? ? "lastProbeTime": null,
? ? ? "lastTransitionTime": "2017-06-06T07:57:06Z"? ? },
? ? {
? ? ? "type": "Ready",
? ? ? "status": "True",
? ? ? "lastProbeTime": null,
? ? ? "lastTransitionTime": "2017-06-06T07:57:08Z"? ? },
? ? {
? ? ? "type": "PodScheduled",
? ? ? "status": "True",
? ? ? "lastProbeTime": null,
? ? ? "lastTransitionTime": "2017-06-06T07:57:06Z"? ? }
? ],
? "hostIP": "10.39.1.35",
? "podIP": "192.168.107.204",
? "startTime": "2017-06-06T07:57:06Z",
? "containerStatuses": [
? ? {
? ? ? "name": "busybox",
? ? ? "state": {
? ? ? ? "running": {
? ? ? ? ? "startedAt": "2017-06-06T07:57:08Z"? ? ? ? }
? ? ? },
? ? ? "lastState": {},
? ? ? "ready": true,
? ? ? "restartCount": 0,
? ? ? "image": "busybox:latest",
? ? ? "imageID": "docker-pullable://busybox@sha256:c79345819a6882c31b41bc771d9a94fc52872fa651b36771fbe0c8461d7ee558",
? ? ? "containerID": "docker://a6af9d58c7dabf55fdfe8d4222b2c16349e3b49b3d0eca4bc761fdb571f3cf44"? ? }
? ],
? "qosClass": "BestEffort"
}
CrashLoopBackOff: 容器退出,kubelet正在將它重啟
InvalidImageName: 無法解析鏡像名稱
ImageInspectError: 無法校驗鏡像
ErrImageNeverPull: 策略禁止拉取鏡像
ImagePullBackOff: 正在重試?yán)?/p>
RegistryUnavailable: 連接不到鏡像中心
ErrImagePull: 通用的拉取鏡像出錯
CreateContainerConfigError: 不能創(chuàng)建kubelet使用的容器配置
CreateContainerError: 創(chuàng)建容器失敗
m.internalLifecycle.PreStartContainer? 執(zhí)行hook報錯
RunContainerError: 啟動容器失敗
PostStartHookError: 執(zhí)行hook報錯
ContainersNotInitialized: 容器沒有初始化完畢
ContainersNotReady: 容器沒有準(zhǔn)備完畢
ContainerCreating:容器創(chuàng)建中
PodInitializing:pod 初始化中
DockerDaemonNotReady:docker還沒有完全啟動
NetworkPluginNotReady: 網(wǎng)絡(luò)插件還沒有完全啟動