|
| 1 | +--- |
| 2 | +reviewers: |
| 3 | +- lmktfy |
| 4 | +title: Configuring swap memory on Kubernetes nodes |
| 5 | +content_type: tutorial |
| 6 | +weight: 35 |
| 7 | +min-kubernetes-server-version: "1.33" |
| 8 | +--- |
| 9 | + |
| 10 | +<!-- overview --> |
| 11 | + |
| 12 | +This page provides an example of how to provision and configure swap memory on a Kubernetes node using kubeadm. |
| 13 | + |
| 14 | +<!-- lessoncontent --> |
| 15 | + |
| 16 | +## {{% heading "objectives" %}} |
| 17 | + |
| 18 | +* Provision swap memory on a Kubernetes node using kubeadm. |
| 19 | +* Learn to configure both encrypted and unencrypted swap. |
| 20 | +* Learn to enable swap on boot. |
| 21 | + |
| 22 | +## {{% heading "prerequisites" %}} |
| 23 | + |
| 24 | + |
| 25 | +{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}} |
| 26 | + |
| 27 | +You need at least one worker node in your cluster which needs to run a Linux operating system. |
| 28 | +It is required for this demo that the kubeadm tool be installed, following the steps outlined in the |
| 29 | +[kubeadm installation guide](/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm). |
| 30 | + |
| 31 | +On each worker node where you will configure swap use, you need: |
| 32 | +* `fallocate` |
| 33 | +* `mkswap` |
| 34 | +* `swapon` |
| 35 | + |
| 36 | +* For encrypted swap space (recommended), you also need: |
| 37 | +* `cryptsetup` |
| 38 | + |
| 39 | +<!-- lessoncontent --> |
| 40 | + |
| 41 | + |
| 42 | +## Install a swap-enabled cluster with kubeadm |
| 43 | + |
| 44 | +### Create a swap file and turn swap on |
| 45 | + |
| 46 | +If swap is not enabled, there's a need to provision swap on the node. |
| 47 | +The following sections demonstrate creating 4GiB of swap, both in the encrypted and unencrypted case. |
| 48 | + |
| 49 | +{{< tabs name="Create a swap file and turn swap on" >}} |
| 50 | + |
| 51 | +{{% tab name="Setting up encrypted swap" %}} |
| 52 | +An encrypted swap file can be set up as follows. |
| 53 | +Bear in mind that this example uses the `cryptsetup` binary (which is available |
| 54 | +on most Linux distributions). |
| 55 | + |
| 56 | +```bash |
| 57 | +# Allocate storage and restrict access |
| 58 | +fallocate --length 4GiB /swapfile |
| 59 | +chmod 600 /swapfile |
| 60 | + |
| 61 | +# Create an encrypted device backed by the allocated storage |
| 62 | +cryptsetup --type plain --cipher aes-xts-plain64 --key-size 256 -d /dev/urandom open /swapfile cryptswap |
| 63 | + |
| 64 | +# Format the swap space |
| 65 | +mkswap /dev/mapper/cryptswap |
| 66 | + |
| 67 | +# Activate the swap space for paging |
| 68 | +swapon /dev/mapper/cryptswap |
| 69 | +``` |
| 70 | + |
| 71 | +{{% /tab %}} |
| 72 | + |
| 73 | +{{% tab name="Setting up unencrypted swap" %}} |
| 74 | +An unencrypted swap file can be set up as follows. |
| 75 | + |
| 76 | +```bash |
| 77 | +# Allocate storage and restrict access |
| 78 | +fallocate --length 4GiB /swapfile |
| 79 | +chmod 600 /swapfile |
| 80 | + |
| 81 | +# Format the swap space |
| 82 | +mkswap /swapfile |
| 83 | + |
| 84 | +# Activate the swap space for paging |
| 85 | +swapon /swapfile |
| 86 | +``` |
| 87 | + |
| 88 | +{{% /tab %}} |
| 89 | + |
| 90 | +{{< /tabs >}} |
| 91 | + |
| 92 | +#### Verify that swap is enabled |
| 93 | + |
| 94 | +Swap can be verified to be enabled with both `swapon -s` command or the `free` command. |
| 95 | + |
| 96 | +Using `swapon -s`: |
| 97 | +``` |
| 98 | +Filename Type Size Used Priority |
| 99 | +/dev/dm-0 partition 4194300 0 -2 |
| 100 | +``` |
| 101 | + |
| 102 | +Using `free -h`: |
| 103 | +``` |
| 104 | + total used free shared buff/cache available |
| 105 | +Mem: 3.8Gi 1.3Gi 249Mi 25Mi 2.5Gi 2.5Gi |
| 106 | +Swap: 4.0Gi 0B 4.0Gi |
| 107 | +``` |
| 108 | + |
| 109 | +#### Enable swap on boot |
| 110 | + |
| 111 | +After setting up swap, to start the swap file at boot time, |
| 112 | +you typically either set up a systemd unit to activate (encrypted) swap, or you |
| 113 | +add a line similar to `/swapfile swap swap defaults 0 0` into `/etc/fstab`. |
| 114 | + |
| 115 | +Using systemd for swap activation allows the system to delay kubelet start until swap is available, |
| 116 | +if that is something you want to ensure. |
| 117 | +In a similar way, using systemd allows your server to leave swap active until kubelet |
| 118 | +(and, typically, your container runtime) have shut down. |
| 119 | + |
| 120 | +### Set up kubelet configuration |
| 121 | + |
| 122 | +After enabling swap on the node, kubelet needs to be configured in the following way: |
| 123 | + |
| 124 | +```yaml |
| 125 | + # this fragment goes into the kubelet's configuration file |
| 126 | + failSwapOn: false |
| 127 | + memorySwap: |
| 128 | + swapBehavior: LimitedSwap |
| 129 | +``` |
| 130 | +
|
| 131 | +In order for these configurations to take effect, kubelet needs to be restarted. |
0 commit comments