如何跟踪 pods 处于终止状态的时间?

How can I track the time pods are in Terminating status?

我正在开发一个 shell 脚本,它将强制删除卡在终止状态(由于任何原因)的 pod。

kubectl get pods --all-namespaces | grep Terminating | while read line; do 
pod_name=$(echo $line | awk '{print }' ) name_space=$(echo $line | awk 
'{print }' ); kubectl delete pods $pod_name -n $name_space --grace-period=0 --force; 
done

但为了安全起见,我只想删除自上次 10 分钟以来处于终止状态的 pod。

我如何获得这个时间,因为 kubectl get pods --all-namespaces | grep Terminating 给出的是 Pod 的 AGE 而不是当前状态的时间。

根据 documentation pod-lifecycle status.phase 只能有 5 个不同的值,其中 none 正在终止。这意味着终止状态未反映在该字段中,因此通过相位字段进行过滤将无济于事。

更新 1: 在输出@Vitalii 的建议后,由于节点关闭

,这些pods (statefulset) 处于终止状态
kubectl -n my_ns get pod mypod-0  -o json| jq '.status.containerStatuses[]'
{
  "containerID": "docker://bc4fdf965b19dffd5cb54dddb1931d9f522aad885d1576d03a41cd45d6d60c0d",
  "image": "mypod:1.11.0",
  "imageID": "docker://sha256:128b5cb1a95cfa001620d9e54b8cf9d0ef96b2a261cab4850accab4cfa0fe6cb",
  "lastState": {},
  "name": "mypod",
  "ready": true,
  "restartCount": 0,
  "started": true,
  "state": {
    "running": {
      "startedAt": "2021-04-21T16:42:23Z"
    }
  }
}

ContainerStateTerminated v1 core 中有一个 finishedAt 状态。

也许它可以帮助你 - 我只是没有看到你的输出 kubectl get pod ... -o json
当 pod 处于 Terminating 状态时 - 也许您可以检索有关容器何时在 pod 内终止的信息。

kubectl get pod $pod_name -n $name_space | jq '.status.containerStatuses[].lastState.terminated.finishedAt'
kubectl get pod $pod_name -n $name_space -o="jsonpath={.status.containerStatuses[].lastState.terminated.finishedAt}"

终止日期时间 保存在 Pod 的 属性 .metadata.deletionTimestamp:

kubectl get pods -o jsonpath="{.items[*].metadata.deletionTimestamp}"

或单个广告连播:

kubectl get pod pod_name -o jsonpath="{.metadata.deletionTimestamp}"

注意:Ready 状态下的 pods 不存在属性 .metadata.deletionTimestamp.metadata.deletionGracePeriodSeconds

示例:

k8s-m1:~$ kubectl get nodes
NAME     STATUS     ROLES    AGE    VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION   CONTAINER-RUNTIME
k8s-m1   Ready      master   256d   v1.18.8   10.156.0.49   <none>        Ubuntu 18.04.5 LTS   5.4.0-1040-gcp   docker://19.3.12
k8s-w1   NotReady   <none>   256d   v1.18.8   10.156.0.50   <none>        Ubuntu 18.04.5 LTS   5.4.0-1040-gcp   docker://19.3.12

$ kubectl get pods
NAME                                          READY   STATUS        RESTARTS   AGE     IP               NODE     NOMINATED NODE   READINESS GATES
aloha-pod                                     1/1     Running       0          11d     192.168.42.159   k8s-m1   <none>           <none>
hello-pod                                     1/1     Terminating   0          11d     192.168.228.80   k8s-w1   <none>           <none>


k8s-m1:~$ kubectl get pod aloha-pod -o jsonpath="deletionTimestamp={.metadata.deletionTimestamp},  deletionGracePeriodSeconds={.metadata.deletionGracePeriodSeconds}";echo
deletionTimestamp=,  deletionGracePeriodSeconds=


k8s-m1:~$ kubectl get pod hello-pod -o jsonpath="deletionTimestamp={.metadata.deletionTimestamp},  deletionGracePeriodSeconds={.metadata.deletionGracePeriodSeconds}";echo
deletionTimestamp=2021-04-29T09:39:03Z,  deletionGracePeriodSeconds=30