kubernetes: GracefulNodeShutdown does not trigger on nodes shutdown with `shutdown -h now`

What happened?

Shutting down a node with shutdown -h now does not trigger the graceful node drain.

Despite being indicated in the original proposal https://github.com/kubernetes/enhancements/tree/master/keps/sig-node/2000-graceful-node-shutdown

In the context of this KEP, shutdown is referred to as shutdown of the underlying machine. On most linux distros shutdown can be initiated via a variety of methods for example:

  1. shutdown -h now
  2. shutdown -h +30 #schedule a delayed shutdown in 30mins

and explained in the blog: https://kubernetes.io/blog/2021/04/21/graceful-node-shutdown-beta/

How does it work? On Linux, your system can shut down in many different situations. For example:

  • A user or script running shutdown -h now or systemctl poweroff or systemctl reboot.

the command seems to not behave the same way as sending a PrepareForShutdown signal on the dbus.

The node does not drain itself from its pods and pods will stay Running until timeout and it becomes stuck at Terminating

On the other hand, shutdown -h now acts similarly as a forced shutdown and as I understand, a forced shutdown on a node doesn’t allow a grace period as it is forced; so one can argue that there is nothing wrong with the behavior. However, the need to clarify the documentation on what counts as a shutdown that can trigger GracefulNodeShutdown may be helpful.

What did you expect to happen?

Expecting a similar behavior as if executing # systemctl poweroff or #dbus-send --system /org/freedesktop/login1 org.freedesktop.login1.Manager.PrepareForShutdown boolean:true

where the graceful node drain occurs and terminates the pods.

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

  1. Start a cluster with kubeadm init with this config. I use 2 worker nodes by default but can be reproduced with 1.
apiVersion: kubeadm.k8s.io/v1beta3
kind: ClusterConfiguration
networking:
  podSubnet: "10.244.0.0/16"  #flannel
controlPlaneEndpoint: "XXX.XXX.XXX.XXX"
---
apiVersion: kubelet.config.k8s.io/v1beta1
cgroupDriver: systemd
featureGates:
  GracefulNodeShutdown: true
shutdownGracePeriod: 90s
shutdownGracePeriodCriticalPods: 30s
kind: KubeletConfiguration                   
  1. Make a deployment or a statefulset; I used this statefulset config in my environment: https://github.com/kubernetes/kubernetes/issues/118310#issuecomment-1698823460

  2. Monitor the pods on a terminal logged SSHed the control plane:

kubectl get pods --watch

  1. On a terminal logged in a worker node, execute shutdown with the following command:

shutdown -h now

Anything else we need to know?

No response

Kubernetes version

tested on kubernetes versions 1.26, 1.27, 1.28, 1.29

Cloud provider

N/A

OS version

# On Linux:
$ cat /etc/os-release
NAME="CentOS Stream"
VERSION="8"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Stream 8"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://centos.org/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux 8"
REDHAT_SUPPORT_PRODUCT_VERSION="CentOS Stream"
$ uname -a
Linux localhost.localdomain 4.18.0-544.el8.x86_64 #1 SMP Thu Feb 22 21:29:23 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

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 3 years ago
  • Comments: 28 (13 by maintainers)

Most upvoted comments

Thanks.

  • Created PR in the enhancements for a potential fix in the proposal document cross-referencing this issue. https://github.com/kubernetes/enhancements/pull/4640
  • Was able to confirm that PrepareForShutdown is being sent in systemd v252 (CentOS 9 Stream default systemd version); current unconfirmed release range (systemd v240- systemd v251)

Sure. Feel free to make an addendum to the KEP with a small editorial that mentions potential issues with some versions of systemd and how the System V compatibility mode tooling has been implemented.

I see. I haven’t really done much in the kubernetes/enhancements repo. Do I just make a PR in the kubernetes/enhnacements repository with my suggested corrections for https://github.com/kubernetes/enhancements/tree/master/keps/sig-node/2000-graceful-node-shutdown ?

@tuibeovince, no worries. The process here is like any other change. See for example:

Make sure to add the original authors, etc.

@harche @dustymabe After attempting to upgrade my systemd (because they only accept issues on the latest 2 releases of systemd); I was able to infer that:

  • my reported environment using environment as of reporting (systemd 239) does not yield the expected signal for GracefulNodeShutdown. I assume any older version of systemd will have this same behavior.
  • upgrading to systemd 255, the PrepareForShutdown signal is properly emitted. (Thus there is no need to create an issue on the systemd side?)
  • versions 240 - 254 are untested. The fix was implemented sometime in these releases.

Additional supporting information, I also checked the StatefulSet and Deployment pod behaviors on a cluster in a Centos 9 Stream environment with systemd 255 using kubernetes 1.29.0 and the GracefulNodeShutdown works as expected, with shutdown -h now working as if I executed systemctl poweroff.