KB: What are containers (chroot & cgroup)

 Dockers/Kubernetes are a glorified chroot and cgroups ;)

Root and Chroot
In a Unix-like OS, root directory(/) is the top directory. root file system sits on the same disk partition where root directory is located. And it is on top of this root file system that all other file systems are mounted. All file system entries branch out of this root. This is the system’s actual root.

But each process has its own idea of what the root directory is. By default, it is actual system root but we can change this by using chroot()system call. We can have a different root so that we can create a separate environment to run so that it becomes easier to run and debug the process. Or it may also be to use legacy dependencies and libraries for the process.
chroot changes the apparent root directory for the current running process and its children.

cgroups- Isolate and manage resources
Control groups(cgroups) is a Linux kernel feature which limits, isolates and measures resource usage of a group of processes. Resources quotas for memory, CPU, network and IO can be set. These were made part of Linux kernel in Linux 2.6.24.

Though Linux is excellent at handling and sharing available resources between processes, sometimes we want better control over resources.We want to allocate or guarantee a certain amount of resources to a group of processes. We do this with cgroups. This isolates an application/group’s resources.

Suppose we have an application we want to isolate usage for. Lets call it A1. Lets call rest of system as S. We will create a control group and assign resource limits on it: say 3GB of memory limit and 70% of CPU. Then we can add requisite application’s process id to the group and application resource usage now is throttled. Though the application may exceed the limits in normal scenarios, it will be throttled back to pre set limits in case system is facing resource crunch. This makes even more sense when we are handling many VMs running on a machine-have a cgroup for VMs and throttle them individually to a set limit when resource contention happens.

  • Define the solution to problem
  • Create a cgroup to handle the allocation
  • Add applications to the group.
  • Keep monitoring the group(happens as part of cgroups, we need not handle explicitly)

In Kubernetes, when a pod is created, a parent cgroup is created for that pod. All the containers within the pod are then placed into this parent cgroup. This parent cgroup is used to enforce resource limits for the entire pod, which includes all of its containers. So, in this sense, the pod's cgroup acts as the parent cgroup for all of the pod's containers.
Containers in pod also share network and IPC namespaces by default.



Reference:
https://itnext.io/chroot-cgroups-and-namespaces-an-overview-37124d995e3d

https://subscription.packtpub.com/book/cloud-and-networking/9781785888304/1/ch01lvl1sec10/coreos-high-level-architecture

https://medium.com/@BeNitinAgarwal/understanding-the-docker-internals-7ccb052ce9fe

https://earthly.dev/blog/chroot/

https://stackoverflow.com/questions/62727781/does-kubernetes-pod-have-namespace-and-cgroup-associated-with-it

Comments

Popular posts from this blog

KB: Azure ACA Container fails to start (no User Assigned or Delegated Managed Identity found for specified ClientId)

Electron Process Execution Failure with FSLogix

KB:RMM VS DEX (Remote Monitoring Management vs Digital Employee Experience)