Swap file - how to check usage and expand it

how to check usage and expand it

SWAP FILE

When setting up Kubernetes nodes, it's typically not recommended to use swap files or partitions. This is because swap can disrupt Kubernetes' resource management and scheduling processes. However, if you really need to enable swap, you can change the kubelet settings by adding the --fail-swap-on=false flag. Keep in mind, though, that this is generally not advised for production environments.

image loading ...

Adding swap space

Adding swap space on Arch Linux can be done in two ways:

by creating a swap file or by setting up a swap partition.

Creating a Swap File

You can create a swap file of the desired size using the dd command. For example, to create a 4GB swap file, you can use:

linux - dd cli
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress
This command will create a file called /swapfile with a size of 4GB.

Set Permissions

For security, set the swap file's permissions so that only root can read and write to it:

sudo chmod 600 /swapfile

Format the file to a swap format using the mkswap command:

sudo mkswap /swapfile

Activate the Swap File Turn on the swap file with the swapon command:

sudo swapon /swapfile

Make the Swap File Permanent

To ensure the swap file is used on system boot, add it to your /etc/fstab file.

Add the following line at the end of the file:

bash /etc/fstab
/swapfile none swap sw 0 0

Check the Swap Status

You can verify that the swap is active by using:

swapon --show
 
free -m

Adjusting Swappiness

The "swappiness" parameter controls how often your system uses swap space. It's a value between 0 and 100. a lower value makes the kernel try to avoid swapping whenever possible, while a higher value allows more aggressive swapping.

To temporarily change the swappiness value:

sudo sysctl vm.swappiness=10

To make this change permanent, add the following line to your /etc/sysctl.conf file:

vm.swappiness=10

To check swappiness

sysctl vm.swappiness

Last

This approach of using a swap file is flexible and simple for most use cases. If you prefer a swap partition due to performance considerations or personal preference, the process involves using a partitioning tool like fdisk or gparted to create the swap partition, and then using mkswap and swapon to initialize and enable it, similarly to how the swap file is set up.

image loading ...