背景
最近打算使用 k8s api server
的 api
来监控 pod 状态变更事件,所以顺带研究了一下 pod 状态变化
k8s api
k8s 的 这个 list-watch
很牛逼,把 HTTP/1.1
玩出花来了
知乎
使用 curl
curl -v -k https://172.18.xxxx:6443/api/v1/namespaces/backend/pods?watch=true -H "Authorization: Bearer eyJxxxxxx"
![](https://img.haomeiwen.com/i13864094/4dd119eec202b438.png)
我的这个pod
副本数量为1
逐个分析
0x1 ADDED 新pod
{
"type": "ADDED",
"object": {
"kind": "Pod",
"apiVersion": "v1",
"metadata": {
"name": "cayenne-598b9fd477-r8qvm"
},
"status": {
"phase": "Pending",
"qosClass": "Burstable"
}
}
}
创建新的 pod
, 名字是 cayenne-598b9fd477-r8qvm
0x2 MODIFIED 新pod
{
"type": "MODIFIED",
"object": {
"kind": "Pod",
"apiVersion": "v1",
"metadata": {
"name": "cayenne-598b9fd477-r8qvm"
},
"status": {
"phase": "Pending",
"conditions": [{
"type": "PodScheduled",
"status": "True",
"lastProbeTime": null,
"lastTransitionTime": "2020-09-23T07:15:34Z"
}
],
"qosClass": "Burstable"
}
}
}
分析
状态更新
PodScheduled
意思是把pod 调度到节点node上
pod 状态为 Pending
容器已调度到节点上了,此时正在做一些比如 pull docker image 之类的事情
0x3 MODIFIED 新pod
{
"type": "MODIFIED",
"object": {
"kind": "Pod",
"apiVersion": "v1",
"metadata": {
"name": "cayenne-598b9fd477-r8qvm",
},
"status": {
"phase": "Pending",
"conditions": [{
"type": "Initialized",
"status": "True",
"lastProbeTime": null,
"lastTransitionTime": "2020-09-23T07:15:34Z"
}, {
"type": "Ready",
"status": "False",
"reason": "ContainersNotReady",
"message": "containers with unready status: [cayenne]"
}, {
"type": "ContainersReady",
"status": "False",
"lastProbeTime": null,
"lastTransitionTime": "2020-09-23T07:15:34Z",
"reason": "ContainersNotReady",
"message": "containers with unready status: [cayenne]"
}, {
"type": "PodScheduled",
"status": "True",
}
],
"hostIP": "172.18.237.190",
"startTime": "2020-09-23T07:15:34Z",
"containerStatuses": [{
"name": "cayenne",
"state": {
"waiting": {
"reason": "ContainerCreating"
}
},
"lastState": {},
"ready": false,
"restartCount": 0,
}
],
"qosClass": "Burstable"
}
}
}
分析
状态更新
PodScheduled: true
pod 已调度到节点上
Initialized: true
所有的 init containers 启动成功
Ready: false
pod里面的容器,不是所有的都就绪了
ContainersReady: false
这个pod还没有准备好接收service的请求,此时也不参与service的负载均衡
此时容器的状态是 waiting
reason: ContainerCreating
说明当前处于容器启动过程
pod 此时状态是 Pending
容器已调度到节点上
init containers 全部启动完成
容器状态为 waiting,此时正在创建容器
0x4 MODIFIED 新pod
{
"type": "MODIFIED",
"object": {
"kind": "Pod",
"apiVersion": "v1",
"metadata": {
"name": "cayenne-598b9fd477-r8qvm",
},
"status": {
"phase": "Running",
"conditions": [{
"type": "Initialized",
"status": "True",
"lastProbeTime": null,
"lastTransitionTime": "2020-09-23T07:15:34Z"
}, {
"type": "Ready",
"status": "False",
"lastProbeTime": null,
"lastTransitionTime": "2020-09-23T07:15:34Z",
"reason": "ContainersNotReady",
"message": "containers with unready status: [cayenne]"
}, {
"type": "ContainersReady",
"status": "False",
"lastProbeTime": null,
"lastTransitionTime": "2020-09-23T07:15:34Z",
"reason": "ContainersNotReady",
"message": "containers with unready status: [cayenne]"
}, {
"type": "PodScheduled",
"status": "True",
"lastProbeTime": null,
"lastTransitionTime": "2020-09-23T07:15:34Z"
}
],
"containerStatuses": [{
"name": "cayenne",
"state": {
"running": {
"startedAt": "2020-09-23T07:15:40Z"
}
},
"ready": false,
}
],
"qosClass": "Burstable"
}
}
}
分析
容器状态为 running,创建完成,正处于启动过程
pod 此时状态是 Running,但是没有就绪
0x5 MODIFIED 新pod
{
"type": "MODIFIED",
"object": {
"kind": "Pod",
"apiVersion": "v1",
"metadata": {
"name": "cayenne-598b9fd477-r8qvm",
},
"status": {
"phase": "Running",
"conditions": [{
"type": "Initialized",
"status": "True",
"lastProbeTime": null,
"lastTransitionTime": "2020-09-23T07:15:34Z"
}, {
"type": "Ready",
"status": "True",
"lastProbeTime": null,
"lastTransitionTime": "2020-09-23T07:15:50Z"
}, {
"type": "ContainersReady",
"status": "True",
"lastProbeTime": null,
"lastTransitionTime": "2020-09-23T07:15:50Z"
}, {
"type": "PodScheduled",
"status": "True",
"lastProbeTime": null,
"lastTransitionTime": "2020-09-23T07:15:34Z"
}
],
"containerStatuses": [{
"name": "cayenne",
"state": {
"running": {
"startedAt": "2020-09-23T07:15:40Z"
}
},
"lastState": {},
"ready": true,
}
],
"qosClass": "Burstable"
}
}
}
分析
容器状态为 running,创建完成,启动完成,通过了就绪检查
pod 此时状态是 Running,且已经就绪,可以开始参与服务请求
0x6 MODIFIED 老pod
{
"type": "MODIFIED",
"object": {
"kind": "Pod",
"apiVersion": "v1",
"metadata": {
"name": "cayenne-6cdf75c8b7-rtbgg",
},
"status": {
"phase": "Running",
"conditions": [{
"type": "Initialized",
"status": "True",
"lastProbeTime": null,
"lastTransitionTime": "2020-09-23T07:13:52Z"
}, {
"type": "Ready",
"status": "True",
"lastProbeTime": null,
"lastTransitionTime": "2020-09-23T07:14:04Z"
}, {
"type": "ContainersReady",
"status": "True",
"lastProbeTime": null,
"lastTransitionTime": "2020-09-23T07:14:04Z"
}, {
"type": "PodScheduled",
"status": "True",
"lastProbeTime": null,
"lastTransitionTime": "2020-09-23T07:13:52Z"
}
],
"containerStatuses": [{
"name": "cayenne",
"state": {
"running": {
"startedAt": "2020-09-23T07:13:53Z"
}
},
"ready": true,
}
],
"qosClass": "Burstable"
}
}
}
分析
老 pod 此时状态是 Running, 开始准备变化了
0x7 MODIFIED 老pod
{
"type": "MODIFIED",
"object": {
"kind": "Pod",
"apiVersion": "v1",
"metadata": {
"name": "cayenne-6cdf75c8b7-rtbgg",
},
"status": {
"phase": "Pending",
"conditions": [{
"type": "Initialized",
"status": "True",
"lastProbeTime": null,
"lastTransitionTime": "2020-09-23T07:13:52Z"
}, {
"type": "Ready",
"status": "False",
"lastProbeTime": null,
"lastTransitionTime": "2020-09-23T07:15:52Z",
"reason": "ContainersNotReady",
"message": "containers with unready status: [cayenne]"
}, {
"type": "ContainersReady",
"status": "False",
"lastProbeTime": null,
"lastTransitionTime": "2020-09-23T07:15:52Z",
"reason": "ContainersNotReady",
"message": "containers with unready status: [cayenne]"
}, {
"type": "PodScheduled",
"status": "True",
"lastProbeTime": null,
"lastTransitionTime": "2020-09-23T07:13:52Z"
}
],
"containerStatuses": [{
"name": "cayenne",
"state": {
"waiting": {
"reason": "ContainerCreating"
}
},
"ready": false,
}
],
"qosClass": "Burstable"
}
}
}
分析
老 pod 此时状态是 Pending
Ready 状态变为 false, 不参与服务请求了
容器状态变为 waiting, 原因是 ContainerCreating (这里貌似不太对,应该是容器销毁吧)
0x8 MODIFIED 老pod
和0x7
一样,没看出什么区别
0x8 DELETED 老pod
{
"type": "DELETED",
"object": {
"kind": "Pod",
"apiVersion": "v1",
"metadata": {
"name": "cayenne-6cdf75c8b7-rtbgg",
},
"status": {
"phase": "Pending",
"conditions": [{
"type": "Initialized",
"status": "True",
"lastProbeTime": null,
"lastTransitionTime": "2020-09-23T07:13:52Z"
}, {
"type": "Ready",
"status": "False",
"lastProbeTime": null,
"lastTransitionTime": "2020-09-23T07:15:52Z",
"reason": "ContainersNotReady",
"message": "containers with unready status: [cayenne]"
}, {
"type": "ContainersReady",
"status": "False",
"lastProbeTime": null,
"lastTransitionTime": "2020-09-23T07:15:52Z",
"reason": "ContainersNotReady",
"message": "containers with unready status: [cayenne]"
}, {
"type": "PodScheduled",
"status": "True",
"lastProbeTime": null,
"lastTransitionTime": "2020-09-23T07:13:52Z"
}
],
"containerStatuses": [{
"name": "cayenne",
"state": {
"waiting": {
"reason": "ContainerCreating"
}
},
"lastState": {},
"ready": false
}
],
"qosClass": "Burstable"
}
}
}
分析
此时准备删除pod,删除之前pod 的状态是 Pending
``
## 参考
[监听所有命名空间](https://kubernetes.cn/docs/reference/generated/kubernetes-api/v1.19/#list-all-namespaces-pod-v1-core)
[监听一个命名空间](https://kubernetes.cn/docs/reference/generated/kubernetes-api/v1.19/#list-pod-v1-core)
[知乎](https://zhuanlan.zhihu.com/p/59660536)
网友评论