Master swap memory configuration in Kubernetes clusters with our comprehensive tutorial covering K8s v1.32+ features, performance optimization, and production best practices.

🚀 Introduction to Kubernetes NodeSwap

Kubernetes NodeSwap represents a paradigm shift in how container orchestration platforms handle memory management. For years, Kubernetes administrators had to disable swap memory entirely, following the platform’s strict “no-swap” policy. However, with the introduction and maturation of NodeSwap support, clusters can now leverage Linux swap memory to improve resource utilization and prevent out-of-memory (OOM) kills.

Why NodeSwap Matters

Traditional Kubernetes deployments often faced memory pressure issues, leading to pod evictions and service disruptions. NodeSwap addresses this by allowing the kernel to swap less-frequently accessed memory pages to disk, effectively extending available memory and improving cluster stability.

🛡️

Enhanced Stability

Reduces OOM kills and improves workload reliability by providing memory overflow protection.

📈

Better Resource Utilization

Maximizes node memory efficiency by utilizing secondary storage for virtual memory expansion.

💰

Cost Optimization

Allows running more workloads on existing hardware, reducing the need for memory upgrades.

📈 Evolution of Swap Support in Kubernetes

K8s v1.22

Alpha Release

Initial alpha support for swap memory introduced with basic NodeSwap feature gate and limited configuration options.

K8s v1.28

Beta Graduation

Swap support graduated to beta with enhanced stability, improved memory accounting, and better integration with cgroup v2.

K8s v1.32

Fresh Improvements

Latest enhancements include advanced tuning options, improved monitoring capabilities, and better IO pressure handling.

85%
OOM Reduction
40%
Memory Efficiency
25%
Cost Savings

⚙️ Prerequisites & Requirements

Critical Requirements

Before enabling NodeSwap, ensure your cluster meets these mandatory requirements. Missing any of these will prevent swap functionality from working correctly.

Linux Operating System: Only Linux nodes support swap memory
Cgroup v2: Kubernetes requires cgroup v2 for swap support
Kubernetes v1.22+: Minimum version for alpha support (v1.28+ recommended)
Swap Partition/File: Pre-configured swap space on the node
NodeSwap Feature Gate: Must be enabled in kubelet configuration

Checking System Compatibility

# Check if cgroup v2 is enabled
ls /sys/fs/cgroup/cgroup.controllers

# Verify kernel version (4.0+ recommended)
uname -r

# Check current swap status
swapon --show

# Verify Kubernetes version
kubectl version --short

🔧 Step-by-Step Configuration Guide

Step-1: Configure Swap on the Node

First, set up swap space on your Kubernetes nodes. You can either use a swap partition or a swap file.

# Option 1: Create a swap file (4GB example)
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

# Option 2: Use existing partition
sudo swapon /dev/sdXY

# Make swap permanent
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

# Verify swap is active
free -h

Step-2: Configure Kubelet for Swap Support

Modify the kubelet configuration to enable NodeSwap and configure swap behavior.

# /var/lib/kubelet/config.yaml
apiVersion: kubelet.config.k8s.io/v1beta1
kind: KubeletConfiguration
featureGates:
  NodeSwap: true
failSwapOn: false
memorySwap:
  swapBehavior: LimitedSwap  # or UnlimitedSwap
systemReserved:
  memory: 1Gi
kubeReserved:
  memory: 1Gi

Swap Behavior Options

  • LimitedSwap: Pods can use swap up to their memory limit (recommended for production)
  • UnlimitedSwap: Pods can use unlimited swap (useful for development/testing)

Step-3: Configure Pod-Level Swap Settings

apiVersion: v1
kind: Pod
metadata:
  name: swap-enabled-pod
spec:
  containers:
  - name: app-container
    image: nginx:latest
    resources:
      requests:
        memory: "512Mi"
      limits:
        memory: "1Gi"
  # Swap usage will be calculated based on memory limits
  nodeSelector:
    node.kubernetes.io/swap-available: "true"

Step-4: Restart Kubelet Service

# Restart kubelet to apply new configuration
sudo systemctl restart kubelet

# Verify kubelet is running with swap enabled
sudo systemctl status kubelet

# Check kubelet logs for swap-related messages
sudo journalctl -u kubelet -f | grep -i swap

⚡ Performance Tuning and Optimization

Proper tuning is essential for optimal swap performance in Kubernetes environments. The following configuration parameters significantly impact system behavior.

Kernel Parameters Tuning

# Configure swappiness (0-100, lower = less aggressive swapping)
echo 'vm.swappiness=10' >> /etc/sysctl.conf

# Set swap tendency for anonymous vs file-backed pages
echo 'vm.swap_ratio=60' >> /etc/sysctl.conf

# Configure memory pressure handling
echo 'vm.vfs_cache_pressure=50' >> /etc/sysctl.conf

# Apply settings immediately
sudo sysctl -p

Performance Impact Considerations

Enabling swap can introduce I/O latency. It’s recommended to prioritize system-critical daemons with higher I/O scheduling classes and monitor swap usage patterns closely.

I/O Priority Configuration

# Set I/O priority for critical system processes
sudo ionice -c 1 -n 0 -p $(pidof kubelet)
sudo ionice -c 1 -n 1 -p $(pidof containerd)

# Monitor I/O pressure
iostat -x 1 5

# Check swap I/O patterns
iotop -o -d 1

Advanced Memory Management

# Advanced kubelet memory management
apiVersion: kubelet.config.k8s.io/v1beta1
kind: KubeletConfiguration
featureGates:
  NodeSwap: true
  MemoryQoS: true
memorySwap:
  swapBehavior: LimitedSwap
memoryThrottlingFactor: 0.8
systemReserved:
  memory: "2Gi"
  cpu: "500m"
kubeReserved:
  memory: "1Gi"
  cpu: "250m"
evictionHard:
  memory.available: "200Mi"
  nodefs.available: "10%"

📊 Monitoring & Troubleshooting

Essential Monitoring Metrics

📈

Memory Metrics

Monitor swap usage, memory pressure, and pod memory consumption patterns.

💾

I/O Performance

Track disk I/O latency, throughput, and swap-related I/O operations.

Application Performance

Observe application response times and resource utilization changes.

Monitoring Commands

# Check swap usage in real-time
watch -n 1 'free -h && swapon -s'

# Monitor per-process swap usage
for dir in /proc/*/; do
  echo "$(cat $dir/comm) $(cat $dir/status | grep VmSwap)"
done | grep -v "0 kB" | sort -k2 -n

# Kubernetes-specific monitoring
kubectl top nodes
kubectl describe nodes | grep -A 5 "Allocated resources"

# Check pod memory usage with swap
kubectl exec -it POD_NAME -- cat /proc/meminfo

Common Troubleshooting Scenarios

🔍 High Swap Usage: Check for memory leaks or insufficient resource limits
🔍 Performance Degradation: Adjust swappiness or consider SSD storage for swap
🔍 Pod Evictions: Review memory requests/limits and eviction policies
🔍 Kubelet Crashes: Verify cgroup v2 compatibility and feature gate configuration

✅ Production Best Practices

Security Considerations

Security Best Practices

Swap files can contain sensitive data from memory. Ensure proper encryption and access controls are in place.

# Encrypt swap partition
cryptsetup luksFormat /dev/sdXY
cryptsetup luksOpen /dev/sdXY swap
mkswap /dev/mapper/swap

# Set proper permissions for swap files
sudo chmod 600 /swapfile
sudo chown root:root /swapfile

# Configure SELinux/AppArmor policies if applicable

Leave a Reply

Quote of the week

“One machine can do the work of fifty ordinary men.  No machine can do the work of one extraordinary man”

~ Elbert Hubbard