Skip to content

Commit 6bdb4d8

Browse files
authored
Disable swap for kubelet (#67)
1 parent 5c59237 commit 6bdb4d8

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

aks-flex-node-sudoers

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ aks-flex-node ALL=(root) NOPASSWD:SETENV: /usr/bin/test *, /bin/test *
9999
aks-flex-node ALL=(root) NOPASSWD:SETENV: /sbin/sysctl --system
100100
aks-flex-node ALL=(root) NOPASSWD:SETENV: /sbin/modprobe overlay
101101
aks-flex-node ALL=(root) NOPASSWD:SETENV: /sbin/modprobe br_netfilter
102+
aks-flex-node ALL=(root) NOPASSWD:SETENV: /sbin/swapoff -a
102103

103104
# Configuration file management and reading
104105
aks-flex-node ALL=(root) NOPASSWD:SETENV: /bin/tee /etc/sysctl.d/k8s.conf

pkg/components/system_configuration/system_configuration_installer.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,19 @@ func (i *Installer) Validate(ctx context.Context) error {
5454

5555
// configureSysctl creates and applies sysctl configuration for Kubernetes
5656
func (i *Installer) configureSysctl() error {
57+
// Disable swap immediately - kubelet sees no active swap devices
58+
// so it can start successfully. This is a critical step for kubelet compatibility.
59+
if err := i.disableSwap(); err != nil {
60+
return fmt.Errorf("failed to disable swap: %w", err)
61+
}
62+
5763
sysctlConfig := `# Kubernetes sysctl settings
5864
net.bridge.bridge-nf-call-iptables = 1
5965
net.bridge.bridge-nf-call-ip6tables = 1
6066
net.ipv4.ip_forward = 1
6167
vm.overcommit_memory = 1
6268
kernel.panic = 10
63-
kernel.panic_on_oops = 1
64-
# Disable swap permanently - required for kubelet
65-
vm.swappiness = 0`
69+
kernel.panic_on_oops = 1`
6670

6771
// Create sysctl directory if it doesn't exist
6872
if err := utils.RunSystemCommand("mkdir", "-p", sysctlDir); err != nil {
@@ -111,6 +115,20 @@ func (i *Installer) configureResolvConf() error {
111115
return nil
112116
}
113117

118+
// disableSwap disables swap immediately for kubelet compatibility
119+
func (i *Installer) disableSwap() error {
120+
i.logger.Info("Disabling swap for kubelet compatibility")
121+
122+
// Disable all swap devices immediately
123+
if err := utils.RunSystemCommand("swapoff", "-a"); err != nil {
124+
i.logger.WithError(err).Warning("Failed to disable swap - may not be enabled")
125+
} else {
126+
i.logger.Info("Swap disabled successfully")
127+
}
128+
129+
return nil
130+
}
131+
114132
// GetName returns the step name
115133
func (i *Installer) GetName() string {
116134
return "SystemConfigured"

pkg/utils/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020

2121
// sudoCommandLists holds the command lists for sudo determination
2222
var (
23-
alwaysNeedsSudo = []string{"apt", "apt-get", "dpkg", "systemctl", "mount", "umount", "modprobe", "sysctl", "azcmagent", "usermod", "kubectl"}
23+
alwaysNeedsSudo = []string{"apt", "apt-get", "dpkg", "systemctl", "mount", "umount", "modprobe", "sysctl", "azcmagent", "usermod", "kubectl", "swapoff"}
2424
conditionalSudo = []string{"mkdir", "cp", "chmod", "chown", "mv", "tar", "rm", "bash", "install", "ln", "cat"}
2525
systemPaths = []string{"/etc/", "/usr/", "/var/", "/opt/", "/boot/", "/sys/"}
2626
)

0 commit comments

Comments
 (0)