How to Disable Swap in Linux for Kubernetes

Part of the official requirements for Kubernetes is to disable swap space completely before attempting to launch Kubernetes. In some recent patch notes, Kubernetes has begun implementing support for swap space in some scenarios, but it is not fully supported. The official reason for not supporting swap space is because it is hard to predict how a pod might utilize memory when swap is enabled.

If you are looking to install Kubernetes and get started with using it, you will need to ensure that swap space is turned off in your Linux system, both for the master node and all worker nodes in the Kubernetes cluster. In this tutorial, we will show you how to permanently disable swap space on a Linux system to make it ready for Kubernetes.

In this tutorial you will learn:

  • How to temporarily disable swap space
  • How to permanently disable swap space
How to Disable Swap in Linux for Kubernetes
How to Disable Swap in Linux for Kubernetes
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Kubernetes
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to Disable Swap Space in Linux




Running the following command will disable swap temporarily. This will keep swap disabled until a system reboot.

$ sudo swapoff -a

To disable swap permanently, we need to comment out the corresponding line in the /etc/fstab file.

$ sudo nano /etc/fstab

Comment out the swap line by adding a # pound sign in front of it.

The swap line inside of our system's /etc/fstab file
The swap line inside of our system’s /etc/fstab file

Even more simply, we can use the sed command to add a # character to any line that contains the word “swap” inside of our system’s /etc/fstab file:

$ sudo sed -i '/ swap / s/^/#/' /etc/fstab

Closing Thoughts




In this tutorial, we saw how to disable swap space on a Linux system in order to accommodate Kubernetes. Since Kubernetes requires that swap space be disabled, we must be sure to fulfill this crucial step before attempting to get our cluster up and running.



Comments and Discussions
Linux Forum