Reputation: 193
I need to add swap memory in kubernates pod. so that if any pods exceeds the available RAM then it can use the swap memory from hard disk. is that possible in kubernates?
Upvotes: 7
Views: 7679
Reputation: 3553
There is now beta support in v1.28
https://kubernetes.io/blog/2023/08/24/swap-linux-beta/
Upvotes: 0
Reputation: 31
Enabling swap on a node is already covered by previous answers.
If you need to control which pods and containers can be swapped and to what extent, you can install the nri-memory-qos plugin to your cluster. This allows controlling swap on container level with annotations.
helm repo add nri-plugins https://containers.github.io/nri-plugins
helm install nri-memory-qos nri-plugins/nri-memory-qos --set nri.patchRuntimeConfig=true --namespace kube-system
Upvotes: 1
Reputation: 31
I would like the pods to have swap inside the container, not on the host.. if the application uses too much memory the kernal restarts the service.. if the container restarts then it lessens uptime right.. swap can assist in that
Upvotes: 0
Reputation: 4890
As of Kubernetes 1.22, swap memory is supported (as an alpha feature).
swapon
or /etc/fstab
);fail-on-swap
),NodeSwap
feature gate, andMemorySwap.SwapBehavior=UnlimitedSwap
to let kubernetes workloads use swap memory.Note, there is currently no support for setting swap limits individually per workload (although this is planned for the beta). Either no containers are permitted to use any swap, or all containers can use unlimited swap memory.
(If workloads are not permitted to use swap then, depending on the Linux kernel cgroups version, they could still get swapped anyway. Prior to cgroups v2, processes were not able to enforce separate limits for swap and physical memory, but only for the combined total.)
See the docs, and the kubernetes enhancement proposal (KEP) cited therein, for more details.
Upvotes: 5
Reputation: 13888
The official Kubernetes prerequisites says that:
Swap disabled. You MUST disable swap in order for the kubelet to work properly.
You are supposed to use cpu/memory limits for deployments. Kubelet is not designed to handle swap situations. If memory swapping is allowed to occur on a host system, it can lead to performance and stability issues within Kubernetes.
However there is a way to disable swap while installing kubeadm
:
/etc/systemd/system/kubelet.service.d/20-allow-swap.conf
with the following content:[Service] Environment="KUBELET_EXTRA_ARGS=--fail-swap-on=false"
sudo systemctl daemon-reload
kubeadm
with flag --ignore-preflight-errors=Swap
:kubeadm init --ignore-preflight-errors=Swap
Bear in mind however that it is not a recommended way.
Upvotes: 4