kubernetes: Graceful node shutdown doesn't wait for volume teardown

What happened?

We have observed that after a node is preempted, pods using persistent volumes take 6+ minutes to restart. This happens when a volume was not able to be cleanly unmounted (indicated by updating Node.Status.VolumesInUse), which causes the Attach/Detach controller (in kube-controller-manager) to wait for 6 minutes before issuing a force detach. In addition to a long pod restart period, an unclean mount could also cause data corruption.

From the logs we’ve observed the following non-ideal behaviors, which I think reduces to the same issue that we only check for containers’ being terminated and not their volumes.

  1. The CSI driver running on the node gets terminated before the workload’s volumes get unmounted. This causes unmount to fail. In our case, the CSI driver is part of the `system-node-critical" class, but since we don’t wait for volumes to be unmounted, we move on to shutting down the critical pods.

  2. The OS gets shutdown before volume teardown is completed, even if there is still time left in the shutdownGracePeriod. In our case, the shutdownGracePeriod is set to 30 seconds, however we see that the node got shutdown in 10 seconds, even while unmount has not succeeded.

kubelet.log

Jan 17 21:48:18 ip-10-0-1-208 kubelet[950]: I0117 21:48:18.460409     950 setters.go:548] "Node became not ready" node="ip-10-0-1-208" condition={Type:Ready Status:False LastHeartbeatTime:2023-01-17 21:48:18.46035065 +0000 UTC m=+1235.025577092 LastTransitionTime:2023-01-17 21:48:18.46035065 +0000 UTC m=+1235.025577092 Reason:KubeletNotReady Message:node is shutting down}
...
Jan 17 21:48:27 ip-10-0-1-208 kubelet[950]: E0117 21:48:27.126414     950 nestedpendingoperations.go:348] Operation for "{volumeName:kubernetes.io/csi/ebs.csi.aws.com^vol-00477959568e1db98 podName: nodeName:}" failed. No retries permitted until 2023-01-17 21:48:35.126400526 +0000 UTC m=+1251.691626967 (durationBeforeRetry 8s). Error: UnmountDevice failed for volume "pvc-513e457c-86c1-4564-911c-6c0670cb839c" (UniqueName: "kubernetes.io/csi/ebs.csi.aws.com^vol-00477959568e1db98") on node "ip-10-0-1-208" : kubernetes.io/csi: attacher.UnmountDevice failed to create newCsiDriverClient: driver name ebs.csi.aws.com not found in the list of registered CSI drivers
...
Jan 17 21:48:29 ip-10-0-1-208 kubelet[950]: E0117 21:48:29.006761     950 kubelet.go:2475] "Container runtime network not ready" networkReady="NetworkReady=false reason:NetworkPluginNotReady message:Network plugin returns error: cni plugin not initialized"
Connection to 10.0.1.208 closed by remote host.
Connection to 10.0.1.208 closed.

Some code tracing:

  1. The logic for releasing the inhibitor lock waits for pods to be terminated within the grace period.

https://github.com/kubernetes/kubernetes/blob/37e73b419e455db34f5fe3e8d815418680ab23df/pkg/kubelet/nodeshutdown/nodeshutdown_manager_linux.go#L326

  1. It depends on IsTerminated to indicate that the pod has been killed: https://github.com/kubernetes/kubernetes/blob/37e73b419e455db34f5fe3e8d815418680ab23df/pkg/kubelet/pod_workers.go#L676

  2. IsTerminated depends on terminatedAt, which is set when all containers have stopped: https://github.com/kubernetes/kubernetes/blob/37e73b419e455db34f5fe3e8d815418680ab23df/pkg/kubelet/pod_workers.go#L273

  3. However, volume termination for a Pod only begins when all containers have stopped: https://github.com/kubernetes/kubernetes/blob/37e73b419e455db34f5fe3e8d815418680ab23df/pkg/kubelet/volumemanager/populator/desired_state_of_world_populator.go#L249

What did you expect to happen?

Node graceful shutdown to also wait for volumes to be torn down.

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

Deploy a StatefulSet pod using an attachable PVC, and then trigger node shutdown.

Anything else we need to know?

No response

Kubernetes version

Tested on 1.25, 1.26

Cloud provider

Observed on GCP, AWS

OS version

# On Linux:
$ cat /etc/os-release
# paste output here
$ uname -a
# paste output here

# On Windows:
C:\> wmic os get Caption, Version, BuildNumber, OSArchitecture
# paste output here

Install tools

Container runtime (CRI) and version (if applicable)

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

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 4
  • Comments: 20 (19 by maintainers)

Most upvoted comments

CSI drivers depend on kubelet (volume manager) to tell it when it’s safe to do an unmount. The key signal for volume manager is that the Pod’s containers have terminated. We do not want to unmount a volume while a container could still be writing to it.

Here’s a diagram I made that illustrates the race. The main challenge is that node shutdown manager and volume manager are operating off of the same signals (pod terminated) concurrently.

Blank diagram (1)

There’s two possible solutions I can see:

  1. preStop or sig-term handler in the CSI driver to detect and wait for all mounts to be unmounted. We would have to recommend that every CSI driver implement this.
  2. node shutdown manager also considers volume cleanup as a signal