Runtime Kernel Changes Last Until Reboot Unless Written to Their Configuration Directory

In progress Linux Kubernetes

modprobe, sysctl -w and swapoff change the running kernel only. At the next boot, systemd rebuilds that state from configuration files, so a node prepared by typing commands works perfectly until its first reboot. Every runtime change needs a persistent twin in the file systemd reads at boot.

Three Commands and Their Persistent Twins

Change now Persist in Applied at boot by
modprobe br_netfilter /etc/modules-load.d/*.conf, one module per line systemd-modules-load.service
sysctl -w net.ipv4.ip_forward=1 /etc/sysctl.d/*.conf, key = value systemd-sysctl.service
swapoff -a remove or comment the swap line in /etc/fstab systemd-fstab-generator, which turns fstab swap lines into swap units

Kubernetes nodes need all three: the overlay and br_netfilter modules, IP forwarding and bridge filtering through sysctl, and, by default, no swap. The swap pair, as my bootstrap script had it:

swapoff -a                                        # disable now
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab    # disable on reboot

Write the File First, Then Apply It

Writing the file and then loading it from the file keeps the live state and the boot state identical:

sysctl --system                              # reload every /etc/sysctl.d file now
systemctl restart systemd-modules-load       # load the modules listed in modules-load.d

When swap comes from somewhere other than fstab (a zram device, a hand-written .swap unit), swapon --show lists it, and masking its unit (for example systemctl mask dev-sda3.swap) stops systemd from activating it.

Configuration management does both halves in one task: Ansible's ansible.posix.sysctl module writes the value to a file and reloads it. Checking for the line before appending keeps a re-run from duplicating it (see Idempotent Scripts Check the Current State Before Changing It).

Swap has a third source besides fstab and hand-written units: systemd can activate a GPT swap partition it discovers itself. systemctl mask swap.target stops every swap unit from being pulled in at boot, whatever created it.

Test the Reboot, Not the Shell

A check run in the same session only proves the runtime state. Reboot once after preparing a node, then verify:

lsmod | grep br_netfilter
sysctl net.ipv4.ip_forward net.bridge.bridge-nf-call-iptables
swapon --show                                # prints nothing when swap is off

Over plain SSH as a normal user, sysctl and swapon may be "not found": they live in /usr/sbin. The same answers come from /proc: cat /proc/sys/net/ipv4/ip_forward /proc/swaps.

References