Sobes.tech
Middle

Why is swap disabled on Kubernetes nodes?

sobes.tech AI

Answer from AI

In Kubernetes, disabling swap on worker/master nodes is recommended, and in some cases, a mandatory requirement.

Main reasons:

  • Unpredictable behavior when running out of memory: When a node with swap enabled runs out of physical memory, the operating system starts actively using the swap file/partition. This leads to significant slowdown of processes, including Kubernetes system components (kubelet, kube-proxy) and containers themselves. kubelet may incorrectly assess resource availability for pods, which can lead to their "killing" (OOMKilled) due to false signals of memory shortage or, conversely, to process hangs instead of restarts.

  • Complexity in resource management: Kubernetes efficiently manages resources (CPU, memory) at the pod and container level using cgroups. Using swap adds an extra layer of abstraction and complicates cgroups and kubelet's work in tracking and limiting memory consumption by pods. kubelet finds it harder to determine the actual memory usage of pods if some data is swapped out.

  • Reduced performance: Swap resides on disk (HDD/SSD), access to which is significantly slower than RAM. Active use of swap greatly reduces the performance of applications and cluster components. This can cause timeouts, errors, and instability in the entire cluster.

  • Need for deterministic behavior: For stable cluster operation and predictable application behavior, it is important that the system responds to memory shortages deterministically. Disabling swap ensures that when RAM is exhausted, OOMKiller mechanisms will trigger, terminating the least priority processes (pods) to free resources for more critical ones. With swap enabled, behavior becomes less predictable.

  • Community and documentation recommendations: Official Kubernetes documentation and community best practices strongly recommend disabling swap on nodes where Kubernetes workloads are planned to run. This is considered standard practice for maintaining cluster stability and performance.

# Example command to disable swap
sudo swapoff -a

# Example of removing swap entry from /etc/fstab to keep the change after reboot
# Find the line containing 'swap' or the path to your swap device/file
# Comment out or delete the line
sudo vi /etc/fstab