Overview
Swap devices are used to extend the available memory on a system by allowing the kernel to move inactive pages from RAM to disk. Swap might be useful to free up memory when running memory-intensive workloads, but it can also lead to performance degradation if used excessively. On other hand, moving inactive pages to swap can allow Linux to use more memory for buffers and caches, which can improve performance for workloads that benefit from caching. Zswap is a compressed cache for swap pages that can help reduce the performance impact of swapping by keeping frequently accessed pages in memory. Swap and zswap can be used together, but they can also be configured independently. Swap and zswap are disabled by default in Talos, but can be enabled through the configuration.Swap Devices
Swap devices can be configured in the Talos machine configuration similar to how User Volumes are configured. As swap devices contain memory pages, it is recommended to enable disk encryption for swap devices to prevent sensitive data from being written to disk in plaintext. It is also recommended to use a separate disk for swap devices to avoid performance degradation on the system disk and other workloads. For example, to configure a swap device on a NVMe disk of size 4GiB, using static key for encryption, the following configuration patch can be used:s-swap1
, encrypt it using the provided key, and enable it as a swap device.
Current swap status can be checked using talosctl get swap
command:
SwapVolumeConfig
document will remove the swap device from the system, but the partition will remain on the disk.
To wipe the disk data, and make it allocatable again, use the following command (replace nvme0n2p2
with the actual device name):
Zswap
Zswap is a compressed cache for swap pages that can help reduce the performance impact of swapping by keeping frequently accessed pages in memory. Zswap can be enabled in the Talos machine configuration:talosctl get zswapstatus
command:
ZswapConfig
document will disable zswap on the system.
Kubernetes and Swap
Kubernetes by default does not allow swap to be used by containers, as it can lead to performance issues and unpredictable behavior. Refer to Tuning Swap for Kubernetes for more information. At the very minimum, enable swap usage in thekubelet
configuration with the following machine configuration patch:
talosctl cgroups
command:
SwapMax
is set to 0 B
, it means that swap is not enabled for this cgroup (container/pod).
Current swap and zswap usage can be seen in the SwapCurrent
and ZswapCurrent
columns, respectively.