<p>I know k8s does not support swap by default.</p>

<p>But since I have a rather static deployment of pods in k8s (3 nodes, each with a solr + zookeeper pod), so I decided to set up 2G swap on all my worker nodes anyway and allow the use of swap by starting <strong>kubelet</strong> with --<code>fail-swap-on=false</code></p>

<p>Now I have got the cluster up &amp; running, and it seemed to be running okay.</p>

<p>However, I found that my java processes are using a lot of swap, I am worried that this might affect the performance.</p>

<ul>
<li>RSS: 317160 Kb</li>
<li>SWAP: 273232 Kb</li>
</ul>

<p>My question is that is there a way to limit the use of swap memory usage by containers? </p>

<p>I was thinking about setting <code>--memory-swap</code> <a href="https://docs.docker.com/config/containers/resource_constraints/#--memory-swap-details" rel="nofollow noreferrer">params of Docker</a></p>

<p>Currently, based on Docker inspect my container has no limit on swap usage ( <code>"MemorySwap": -1</code> )</p>

<pre><code>sudo docker inspect 482d70f73c7c | grep Memory
            "Memory": 671088640,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": -1,
            "MemorySwappiness": null,
</code></pre>

<p>But I just couldn't find this param exposed in k8s.</p>

<p><strong>My vm-related settings</strong></p>

<pre><code>vm.overcommit_kbytes = 0
vm.overcommit_memory = 1
vm.overcommit_ratio = 50
vm.swappiness = 20
vm.vfs_cache_pressure = 1000
</code></pre>

<p>p.s. Will the <strong>limit</strong> on pod memory also limit the swap usage?</p>

<p>Thank you all for reading the post!</p>

<h2>Notes</h2>

<p>This is how I got the RSS &amp; SWAP of my Java process</p>

<p><strong>Run</strong> <code>top</code></p>

<p><code>27137 8983      20   0 3624720 317160      0 S   3.0 32.0   4:06.74 java</code></p>

<p><strong>Run</strong> </p>

<pre><code>find /proc -maxdepth 2 -path "/proc/[0-9]*/status" -readable -exec awk -v FS=":" '{process[$1]=$2;sub(/^[ \t]+/,"",process[$1]);} END {if(process["VmSwap"] &amp;&amp; process["VmSwap"] != "0 kB") printf "%10s %-30s %20s\n",process["Pid"],process["Name"],process["VmSwap"]}' '{}' \; | awk '{print $(NF-1),$0}' | sort -h | cut -d " " -f2-
</code></pre>

<p><code>27137 java                                      273232 kB</code></p>

John the Traveler
<p>And now you know why the kubelet literally refuses to start by default if there swap active. This is not exposed anywhere in CRI and won’t be.</p>
