Kubernetes Cluster using ‘swap memory’ on Host Node

Before Kubernetes release 1.22, swap space must be disabled on host node in order to provide full disk resource for cluster and pods running on it.

With new release, swap space can remain on host node. For new deployments, this seems pretty straightforward but what if you have kubernetes cluster already deployed, running without swap memory and you want to restart using swap on host node. This may not be a common use case but certain architects may design compute servers to use swap memory as mandatory requirement.

In this blog, I will solve this same scenario on a kubernetes cluster running on v.1.23.3 without swap memory on host node.

ububuntu@ubmaster:~$ kubectl get nodes 
NAME       STATUS   ROLES                  AGE     VERSION
ubmaster   Ready    control-plane,master   3d22h   v1.23.3
ubworker   Ready    <none>                 3d21h   v1.23.3

Step 1

Firstly, make sure you don’t have any running application pod on worker nodes since we will be resetting whole cluster. I uninstall one release of nginx before proceeding to drain worker node.

ubuntu@ubmaster:~$ helm uninstall my-release

ubuntu@ubmaster:~$ kubectl drain ubworker --ignore-daemonsets
node/ubworker cordoned

Step 2

Proceed to reset of kubernetes cluster using kubeadm utility. Repeat this step for both Master and all Worker nodes.

root@ubmaster:/home/ubuntu# kubeadm reset

Repeat this step for both Master and all Worker nodes.

root@ubworker:/home/ubuntu# kubeadm reset

Step 3

Add “–fail-swap-on=false” as extra argument in kubeadm conf file so this parameter is taken when we run kubeadm init and join on nodes.

root@ubmaster:/home/ubuntu# echo 'Environment="KUBELET_EXTRA_ARGS=--fail-swap-on=false"' >> /etc/systemd/system/kubelet.service.d/10-kubeadm.conf

Additionally, we enabled swap memory by uncommenting swap disk in /etc/fstab file.

Step 4

Restart daemon and kubelet services.

root@ubmaster:/home/ubuntu# systemctl daemon-reload

root@ubmaster:/home/ubuntu# systemctl restart kubelet

Step 5

Initialize kubernetes cluster specifying same pod-network cidr as existing.

ubuntu@ubmaster:~$ sudo kubeadm init --ignore-preflight-errors Swap --pod-network-cidr=xx.xx.xx.xx/24

Step 6

Join worker node with kubeadm utility as we did in our last setup.

ubuntu@ubmaster:~$sudo kubeadm join <control-plane-host>:<control-plane-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s