Managing graceful node shutdown

Graceful node shutdown enables the kubelet to delay forcible eviction of pods during a node shutdown. When you configure a graceful node shutdown, you can define a time period for pods to complete running workloads before shutting down. This grace period minimizes interruption to critical workloads during unexpected node shutdown events. Using priority classes, you can also specify the order of pod shutdown.

About graceful node shutdown

During a graceful node shutdown, the kubelet sends a termination signal to pods running on the node and postpones the node shutdown until all the pods evicted. If a node unexpectedly shuts down, the graceful node shutdown feature minimizes interruption to workloads running on these pods.

During a graceful node shutdown, the kubelet stops pods in two phases:

  • Regular pod termination

  • Critical pod termination

You can define shutdown grace periods for regular and critical pods by configuring the following specifications in the KubeletConfig custom resource:

  • shutdownGracePeriod: Specifies the total duration for pod termination for regular and critical pods.

  • shutdownGracePeriodCriticalPods: Specifies the duration for critical pod termination. This value must be less than the shutdownGracePeriod value.

For example, if the shutdownGracePeriod value is 30s, and the shutdownGracePeriodCriticalPods value is 10s, the kubelet delays the node shutdown by 30 seconds. During the shutdown, the first 20 (30-10) seconds are reserved for gracefully shutting down regular pods, and the last 10 seconds are reserved for gracefully shutting down critical pods.

To define a critical pod, assign a pod priority value greater than or equal to 2000000000. To define a regular pod, assign a pod priority value of less than 2000000000.

For more information about how to define a priority value for pods, see the Additional resources section.

Configuring graceful node shutdown

To configure graceful node shutdown, create a KubeletConfig custom resource (CR) to specify a shutdown grace period for pods on a set of nodes. The graceful node shutdown feature minimizes interruption to workloads that run on these pods.

If you do not configure graceful node shutdown, the default grace period is 0 and the pod is forcefully evicted from the node.

Prerequisites

  • You have access to the cluster with the cluster-admin role.

  • You have defined priority classes for pods that require critical or regular classification.

Procedure

  1. Define shutdown grace periods in the KubeletConfig CR by saving the following YAML in the kubelet-gns.yaml file:

    1. apiVersion: machineconfiguration.openshift.io/v1
    2. kind: KubeletConfig
    3. metadata:
    4. name: graceful-shutdown
    5. namespace: openshift-machine-config-operator
    6. spec:
    7. machineConfigPoolSelector:
    8. matchLabels:
    9. pools.operator.machineconfiguration.openshift.io/worker: "" (1)
    10. kubeletConfig:
    11. shutdownGracePeriod: "3m" (2)
    12. shutdownGracePeriodCriticalPods: "2m" (3)
    1This example applies shutdown grace periods to nodes with the worker role.
    2Define a time period for regular pods to shut down.
    3Define a time period for critical pods to shut down.
  2. Create the KubeletConfig CR by running the following command:

    1. $ oc create -f kubelet-gns.yaml

    Example output

    1. kubeletconfig.machineconfiguration.openshift.io/graceful-shutdown created

Verification

  1. View the kubelet logs for a node to verify the grace period configuration by using the command line or by viewing the kubelet.conf file.

    Ensure that the log messages for shutdownGracePeriodRequested and shutdownGracePeriodCriticalPods match the values set in the KubeletConfig CR.

    1. To view the logs by using the command line, run the following command, replacing <node_name> with the name of the node:

      1. $ oc adm node-logs <node_name> -u kubelet

      Example output

      1. Sep 12 22:13:46
      2. ci-ln-qv5pvzk-72292-xvkd9-worker-a-dmbr4
      3. hyperkube[22317]: I0912 22:13:46.687472
      4. 22317 nodeshutdown_manager_linux.go:134]
      5. "Creating node shutdown manager"
      6. shutdownGracePeriodRequested="3m0s" (1)
      7. shutdownGracePeriodCriticalPods="2m0s"
      8. shutdownGracePeriodByPodPriority=[
      9. {Priority:0
      10. ShutdownGracePeriodSeconds:1200}
      11. {Priority:2000000000
      12. ShutdownGracePeriodSeconds:600}]
      13. ...
      1Ensure that the log messages for shutdownGracePeriodRequested and shutdownGracePeriodCriticalPods match the values set in the KubeletConfig CR.
    2. To view the logs in the kubelet.conf file on a node, run the following commands to enter a debug session on the node:

      1. $ oc debug node/<node_name>
      1. $ chroot /host
      1. $ cat /etc/kubernetes/kubelet.conf

      Example output

      1. ...
      2. memorySwap”: {},
      3. containerLogMaxSize”: 50Mi”,
      4. logging”: {
      5. flushFrequency”: 0,
      6. verbosity”: 0,
      7. options”: {
      8. json”: {
      9. infoBufferSize”: 0
      10. }
      11. }
      12. },
      13. shutdownGracePeriod”: 10m0s”, (1)
      14. shutdownGracePeriodCriticalPods”: 3m0s
      15. }
      1Ensure that the log messages for shutdownGracePeriodRequested and shutdownGracePeriodCriticalPods match the values set in the KubeletConfig CR.
  2. During a graceful node shutdown, you can verify that a pod was gracefully shut down by running the following command, replacing <pod_name> with the name of the pod:

    1. $ oc describe pod <pod_name>

    Example output

    1. Reason: Terminated
    2. Message: Pod was terminated in response to imminent node shutdown.

Additional resources