兩張思維導圖,幫你理清 K8s 中 Pod 的 phase 和 conditions
原文:
http://www.knockatdatabase.com/2022/05/21/kubernetes-pod-phase-and-pod-conditions/
pod 的 phase
phase 作用
用於描述、查看、分析 pod 當前處於什麼狀態。
有哪些 phase
通常情況下,在 pod 的生命週期中,每個 pod 會處於 5 個不同的 phase:pending,running,succeed,failed,unknown。同一時間,1 個 pod 只能處於 1 個 phase。
-
當 pod 剛被創建時,它處於 pending 這個 phase,等待被調度;
-
如果 pod 中的一個或多個 container 處於運行狀態時,那麼 pod 就處於 running phase;
-
如果 pod 中的 container 不是被設置爲無限運行下去的情況下 (比如執行定時任務或一次性任務),且 container 運行結束,那麼 pod 處於 succeed phase;
-
反之,如果 pod 中的 container 不是被設置爲無限運行下去的情況下 (比如執行定時任務或一次性任務),且 container 運行失敗,那麼 pod 處於 failed phase;
-
如果 pod 所在 node 上的 kubelet 出現故障或意外,而停止向 Kubernetes API server 報告它所在 node 上的 pod 的狀態時,那麼此時該 node 上的 pod 就處於 unknown phase;
如何查看 pod 的 phase
由於 pod 的 phase 字段位於 pod 的 manifest 中的 Status 部分,也就是說 ,我們可以從 Kubernetes API server 那裏獲取 pod 的 yaml 文件,然後從 status 字段中找到 pod 的 phase。那麼,我們就可以,通過 kubectl get pod pod_name -o yaml|grep phase 來查看 pod 的 phase:
[root@master-node ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
curl 1/1 Running 0 6d9h
curl-with-ambassador 2/2 Running 0 28d
downward 1/1 Running 0 28d
fortune-configmap-volume 2/2 Running 0 36d
fortune-https 2/2 Running 0 35d
my-job-jfhz9 0/1 Completed 0 6d9h
[root@master-node ~]# kubectl get pods curl -o yaml|grep phase
phase: Running
[root@master-node ~]# kubectl get pods my-job-jfhz9 -o yaml|grep phase
phase: Succeeded
[root@master-node ~]#
從上,我們通過 pod 的 yaml 文件裏獲取了它們的 phase。其中的 my-job-jfhz9 是一個 job 且已經執行完成。所以,它處於 succeed 的 phase。
pod 的 conditions
pod 有了 phase,爲什麼還要有 conditions
因爲 pod 的 phase 比較簡單的描述了 pod 處於哪個具體情況,但是沒有明確說明具體原因。
pod 的 conditions 的作用
用於描述 1 個 pod 當前是否處於哪個 phase,以及處於該 phase 的原因。及作爲一個輔助手段,詳細的展示 pod 的狀態信息,用於問題排查分析時提供更多依據。同一時間,1 個 pod 可能處於多個 conditions。
pod 的 conditions 分類
通常分爲 4 個 conditions:PodScheduled,Initialized,ContainersReady,Ready。見名知意:
-
PodScheduled:意味着 pod 是否已經被調度到某個 node;
-
Initialized:Pod 的 init containers 是否全部完成;
-
ContainersReady:pod 中的所有 container 是否全部就緒;但這並不意味着 pod 也 ready;
-
Ready:pod 是否就緒;只有 pod 中的所有 container 就緒,且 pod 的 readiness probe 也完成了,意味着 pod 可以對外提供服務了,纔是 ready 狀態。
如何查看 pod 的 conditions
同樣,由於 pod 的 conditions 源於 yaml 格式的 manifest 中的 Status 字段,我們可以從 yaml 文件裏查看。
[root@master-node ~]# kubectl get pods curl -o yaml
apiVersion: v1
kind: Pod
...
status:
conditions:
- lastProbeTime: null
lastTransitionTime: "2022-05-09T15:23:37Z"
status: "True"
type: Initialized #conditions狀態2
- lastProbeTime: null
lastTransitionTime: "2022-05-09T15:23:51Z"
status: "True"
type: Ready #conditions狀態4
- lastProbeTime: null
lastTransitionTime: "2022-05-09T15:23:51Z"
status: "True"
type: ContainersReady #conditions狀態3
- lastProbeTime: null
lastTransitionTime: "2022-05-09T15:23:37Z"
status: "True"
type: PodScheduled #conditions狀態1
containerStatuses:
- containerID: docker://9d56be349349b7581a4178b11895167b5be8c1c68ce1630f440389a1e8257a35
image: docker.io/rancher/curl:latest
imageID: docker-pullable://docker.io/rancher/curl@sha256:85aea1846e2e9b921629e9c3adf0c5aa63dbdf13aa84d4dc1b951982bf42d1a4
lastState: {}
name: main
ready: true
restartCount: 0
started: true
state:
running:
startedAt: "2022-05-09T15:23:50Z"
hostIP: 172.16.11.161
phase: Running
podIP: 10.244.2.245
podIPs:
- ip: 10.244.2.245
qosClass: BestEffort
startTime: "2022-05-09T15:23:37Z"
[root@master-node ~]#
從上,我們可以從 yaml 中的 Status 字段裏的 conditions 字段看到 pod 的 4 個 conditions。
同樣,我們也可以通過 kubectl describe pod 來獲取 conditions:kubectl describe pod pod_name|grep Conditions: -A 5
[root@master-node ~]# kubectl describe pod curl|grep Conditions: -A 5
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
[root@master-node ~]# kubectl describe pod my-job-jfhz9|grep Conditions: -A5
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
[root@master-node ~]#
表示要過濾 Conditions: 字段,然後,-A5 表示緊隨其後的 5 行。-A 5 之間也可以留空格,不影響結果。
比如:上述我們看到的類型爲 job 的 pod my-job-jfhz9 它的 conditions 中,有多個是 False 的。爲什麼呢?我們同樣,可以從 kubectl get pods pod_name -oyaml 裏查看到類似下述的說明信息:
conditions:
- lastProbeTime: null
lastTransitionTime: "2022-05-09T15:22:23Z"
reason: PodCompleted
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: "2022-05-09T15:24:44Z"
reason: PodCompleted
status: "False"
type: Ready
- lastProbeTime: null
lastTransitionTime: "2022-05-09T15:24:44Z"
reason: PodCompleted
status: "False"
type: ContainersReady
- lastProbeTime: null
lastTransitionTime: "2022-05-09T15:22:19Z"
status: "True"
type: PodScheduled
我們,從 reason 字段裏,看到 pod 處於某個狀態下的具體原因。原來,ContainersReady 的狀態爲 false 的原因,是 PodCompleted 了。
其中的 lastProbeTime 表示的該 conditions 在什麼時間被檢查過,名字中雖然有 probe,但是它跟 probe 沒有關係;
lastTransitionTime 表示該 conditions 在什麼時間點兒發生的變化;
小結
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/f6vKgBBuMaO5mVYDbhQl1w