<p>In a multi-container Kubernetes pod, are the containers part of the same cgroup (along with pod) or a separate cgroup is created for each container.</p>

<h1>Cgroups</h1>
<p>Container in a pod share part of cgroup hierarchy but each container get's it's own cgroup. We can try this out and verify ourself.</p>
<ol>
<li>Start a multi container pod.</li>
</ol>
<pre><code># cat mc2.yaml
apiVersion: v1
kind: Pod
metadata:
  name: two-containers
spec:
  restartPolicy: Never
  containers:
  - name: container1
    image: ubuntu
    command: [ &quot;/bin/bash&quot;, &quot;-c&quot;, &quot;--&quot; ]
    args: [ &quot;while true; do sleep 30; done;&quot; ]

  - name: container2
    image: ubuntu
    command: [ &quot;/bin/bash&quot;, &quot;-c&quot;, &quot;--&quot; ]
    args: [ &quot;while true; do sleep 30; done;&quot; ]
</code></pre>
<pre><code># kubectl apply -f mc2.yaml
pod/two-containers created
</code></pre>
<ol start="2">
<li>Find the process cgroups on the host machine</li>
</ol>
<pre><code># ps -ax | grep while | grep -v grep
19653 ?        Ss     0:00 /bin/bash -c -- while true; do sleep 30; done;
19768 ?        Ss     0:00 /bin/bash -c -- while true; do sleep 30; done;
</code></pre>
<pre><code># cat /proc/19653/cgroup
12:hugetlb:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/ed89697807a981b82f6245ac3a13be232c1e13435d52bc3f53060d61babe1997
11:memory:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/ed89697807a981b82f6245ac3a13be232c1e13435d52bc3f53060d61babe1997
10:perf_event:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/ed89697807a981b82f6245ac3a13be232c1e13435d52bc3f53060d61babe1997
9:freezer:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/ed89697807a981b82f6245ac3a13be232c1e13435d52bc3f53060d61babe1997
8:cpuset:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/ed89697807a981b82f6245ac3a13be232c1e13435d52bc3f53060d61babe1997
7:net_cls,net_prio:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/ed89697807a981b82f6245ac3a13be232c1e13435d52bc3f53060d61babe1997
6:cpu,cpuacct:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/ed89697807a981b82f6245ac3a13be232c1e13435d52bc3f53060d61babe1997
5:blkio:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/ed89697807a981b82f6245ac3a13be232c1e13435d52bc3f53060d61babe1997
4:pids:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/ed89697807a981b82f6245ac3a13be232c1e13435d52bc3f53060d61babe1997
3:devices:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/ed89697807a981b82f6245ac3a13be232c1e13435d52bc3f53060d61babe1997
2:rdma:/
1:name=systemd:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/ed89697807a981b82f6245ac3a13be232c1e13435d52bc3f53060d61babe1997
0::/
</code></pre>
<pre><code># cat /proc/19768/cgroup
12:hugetlb:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/e10fa18a63cc26de27f3f79f46631cd814efa3ef7c2f5ace4b84cf5abce89765
11:memory:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/e10fa18a63cc26de27f3f79f46631cd814efa3ef7c2f5ace4b84cf5abce89765
10:perf_event:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/e10fa18a63cc26de27f3f79f46631cd814efa3ef7c2f5ace4b84cf5abce89765
9:freezer:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/e10fa18a63cc26de27f3f79f46631cd814efa3ef7c2f5ace4b84cf5abce89765
8:cpuset:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/e10fa18a63cc26de27f3f79f46631cd814efa3ef7c2f5ace4b84cf5abce89765
7:net_cls,net_prio:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/e10fa18a63cc26de27f3f79f46631cd814efa3ef7c2f5ace4b84cf5abce89765
6:cpu,cpuacct:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/e10fa18a63cc26de27f3f79f46631cd814efa3ef7c2f5ace4b84cf5abce89765
5:blkio:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/e10fa18a63cc26de27f3f79f46631cd814efa3ef7c2f5ace4b84cf5abce89765
4:pids:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/e10fa18a63cc26de27f3f79f46631cd814efa3ef7c2f5ace4b84cf5abce89765
3:devices:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/e10fa18a63cc26de27f3f79f46631cd814efa3ef7c2f5ace4b84cf5abce89765
2:rdma:/
1:name=systemd:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/e10fa18a63cc26de27f3f79f46631cd814efa3ef7c2f5ace4b84cf5abce89765
0::/
</code></pre>
<p>As you can see the containers in the pods share the cgroup hierarchy until <code>/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011</code> and then they get their own cgroup. (<a href="https://kubernetes.io/docs/tasks/configure-pod-container/quality-service-pod/#qos-classes" rel="noreferrer">These containers are under <code>besteffort</code> cgroup because we have not specified the resource requests</a>)</p>
<p><a href="https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#resource-requests-and-limits-of-pod-and-container" rel="noreferrer">Another clue that containers run in their own cgroup is that kubernetes allows you to set resource requests at the container level.</a></p>
<p>You can also find the cgroups of the container by logging into the container and viewing /proc/self/cgroup file. (This may not work in recent versions of kubernetes if cgroup namespace is enabled)</p>
<pre><code># kubectl exec -it two-containers -c container2 bash
# root@two-containers:# cat /proc/self/cgroup
12:hugetlb:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/ed89697807a981b82f6245ac3a13be232c1e13435d52bc3f53060d61babe1997
11:memory:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/ed89697807a981b82f6245ac3a13be232c1e13435d52bc3f53060d61babe1997
10:perf_event:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/ed89697807a981b82f6245ac3a13be232c1e13435d52bc3f53060d61babe1997
9:freezer:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/ed89697807a981b82f6245ac3a13be232c1e13435d52bc3f53060d61babe1997
8:cpuset:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/ed89697807a981b82f6245ac3a13be232c1e13435d52bc3f53060d61babe1997
7:net_cls,net_prio:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/ed89697807a981b82f6245ac3a13be232c1e13435d52bc3f53060d61babe1997
6:cpu,cpuacct:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/ed89697807a981b82f6245ac3a13be232c1e13435d52bc3f53060d61babe1997
5:blkio:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/ed89697807a981b82f6245ac3a13be232c1e13435d52bc3f53060d61babe1997
4:pids:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/ed89697807a981b82f6245ac3a13be232c1e13435d52bc3f53060d61babe1997
3:devices:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/ed89697807a981b82f6245ac3a13be232c1e13435d52bc3f53060d61babe1997
2:rdma:/
1:name=systemd:/kubepods/besteffort/poda9c80282-3f6b-4d5b-84d5-a137a6668011/ed89697807a981b82f6245ac3a13be232c1e13435d52bc3f53060d61babe1997
0::/
</code></pre>
<hr />
<h1>Namespaces</h1>
<p>Containers in pod also share network and IPC namespaces by default.</p>
<pre><code># cd /proc/19768/ns/
# /proc/19768/ns# ls -lrt
total 0
lrwxrwxrwx 1 root root 0 Jul  4 01:41 uts -&gt; uts:[4026536153]
lrwxrwxrwx 1 root root 0 Jul  4 01:41 user -&gt; user:[4026531837]
lrwxrwxrwx 1 root root 0 Jul  4 01:41 pid_for_children -&gt; pid:[4026536154]
lrwxrwxrwx 1 root root 0 Jul  4 01:41 pid -&gt; pid:[4026536154]
lrwxrwxrwx 1 root root 0 Jul  4 01:41 net -&gt; net:[4026536052]
lrwxrwxrwx 1 root root 0 Jul  4 01:41 mnt -&gt; mnt:[4026536152]
lrwxrwxrwx 1 root root 0 Jul  4 01:41 ipc -&gt; ipc:[4026536049]
lrwxrwxrwx 1 root root 0 Jul  4 01:41 cgroup -&gt; cgroup:[4026531835]
</code></pre>
<pre><code># cd /proc/19653/ns
# /proc/19653/ns# ls -lrt
total 0
lrwxrwxrwx 1 root root 0 Jul  4 01:42 uts -&gt; uts:[4026536150]
lrwxrwxrwx 1 root root 0 Jul  4 01:42 user -&gt; user:[4026531837]
lrwxrwxrwx 1 root root 0 Jul  4 01:42 pid_for_children -&gt; pid:[4026536151]
lrwxrwxrwx 1 root root 0 Jul  4 01:42 pid -&gt; pid:[4026536151]
lrwxrwxrwx 1 root root 0 Jul  4 01:42 net -&gt; net:[4026536052]
lrwxrwxrwx 1 root root 0 Jul  4 01:42 mnt -&gt; mnt:[4026536149]
lrwxrwxrwx 1 root root 0 Jul  4 01:42 ipc -&gt; ipc:[4026536049]
lrwxrwxrwx 1 root root 0 Jul  4 01:42 cgroup -&gt; cgroup:[4026531835]
</code></pre>
<p>As you can see the containers share the network and IPC namespaces. You can also make the container share pid namespace using <code>shareProcessNamespace</code> field in the pod spec.</p>
<p><a href="https://kubernetes.io/docs/tasks/configure-pod-container/share-process-namespace" rel="noreferrer">https://kubernetes.io/docs/tasks/configure-pod-container/share-process-namespace</a></p>
<hr />
<blockquote>
<p>cgroup:[4026531835] is same for both the containers. Is this(cgroup namespace) different from the cgroups they (containers) are part of.</p>
</blockquote>
<p>cgroups limits the resources(cpu, memory etc) which a process(or group of processes) can use.</p>
<p>namespaces isolate and limit the visibility a process(or a group of processes) has over system resources like network, process trees etc. There are different namespace groups like network, IPC etc. One of such namespace is cgroup namespace. Using cgroup namespace you can limit the visibility of other cgroups from a process(or group of processes)</p>
<p>cgroup namespace virtualises the view of a process's cgroups. Currently if you try <code>cat /proc/self/cgroup</code> from within the container, you would be able to see the full cgroup hierarchy starting from the global cgroup root. This can be avoided using cgroup namespaces and is available from <a href="https://github.com/kubernetes/enhancements/pull/1370" rel="noreferrer">kubernetes v1.19</a>. <a href="https://github.com/moby/moby/pull/38377" rel="noreferrer">Docker also supports this from version 20.03</a>. When cgroup namespace is used while creating the container, you would see the cgroup root as <code>/</code> inside the container instead of seeing the global cgroups hierarchy.</p>
<p><a href="https://man7.org/linux/man-pages/man7/cgroup_namespaces.7.html" rel="noreferrer">https://man7.org/linux/man-pages/man7/cgroup_namespaces.7.html</a></p>
