<ul>
<li><p>Can Kubernetes pods share significant amount of memory?</p>
</li>
<li><p>Does copy-on-write style forking exist for pods?</p>
</li>
</ul>
<p>The purpose is to make pods spawn faster and use less memory.</p>
<p>Our scenario is that we have a dedicated game server to host in kubernetes. The problem is that one instance of the dedicated game server would take up a few GB of memory upfront (e.g. 3 GBs).</p>
<p>Also, we have a few such docker images of game servers, each for game A, game B... Let's call a pod that's running game A's image for game A <code>pod A</code>.</p>
<p>Let's say we now have 3 x <code>pod A</code>, 5 x <code>pod B</code>. Now players rushing into game B, so I need let's say another 4 * <code>pod B</code> urgently.</p>
<p>I can surely spawn 4 more <code>pod B</code>. Kubernetes supports this perfectly. However there are 2 problems:</p>
<ul>
<li>The booting of my game server is very slow (30s - 1min). Players don't want to wait.</li>
<li>More importantly for us, the cost of having this many pods that take up so much memory is very high. Because pods do not share memory as far as I know. Where as if it were plain old EC2 machine or bare metal, processes can share memory because they can fork and then copy-on-write.</li>
</ul>
<p>Copy-on-write style forking and memory sharing seems to solve both problems.</p>

<p>One of Kubernetes' assumptions is that <em>pods</em> are scheduled on different Nodes, which contradicts the idea of sharing common resources (does not apply for storage where <a href="https://kubernetes.io/docs/concepts/storage/volumes/" rel="nofollow noreferrer">there are many options and documentation available</a>).  The situation is different when it comes to sharing resources between <em>containers in one pod</em>, but for your issue this doesn't apply.</p>
<p>However, it seems that there is some possibility to share memory - not well documented and I guess very uncommon in Kubernetes. Check my answers with more details below:</p>
<blockquote>
<p>Can Kubernetes pods share significant amount of memory?</p>
</blockquote>
<p>What I found is that pods can share a common <a href="https://en.wikipedia.org/wiki/Inter-process_communication" rel="nofollow noreferrer">IPC</a> with the host (node).
You can check <a href="https://kubernetes.io/docs/concepts/policy/pod-security-policy/" rel="nofollow noreferrer">Pod Security Policies</a>, especially <a href="https://kubernetes.io/docs/concepts/policy/pod-security-policy/#host-namespaces" rel="nofollow noreferrer">field <code>hostIPC</code></a>:</p>
<blockquote>
<p><strong>HostIPC</strong>  - Controls whether the pod containers can share the host IPC namespace.</p>
</blockquote>
<p>Some usage examples and possible security issues <a href="https://github.com/BishopFox/badPods/tree/main/manifests/hostipc#bad-pod-7-hostipc" rel="nofollow noreferrer">can be found here</a>:</p>
<ul>
<li><a href="https://github.com/BishopFox/badPods/tree/main/manifests/hostipc#inspect-devshm---look-for-any-files-in-this-shared-memory-location" rel="nofollow noreferrer">Shared <code>/dev/sh</code> directory</a></li>
<li><a href="https://github.com/BishopFox/badPods/tree/main/manifests/hostipc#look-for-any-use-of-inter-process-communication-on-the-host" rel="nofollow noreferrer">Use existing IPC facilities</a></li>
</ul>
<p>Keep in mind that this solution is not common in Kubernetes. Pods with elevated <a href="https://kubernetes.io/blog/2021/04/06/podsecuritypolicy-deprecation-past-present-and-future/#why-is-podsecuritypolicy-going-away" rel="nofollow noreferrer">privileges are granted broader permissions than needed</a>:</p>
<blockquote>
<p>The way PSPs are applied to Pods has proven confusing to nearly everyone that has attempted to use them. It is easy to accidentally grant broader permissions than intended, and difficult to inspect which PSP(s) apply in a given situation.</p>
</blockquote>
<p>That's why the Kubernetes team marked Pod Security Policies as deprecated from Kubernetes <code>v1.21</code> - <a href="https://kubernetes.io/blog/2021/04/06/podsecuritypolicy-deprecation-past-present-and-future/" rel="nofollow noreferrer">check more information in this article</a>.</p>
<p>Also, if you are using multiple nodes in your cluster you <a href="https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/" rel="nofollow noreferrer">should use nodeSelector</a> to make sure that pods will be assigned to same node that means they will be able to share one (host's) IPC.</p>
<blockquote>
<p>Does copy-on-write style forking exist for pods?</p>
</blockquote>
<p>I did a re-search and I didn't find any information about this possibility, so I think it is not possible.</p>
<hr />
<p>I think the main issue is that your game architecture is not &quot;very suitable&quot; for Kubernetes. Check these articles and websites about dedicated game servers in Kubernetes- maybe you will them useful:</p>
<ul>
<li><a href="https://agones.dev/site/" rel="nofollow noreferrer">Agones</a></li>
<li><a href="https://www.gamedeveloper.com/programming/scaling-dedicated-game-servers-with-kubernetes-part-3-scaling-up-nodes" rel="nofollow noreferrer">Scaling Dedicated Game Servers with Kubernetes: Part 3 – Scaling Up Nodes</a></li>
<li><a href="https://cloud.google.com/files/DedicatedGameServerSolution.pdf" rel="nofollow noreferrer">Google Cloud - Dedicated Game Server Solution</a></li>
<li><a href="https://cloud.google.com/game-servers" rel="nofollow noreferrer">Google Cloud - Game Servers</a></li>
</ul>
