kubernetes: Graceful node drain results in never-cleaned-up pods in various Error/Completed phases

What happened?

During a graceful node drain, I am seeing various pods in error / completed pod phases that are never cleaned up (deleted). They stay until I manually delete them with either a controller like https://github.com/kubernetes-sigs/descheduler or kubectl delete.

postgres-1 here is just a pod with no statefulset / deployment. It was created manually by cloudnative-pg.

cloudnative-pg-74c458f8cf-257g9   0/1     Completed   0          10h
cloudnative-pg-74c458f8cf-zdb2k   1/1     Running     0          7m46s
postgres-1                        0/1     Error       0          10h
postgres-2                        1/1     Running     0          13h
postgres-3                        1/1     Running     0          14h
redis-node-0                      3/3     Running     0          10h
redis-node-1                      3/3     Running     0          13h
redis-node-2                      3/3     Running     0          14h
Name:             postgres-1
Namespace:        databases
Priority:         0
Service Account:  postgres
Node:             w2/10.0.0.22
Start Time:       Wed, 29 Nov 2023 18:45:01 -0600
Labels:           cnpg.io/cluster=postgres
                  cnpg.io/instanceName=postgres-1
                  cnpg.io/instanceRole=replica
                  cnpg.io/podRole=instance
                  role=replica
Annotations:      cnpg.io/nodeSerial: 1
                  cnpg.io/operatorVersion: 1.21.1
                  cnpg.io/podEnvHash: 8566455577
                  cnpg.io/podSpec:
                    {"volumes":[{"name":"pgdata","persistentVolumeClaim":{"claimName":"postgres-1"}},{"name":"scratch-data","emptyDir":{}},{"name":"shm","empt...
                  k8s.v1.cni.cncf.io/network-status:
                    [{
                        "name": "cilium",
                        "interface": "eth0",
                        "ips": [
                            "10.244.5.94"
                        ],
                        "mac": "0e:ea:e8:5c:2c:5f",
                        "default": true,
                        "dns": {},
                        "gateway": [
                            "10.244.5.192"
                        ]
                    }]
                  kyverno.io/exclude: true
Status:           Failed
Reason:           Terminated
Message:          Pod was terminated in response to imminent node shutdown.
SeccompProfile:   RuntimeDefault
IP:
IPs:              <none>
Controlled By:    Cluster/postgres
Init Containers:
  bootstrap-controller:
    Container ID:    containerd://d19c819b1d25fbed28030d500672fdf9cacee69e61dfd5c1fa8cec6e744976d9
    Image:           ghcr.io/cloudnative-pg/cloudnative-pg:1.21.1
    Image ID:        ghcr.io/cloudnative-pg/cloudnative-pg@sha256:9f707d91de1c92975ab1a098b52e804d8f5b90c7934a0b1c40d00e773522a123
    Port:            <none>
    Host Port:       <none>
    SeccompProfile:  RuntimeDefault
    Command:
      /manager
      bootstrap
      /controller/manager
      --log-level=info
    State:          Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Wed, 29 Nov 2023 18:45:04 -0600
      Finished:     Wed, 29 Nov 2023 18:45:04 -0600
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /controller from scratch-data (rw)
      /dev/shm from shm (rw)
      /etc/app-secret from app-secret (rw)
      /etc/superuser-secret from superuser-secret (rw)
      /run from scratch-data (rw)
      /var/lib/postgresql/data from pgdata (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-q7928 (ro)
Containers:
  postgres:
    Container ID:    containerd://ba3e10ebb1f57e8ed4da10800ec954ea16924105f483ff3b099874cab157c722
    Image:           ghcr.io/cloudnative-pg/postgresql:16.1-4
    Image ID:        sha256:189647f611df17d25679d38dc453c474299bd65cebc6f14635054d471b13e9cd
    Ports:           5432/TCP, 9187/TCP, 8000/TCP
    Host Ports:      0/TCP, 0/TCP, 0/TCP
    SeccompProfile:  RuntimeDefault
    Command:
      /controller/manager
      instance
      run
      --log-level=info
    State:          Terminated
      Reason:       Error
      Exit Code:    137
      Started:      Wed, 29 Nov 2023 18:45:05 -0600
      Finished:     Thu, 30 Nov 2023 05:37:00 -0600
    Ready:          False
    Restart Count:  0
    Liveness:       http-get http://:8000/healthz delay=0s timeout=5s period=10s #success=1 #failure=3
    Readiness:      http-get http://:8000/readyz delay=0s timeout=5s period=10s #success=1 #failure=3
    Startup:        http-get http://:8000/healthz delay=0s timeout=5s period=10s #success=1 #failure=360
    Environment:
      PGDATA:        /var/lib/postgresql/data/pgdata
      POD_NAME:      postgres-1
      NAMESPACE:     databases
      CLUSTER_NAME:  postgres
      PGPORT:        5432
      PGHOST:        /controller/run
    Mounts:
      /controller from scratch-data (rw)
      /dev/shm from shm (rw)
      /etc/app-secret from app-secret (rw)
      /etc/superuser-secret from superuser-secret (rw)
      /run from scratch-data (rw)
      /var/lib/postgresql/data from pgdata (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-q7928 (ro)
Conditions:
  Type               Status
  DisruptionTarget   True
  Initialized        True
  Ready              False
  ContainersReady    False
  PodScheduled       True
Volumes:
  pgdata:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  postgres-1
    ReadOnly:   false
  scratch-data:
    Type:       EmptyDir (a temporary directory that shares a pod's lifetime)
    Medium:
    SizeLimit:  <unset>
  shm:
    Type:       EmptyDir (a temporary directory that shares a pod's lifetime)
    Medium:     Memory
    SizeLimit:  <unset>
  superuser-secret:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  cloudnative-pg-secret
    Optional:    false
  app-secret:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  postgres-app
    Optional:    false
  kube-api-access-q7928:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason   Age   From     Message
  ----    ------   ----  ----     -------
  Normal  Killing  10m   kubelet  Stopping container postgres

Various logging pods:

loki-write-0                                                1/1     Running     0             14h     10.244.4.251   w0     <none>           <none>
loki-write-1                                                0/1     Completed   0             11h     <none>         w2     <none>           <none>
loki-write-2                                                1/1     Running     0             14h     10.244.3.29    w1     <none>           <none>
Name:             loki-write-1
Namespace:        monitoring
Priority:         0
Service Account:  loki
Node:             w2/10.0.0.22
Start Time:       Wed, 29 Nov 2023 18:42:31 -0600
Labels:           app.kubernetes.io/component=write
                  app.kubernetes.io/instance=loki
                  app.kubernetes.io/name=loki
                  app.kubernetes.io/part-of=memberlist
                  apps.kubernetes.io/pod-index=1
                  controller-revision-hash=loki-write-7ff4f96bf9
                  statefulset.kubernetes.io/pod-name=loki-write-1
Annotations:      checksum/config: 67193d214dd0992612d9cefaa5d8f0ca0fc148cbc6943ca6cd8f8aa75d2b9057
                  k8s.v1.cni.cncf.io/network-status:
                    [{
                        "name": "cilium",
                        "interface": "eth0",
                        "ips": [
                            "10.244.5.245"
                        ],
                        "mac": "ea:47:d8:cc:b7:e1",
                        "default": true,
                        "dns": {},
                        "gateway": [
                            "10.244.5.192"
                        ]
                    }]
                  kubectl.kubernetes.io/restartedAt: 2023-11-21T20:33:18-06:00
                  kyverno.io/exclude: true
Status:           Succeeded
IP:
IPs:              <none>
Controlled By:    StatefulSet/loki-write
Containers:
  loki:
    Container ID:  containerd://8705a1d6d2c36fa4c98c1d8f281f2146ae2e410462df7a9cfe92d373421c7cb1
    Image:         docker.io/grafana/loki:2.9.2
    Image ID:      sha256:314b95a20590e79e06a9d244a95ee7320f8762141b446f40f8d74e1e887f5494
    Ports:         3100/TCP, 9095/TCP, 7946/TCP
    Host Ports:    0/TCP, 0/TCP, 0/TCP
    Args:
      -config.file=/etc/loki/config/config.yaml
      -target=write
    State:          Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Wed, 29 Nov 2023 18:42:35 -0600
      Finished:     Thu, 30 Nov 2023 05:36:40 -0600
    Ready:          False
    Restart Count:  0
    Readiness:      http-get http://:http-metrics/ready delay=30s timeout=1s period=10s #success=1 #failure=3
    Environment:    <none>
    Mounts:
      /etc/loki/config from config (rw)
      /etc/loki/runtime-config from runtime-config (rw)
      /var/loki from data (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-4hgg6 (ro)
Conditions:
  Type               Status
  DisruptionTarget   True
  Initialized        True
  Ready              False
  ContainersReady    False
  PodScheduled       True
Volumes:
  data:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  data-loki-write-1
    ReadOnly:   false
  config:
    Type:      ConfigMap (a volume populated by a ConfigMap)
    Name:      loki
    Optional:  false
  runtime-config:
    Type:      ConfigMap (a volume populated by a ConfigMap)
    Name:      loki-runtime
    Optional:  false
  kube-api-access-4hgg6:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason   Age   From     Message
  ----    ------   ----  ----     -------
  Normal  Killing  11m   kubelet  Stopping container loki

What did you expect to happen?

The pods were cleaned up

How can we reproduce it (as minimally and precisely as possible)?

Most likely the same as https://github.com/kubernetes/kubernetes/issues/118310#issuecomment-1698823460

Gracefully drain a node with various Statefulsets / Deployments

Anything else we need to know?

https://github.com/kubernetes/kubernetes/issues/118310 Talos Linux v1.5.5 containerd v1.7.7 K8s v1.28.4 with feature gate GracefulNodeShutdown=true

Kubernetes version

K8s v1.28.4

Cloud provider

N/A

OS version

Talos Linux v1.5.5 running Linux Kernel 6.1.61

Install tools

N/A

Container runtime (CRI) and version (if applicable)

containerd v1.7.7

Related plugins (CNI, CSI, …) and versions (if applicable)

N/A

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 5
  • Comments: 22 (10 by maintainers)

Most upvoted comments

@SergeyKanzhelev Graceful node termination. That’s what I understand talosctl reboot does under the hood.

Re #121389: Looks like the fix needed for StatefulSets to me too, but I read through changelogs and commit log for the latest stable release (1.28.4) and don’t see this PR being included in 1.28.4’s branch. I’m happy to update to 1.28.4 and later stable versions regardless given it’s a homelab, but I don’t think it will incorporate this change you want me to try.

The fix for StatefulSets is on the release-1.28 branch (see here), and will be released in 1.28.5.

I also experience this.

Talos: 1.5.4 Kubernetes: 1.28.3

Also, as pointed out #122122 (comment) the accumulated pods would eventually be collected by PodGC, when their number exceeds 12500 (default value for --terminated-pod-gc-threshold)

I am on 1.29.1 deployed with Talos Linux and I don’t think that is true, I had pods from a deployment in a errored state for 15 hours and it was never cleaned up. This was after a successful drain / graceful shutdown of all my nodes one at a time.

The PodGC cleanup of terminated pods does not depend on time, but on the number of pods in terminal state: https://github.com/kubernetes/kubernetes/blob/50bf3a2060ea798600af1a4c125e0b62d08e9680/pkg/controller/podgc/gc_controller.go#L125-L127. The number of terminated pods needs to exceed --terminated-pod-gc-threshold. see here: https://kubernetes.io/docs/reference/command-line-tools-reference/kube-controller-manager/

Is this ever going to be fixed @mimowo?

Since the pods are recreated I don’t think this is directly related to https://github.com/kubernetes/kubernetes/issues/118310.

It is expected behavior that kubelet initiates the termination, but does not delete the pods: https://kubernetes.io/docs/concepts/architecture/nodes/#graceful-node-shutdown (at least in the Beta version of the feature). The accumulated pods would eventually be collected by PodGC, when their number exceeds 12500 (default value for --terminated-pod-gc-threshold).

Still, I think this could be better handled by the Deployments controller, which could delete the terminated pods, similarly as the StatefulSet controller does here, or the DaemonSet controller - which identifies terminated pods to be deleted here. /sig apps cc @alculquicondor

Follow up from https://github.com/kubernetes/kubernetes/issues/118310#issuecomment-1833019187 in here, here’s my nodes and their versions (also with GracefulNodeShutdown feature flag enabled):

❯ kubectl get nodes -o=wide
NAME        STATUS   ROLES           AGE    VERSION   OS-IMAGE         KERNEL-VERSION   CONTAINER-RUNTIME
ange        Ready    control-plane   5d7h   v1.28.2   Talos (v1.5.4)   6.1.58-talos     containerd://1.6.23
charlotte   Ready    control-plane   24d    v1.28.2   Talos (v1.5.4)   6.1.58-talos     containerd://1.6.23
chise       Ready    control-plane   15d    v1.28.2   Talos (v1.5.4)   6.1.58-talos     containerd://1.6.23

Reproduce: To preface, this is my onprem baremetal homelab cluster running Talos which uses vanilla upstream Kubernetes images, same as Buroa. My 3 nodes are all Lenovo M720q Tiny PCs.

I will use Kyverno for this example. My Helm values for Kyverno can be found here, if interested: (https://github.com/JJGadgets/Biohazard/blob/57eeb5e5e1ee17c828c2cd477af145bd7caee200/kube/deploy/core/kyverno/app/hr.yaml)

Here’s a kubectl get deploy to show the replica counts:

❯ kubectl get deploy -n kyverno
NAME                            READY   UP-TO-DATE   AVAILABLE   AGE
kyverno-admission-controller    3/3     3            3           2d12h
kyverno-background-controller   2/2     2            2           2d12h
kyverno-cleanup-controller      2/2     2            2           2d12h
kyverno-reports-controller      2/2     2            2           2d12h
  1. Grab a quick sample of kubectl get pods before rebooting:
❯ kubectl get pods -n kyverno
NAME                                                       READY   STATUS      RESTARTS       AGE
kyverno-admission-controller-64f9fcc65-js868               1/1     Running     0              2d10h
kyverno-admission-controller-64f9fcc65-tc8q7               1/1     Running     0              2d10h
kyverno-admission-controller-64f9fcc65-tg9sz               1/1     Running     0              2d10h
kyverno-background-controller-5c756fbbbc-n7z77             1/1     Running     0              2d10h
kyverno-background-controller-5c756fbbbc-ppnm6             1/1     Running     0              2d10h
kyverno-cleanup-admission-reports-28357430-vtfbt           0/1     Completed   0              2m31s
kyverno-cleanup-cluster-admission-reports-28357430-fn2rn   0/1     Completed   0              2m31s
kyverno-cleanup-controller-5c9b5b8fb5-kqcdb                1/1     Running     2 (15h ago)    2d10h
kyverno-cleanup-controller-5c9b5b8fb5-ms97n                1/1     Running     17 (26h ago)   2d10h
kyverno-reports-controller-76674fddf9-dzb5w                1/1     Running     0              2d10h
kyverno-reports-controller-76674fddf9-fpg52                1/1     Running     2 (15h ago)    2d10h
  1. Drain and reboot nodes, for Talos users talosctl reboot handles it all.

  2. Once all nodes come back up, wait for a few more minutes or so, and grab another sample of kubectl get pods:

❯ kubectl get pods -n kyverno
NAME                                                       READY   STATUS              RESTARTS      AGE
kyverno-admission-controller-64f9fcc65-js868               0/1     Completed           0             2d10h
kyverno-admission-controller-64f9fcc65-tc8q7               0/1     Completed           0             2d10h
kyverno-admission-controller-64f9fcc65-tg9sz               1/1     Running             1 (11m ago)   2d10h
kyverno-background-controller-5c756fbbbc-n7z77             0/1     Completed           0             2d10h
kyverno-background-controller-5c756fbbbc-ncx9r             1/1     Running             0             8m57s
kyverno-background-controller-5c756fbbbc-nr7zt             1/1     Running             0             8m56s
kyverno-background-controller-5c756fbbbc-ppnm6             0/1     Completed           0             2d10h
kyverno-cleanup-admission-reports-28357430-vtfbt           0/1     Completed           0             16m
kyverno-cleanup-admission-reports-28357440-2c99x           0/1     ContainerCreating   0             5m7s
kyverno-cleanup-cluster-admission-reports-28357430-fn2rn   0/1     Completed           0             16m
kyverno-cleanup-cluster-admission-reports-28357440-xfhf4   0/1     ContainerCreating   0             5m17s
kyverno-cleanup-controller-5c9b5b8fb5-5ndcb                1/1     Running             0             8m57s
kyverno-cleanup-controller-5c9b5b8fb5-bh5sm                0/1     ContainerCreating   0             8m56s
kyverno-cleanup-controller-5c9b5b8fb5-kqcdb                0/1     Completed           2             2d10h
kyverno-cleanup-controller-5c9b5b8fb5-ms97n                0/1     Completed           17            2d10h
kyverno-reports-controller-76674fddf9-7q52m                0/1     ContainerCreating   0             8m56s
kyverno-reports-controller-76674fddf9-dzb5w                0/1     Completed           0             2d10h
kyverno-reports-controller-76674fddf9-fpg52                0/1     Completed           2             2d10h
kyverno-reports-controller-76674fddf9-wzdjh                0/1     ContainerCreating   0             8m56s
  1. Wait even longer (this time was an hour for me as I had left my desk), get another sample:
❯ kubectl get pods -n kyverno
NAME                                                       READY   STATUS      RESTARTS      AGE
kyverno-admission-controller-64f9fcc65-js868               1/1     Running     1 (79m ago)   2d12h
kyverno-admission-controller-64f9fcc65-tc8q7               1/1     Running     1 (79m ago)   2d12h
kyverno-admission-controller-64f9fcc65-tg9sz               1/1     Running     1 (79m ago)   2d12h
kyverno-background-controller-5c756fbbbc-n7z77             0/1     Completed   0             2d12h
kyverno-background-controller-5c756fbbbc-ncx9r             1/1     Running     0             77m
kyverno-background-controller-5c756fbbbc-nr7zt             1/1     Running     0             77m
kyverno-background-controller-5c756fbbbc-ppnm6             0/1     Completed   0             2d12h
kyverno-cleanup-admission-reports-28357510-42lqn           0/1     Completed   0             4m12s
kyverno-cleanup-cluster-admission-reports-28357510-5fqvl   0/1     Completed   0             4m12s
kyverno-cleanup-controller-5c9b5b8fb5-6fdhs                1/1     Running     0             61m
kyverno-cleanup-controller-5c9b5b8fb5-bh5sm                1/1     Running     0             77m
kyverno-cleanup-controller-5c9b5b8fb5-kqcdb                0/1     Completed   2             2d12h
kyverno-cleanup-controller-5c9b5b8fb5-ms97n                0/1     Completed   17            2d12h
kyverno-reports-controller-76674fddf9-7q52m                1/1     Running     0             77m
kyverno-reports-controller-76674fddf9-dzb5w                0/1     Completed   0             2d12h
kyverno-reports-controller-76674fddf9-fpg52                0/1     Completed   2             2d12h
kyverno-reports-controller-76674fddf9-wzdjh                1/1     Running     0             77m

As you can see, there are 2 leftover pods for background-controller, cleanup-controller and reports-controller deployments. The new pods are 77m old, so the cluster has been given quite a while to reconcile the deployments, and the Completed pods have yet to be cleared.

Additionally, I’m not sure if this is intended behaviour, but on this round of reboot, the admission-controller pods managed to restart from their Completed state (they’re 2 days old with +1 restart count post-reboot), while the background-controller, cleanup-controller and reports-controller Completed pods couldn’t restart and new pods had to be spawned in by the deployment controller (given the 77m age).

Aside from the reproduction: running a kubectl get pod -A --field-selector=status.phase==Succeeded showed some StatefulSet pods stuck in Completed too, I didn’t grab a sample of these before the reboot but I visually saw that all these pods were definitely running prior to the reboot on my k9s pods list:

grocy                     grocy-0                                                    0/2     Completed   0          2d11h
monitoring                prometheus-kps-1                                           0/2     Completed   0          2d11h
monitoring                prometheus-kps-2                                           0/2     Completed   0          2d12h
syncthing-jjgadgets       syncthing-jjgadgets-0                                      0/1     Completed   0          2d12h