Lecture 02 · E2C Continuum and Cloud Computing
المحاضرة 02 · استمرارية E2C والحوسبة السحابية
Containers: namespaces and cgroups
الحاويات (Containers): namespaces وcgroups
P1 MUSTA container is not a small VM. It is an ordinary process with a restricted view and a resource budget. Namespaces give the view, cgroups give the budget.
الحاوية (container) ليست جهازاً افتراضياً صغيراً. إنها عملية (process) عادية لها رؤية مقيّدة وميزانية موارد. فالـ namespaces تعطيها الرؤية، والـ cgroups تعطيها الميزانية.
OS-level virtualization: no hypervisor at all
المحاكاة الافتراضية على مستوى نظام التشغيل: بلا hypervisor إطلاقاً
Slide 25 calls containers OS-level virtualization. There is no hypervisor and no guest kernel. The line that matters is the system call interface, the entry point through which user-space code asks the kernel for anything.
تسمّي الشريحة 25 الحاويات OS-level virtualization. فلا يوجد hypervisor ولا نواة ضيفة (guest kernel). والخط المهم هو system call interface، أي نقطة الدخول التي يطلب منها كود user space أي شيء من النواة.
- Above the line (isolated): each container's own user space, its application and libraries.
فوق الخط (معزول): user space الخاص بكل حاوية، أي تطبيقها ومكتباتها.
- Below the line (shared): one host kernel and the hardware, used by every container.
تحت الخط (مشترك): نواة مضيفة واحدة والعتاد، تستخدمهما كل الحاويات.
Three consequences follow. Containers are light and fast (starting one is starting a process). They are tied to the host's OS family (a Linux host runs only Linux containers). And their security boundary is weaker than a VM's: one kernel bug can be a door between every container on the host.
ينتج عن ذلك ثلاث نتائج. الحاويات خفيفة وسريعة (تشغيل حاوية يعني تشغيل عملية). وهي مرتبطة بعائلة نظام المضيف (مضيف Linux لا يشغّل إلا حاويات Linux). وحدودها الأمنية أضعف من حدود VM: فثغرة واحدة في النواة قد تكون باباً بين كل الحاويات على المضيف.
In the cloud the two are combined, not rivals: containers usually run inside VMs. The VM separates tenants; the containers package apps inside each VM. The worker nodes of EKS, GKE and AKS are VMs.
وفي السحابة يجتمع الاثنان ولا يتنافسان: الحاويات تعمل عادةً داخل أجهزة افتراضية. فالـ VM يفصل بين العملاء، والحاويات تحزم التطبيقات داخل كل VM. وعُقد العمل (worker nodes) في EKS وGKE وAKS كلها VMs.
Two problems, two kernel features (slide 26)
مشكلتان، وميزتان في النواة (الشريحة 26)
The slide's example: one server runs a database and a web server as plain processes. Two different things go wrong, and Linux fixes each with a different feature.
مثال الشريحة: خادم واحد يشغّل قاعدة بيانات وخادم ويب كعمليتين عاديتين. ويحدث خطآن مختلفان، ويعالج Linux كلاً منهما بميزة مختلفة.
| Problemالمشكلة | Solutionالحل | Kernel featureميزة النواة | Question it answersالسؤال الذي تجيب عنه |
|---|---|---|---|
| Processes are not contained in their own space: they see and touch each other's PIDs, ports, filesالعمليات ليست محصورة في مساحة خاصة بها: ترى وتلمس PIDs والمنافذ والملفات الخاصة ببعضها | Introduce separate spaces inside the kernelإنشاء مساحات منفصلة داخل النواة | NamespacesNamespaces | What can I see?ماذا يمكنني أن أرى؟ |
| Processes compete for the same CPU, memory and I/Oالعمليات تتنافس على نفس المعالج والذاكرة وI/O | Introduce limits on resource usageوضع حدود على استهلاك الموارد | Control groups (cgroups)مجموعات التحكم (Control groups، cgroups) | How much can I use?كم يمكنني أن أستهلك؟ |
المشكلة الأولى: العمليات ليست محصورة في مساحتها، فهي ترى وتلمس PIDs وports وملفات بعضها، والحل مساحات منفصلة داخل النواة، والميزة هي namespaces، وهي تجيب عن سؤال: ماذا أستطيع أن أرى؟ والمشكلة الثانية: العمليات تتنافس على نفس المعالج والذاكرة وI/O، والحل حدود على استهلاك الموارد، والميزة هي cgroups، وهي تجيب عن سؤال: كم أستطيع أن أستخدم؟
"Namespaces are walls. Cgroups are meters."
"الـ namespaces جدران. والـ cgroups عدّادات."
Walls stop you seeing the neighbours. Meters stop you using all the electricity. You need both: walls without meters still allow a power hog, and meters without walls still allow snooping. In the slide's sentence: namespaces make the DB and web server operate independently, cgroups make sure the DB does not eat all the CPU.
الجدران تمنعك من رؤية الجيران، والعدادات تمنعك من استهلاك كل الكهرباء. وتحتاج الاثنين: جدران بلا عدادات تسمح بمستهلك جشع، وعدادات بلا جدران تسمح بالتجسس. وبعبارة الشريحة: namespaces تجعل قاعدة البيانات وخادم الويب يعملان باستقلال، وcgroups تضمن ألا تلتهم قاعدة البيانات كل المعالج.
Namespaces: what a process can SEE (slide 27)
Namespaces: ما الذي تستطيع العملية أن SEE (تراه) (الشريحة 27)
A namespace wraps a global kernel resource so that the processes inside it appear to have their own private instance. There is one kernel and many views. Run unshare --fork --pid --mount-proc bash and ps a inside shows only two processes, with your shell as PID 1, while the host still sees the same shell under a large global PID such as 24517.
الـ namespace يغلّف مورداً عاماً في النواة بحيث تبدو العمليات بداخله وكأن لها نسخة خاصة منه. فهناك نواة واحدة ورؤى كثيرة. شغّل unshare --fork --pid --mount-proc bash، وسيُظهر ps a بالداخل عمليتين فقط، والـ shell الخاص بك هو PID 1، بينما يرى المضيف نفس الـ shell برقم عام كبير مثل 24517.
| NamespaceNamespace | What it isolatesما الذي يعزله |
|---|---|
| PID | Process ID numbers, restarting at 1أرقام معرّفات العمليات (Process ID)، وتبدأ من جديد من 1 |
| NetworkNetwork | Interfaces, IP stack, routing tables, ports (two containers can both listen on port 80)الواجهات، وIP stack، وجداول التوجيه، والمنافذ (يمكن لحاويتين أن تستمعا كلتاهما على المنفذ 80) |
| MountMount | Mount points, so each container has its own filesystem treeنقاط التركيب (mount points)، فيكون لكل حاوية شجرة ملفات خاصة بها |
| UTS | Hostname and domain nameاسم المضيف (hostname) واسم النطاق |
| IPC | System V IPC objects, POSIX message queuesكائنات System V IPC وطوابير الرسائل POSIX message queues |
| UserUser | User and group IDs (root inside can be unprivileged outside)معرّفات المستخدمين والمجموعات (root في الداخل قد يكون بلا صلاحيات في الخارج) |
| CgroupCgroup | The cgroup root directory a process seesمجلد جذر cgroup الذي تراه العملية |
| TimeTime | Boot and monotonic clock offsetsإزاحات ساعة الإقلاع (boot) والساعة الرتيبة (monotonic) |
الأنواع الثمانية: PID يعزل أرقام العمليات ويبدأ من 1. وNetwork يعزل الواجهات وIP stack وجداول التوجيه والمنافذ، فتستطيع حاويتان الاستماع على المنفذ 80 معاً. وMount يعزل نقاط التركيب فتكون لكل حاوية شجرة ملفات خاصة. وUTS يعزل اسم المضيف. وIPC يعزل كائنات الاتصال بين العمليات. وUser يعزل معرّفات المستخدمين، فيكون root بالداخل مستخدماً عادياً بالخارج. وCgroup يعزل جذر cgroup الذي تراه العملية. وTime يعزل إزاحات الساعة.
The slide's example: a web server process (server.js) runs in one namespace and MongoDB runs in another. The slide also says "an isolated workspace is referred to as a container", which oversimplifies: a single namespace is not a container. A real container combines several namespaces with cgroups and its own root filesystem.
مثال الشريحة: عملية خادم ويب (server.js) تعمل في namespace، وMongoDB في آخر. وتقول الشريحة أيضاً إن "مساحة العمل المعزولة تُسمّى حاوية"، وهذا تبسيط زائد: فـ namespace واحد ليس حاوية. الحاوية الحقيقية تجمع عدة namespaces مع cgroups ونظام ملفات جذري خاص بها.
Cgroups: what a process can USE (slide 28)
Cgroups: ما الذي تستطيع العملية أن USE (تستهلكه) (الشريحة 28)
Control groups organise processes into hierarchical groups whose use of CPU, memory, disk I/O and network can be limited and monitored. Each resource is enforced by a controller (cpu, memory, io, pids, cpuset). The slide gives four functions:
تنظّم control groups العمليات في مجموعات هرمية يمكن تقييد ومراقبة استهلاكها للمعالج والذاكرة وdisk I/O والشبكة. وكل مورد يفرضه controller خاص (cpu وmemory وio وpids وcpuset). وتذكر الشريحة أربع وظائف:
| Functionالوظيفة | Meaningالمعنى | cgroup v2 fileملف cgroup v2 |
|---|---|---|
| Limitingالتحديد (Limiting) | Hard quota on CPU, RAM, I/O bandwidthحصة صارمة من المعالج والذاكرة وعرض نطاق I/O | cpu.max, memory.max, io.max |
| Prioritizationتحديد الأولوية (Prioritization) | Relative shares when groups competeحصص نسبية عندما تتنافس المجموعات | cpu.weight, io.weight |
| Accountingالمحاسبة (Accounting) | Measure and log a group's usageقياس استهلاك المجموعة وتسجيله | cpu.stat, memory.current |
| Controlالتحكم (Control) | Freeze or thaw a whole group; the basis for checkpoint and restart with CRIUتجميد مجموعة كاملة أو استئنافها، وهذا أساس checkpoint وإعادة التشغيل باستخدام CRIU | cgroup.freeze |
الوظائف الأربع: Limiting أي حد صارم على المعالج والذاكرة وعرض I/O (الملفات cpu.max وmemory.max وio.max). وPrioritization أي حصص نسبية عند التنافس (cpu.weight وio.weight). وAccounting أي قياس الاستهلاك وتسجيله (cpu.stat وmemory.current). وControl أي تجميد المجموعة كلها أو إذابتها (cgroup.freeze)، وهو أساس checkpoint/restart بأداة CRIU.
Cgroups do not checkpoint anything by themselves. The freezer only stops every task at once so that a user-space tool such as CRIU can write a consistent checkpoint to disk and restore it later, possibly on another node.
الـ cgroups لا تصنع checkpoint بنفسها. فالـ freezer يوقف كل المهام دفعة واحدة فقط، لكي تكتب أداة في user space مثل CRIU نسخة متسقة على القرص وتستعيدها لاحقاً، وربما على عقدة أخرى.
Worked: reading a quota and a weight
مثال محلول: قراءة الحصة (quota) والوزن (weight)
1) hard limit. cpu.max = "50000 100000"
quota = 50,000 us of CPU time
period = 100,000 us
CPUs = 50000 / 100000 = 0.5 CPU
on a 4-core host: 0.5 / 4 = 12.5% of the machine.
once the group burns 50 ms in a 100 ms period it
waits for the next period, EVEN IF the host is idle.
docker --cpus=1.5 -> cpu.max = "150000 100000"
= 1.5 CPUs
2) weight (soft share). three busy containers,
cpu.weight 100, 100, 200 (sum = 400), no cpu.max:
A = 100/400 = 25%
B = 100/400 = 25%
C = 200/400 = 50%
if A and B go idle, C may take 100%.
weights only matter under contention.
3) both at once. 1-CPU host, X has weight 300 and
cpu.max "50000 100000"; Y has weight 100, no cap.
by weight: X = 300/400 = 0.75 CPU
but X is capped at 0.5 CPU, so X gets 0.5
Y takes the rest: Y gets 0.5
4) memory. memory.max = 512M (536870912 bytes).
the app grows past 512 MiB, the kernel cannot
reclaim enough -> OOM killer kills a process
INSIDE that cgroup only. the neighbours survive.
This is how to read the slide's picture of four groups at 25% each: with equal weights, each gets a quarter only while all four are busy. A limit is a ceiling that holds even on an idle machine; a weight is a share that matters only under contention.
وهكذا تُقرأ صورة الشريحة لأربع مجموعات بنسبة 25% لكل منها: مع أوزان متساوية، تحصل كل مجموعة على الربع فقط ما دامت الأربع مشغولة. فالحد (limit) سقف يبقى حتى على جهاز خامل، والوزن (weight) حصة لا تهم إلا عند التنافس.
The see/use swap. The classic matching or true/false item reverses them: "namespaces limit CPU and memory" or "cgroups hide processes from each other". Both are false. Namespaces = see (visibility). Cgroups = use (consumption). A process alone in its own PID namespace can still burn every core on the machine.
تبديل الرؤية والاستهلاك. السؤال الكلاسيكي في المطابقة أو صح وخطأ يعكسهما: "namespaces تحدّ من المعالج والذاكرة" أو "cgroups تُخفي العمليات عن بعضها". وكلتاهما خاطئة. Namespaces = الرؤية. وcgroups = الاستهلاك. وعملية وحيدة في PID namespace خاص بها تستطيع مع ذلك أن تستهلك كل أنوية الجهاز.
Containers do not each have their own kernel. Run uname -r inside any container and it prints the host's kernel version. That is also why a Linux host can run a Windows VM but not a native Windows container.
ليس لكل حاوية نواتها الخاصة. شغّل uname -r داخل أي حاوية وسيطبع إصدار نواة المضيف. ولهذا السبب أيضاً يستطيع مضيف Linux تشغيل VM بنظام Windows، لكنه لا يستطيع تشغيل حاوية Windows أصلية.
"Containers are more secure than VMs" is false. They are lighter and faster, but the shared kernel is a larger attack surface than a hypervisor.
عبارة "الحاويات أكثر أماناً من VMs" خاطئة. فهي أخف وأسرع، لكن النواة المشتركة سطح هجوم أكبر من hypervisor.
Everything on one card
كل شيء في بطاقة واحدة
Containers cheat card
بطاقة ملخّص الحاويات
- Container = namespaces + cgroups + root filesystem. No hypervisor, no guest kernel.
الحاوية = namespaces + cgroups + root filesystem. بلا hypervisor وبلا نواة ضيفة.
- Shared below the system call interface: kernel and hardware. Isolated above: user space.
مشترك تحت system call interface: النواة والعتاد. ومعزول فوقها: user space.
- Namespaces (see): PID, network, mount, UTS, IPC, user, cgroup, time.
Namespaces (الرؤية): PID وnetwork وmount وUTS وIPC وuser وcgroup وtime.
- Cgroups (use): limiting, prioritization, accounting, control (freeze; CRIU checkpoint/restart).
Cgroups (الاستهلاك): limiting وprioritization وaccounting وcontrol (التجميد، وcheckpoint/restart بأداة CRIU).
- Quota math: CPUs = quota / period; 50000/100000 = 0.5 CPU. Weights: wi / Σw, only under contention.
حساب الحصة: CPUs = quota / period، و50000/100000 = 0.5 CPU. والأوزان: wi / Σw، ولا تعمل إلا عند التنافس.
- Memory limit exceeded: OOM kill inside that cgroup only.
تجاوز حد الذاكرة: OOM kill داخل تلك cgroup فقط.
- In the cloud: containers usually run inside VMs.
في السحابة: تعمل الحاويات عادةً داخل VMs.
Check yourself
اختبر نفسك
A database on a shared Linux host starts using every CPU core and the website on the same host slows down. Which kernel feature fixes this?
قاعدة بيانات على مضيف Linux مشترك بدأت تستهلك كل أنوية المعالج، فتباطأ الموقع على نفس المضيف. أي ميزة في النواة تحل ذلك؟
Consumption is a cgroup problem. Namespaces only change what a process can see; a database alone in its own PID namespace can still use every core.
الاستهلاك مشكلة cgroups. أما namespaces فتغيّر فقط ما تراه العملية، وقاعدة بيانات وحيدة في PID namespace خاص بها تستطيع مع ذلك استهلاك كل الأنوية.
Inside a container, ps shows only two processes and your app has PID 1. What produces this view?
داخل حاوية، يُظهر ps عمليتين فقط، وتطبيقك رقمه PID 1. ما الذي يُنتج هذه الرؤية؟
The PID namespace gives its members their own numbering starting at 1, while the host still sees the same processes under global PIDs. No second kernel exists (C), and UTS isolates only the hostname (D).
يعطي PID namespace أعضاءه ترقيماً خاصاً يبدأ من 1، بينما يرى المضيف نفس العمليات بأرقامها العامة. ولا توجد نواة ثانية (C)، وUTS يعزل اسم المضيف فقط (D).
A container's cpu.max reads 50000 100000. How much CPU may it use?
قيمة cpu.max لحاوية هي 50000 100000. كم من المعالج يُسمح لها باستخدامه؟
CPUs = quota / period = 50000 / 100000 = 0.5. It is a hard limit, so option C (which describes a weight) is wrong. Option B divides the wrong way round.
CPUs = quota / period = 50000 / 100000 = 0.5. وهو حد صارم، فالخيار C (الذي يصف weight) خاطئ. والخيار B يقسم بالاتجاه المعكوس.
Which statement about containers is TRUE?
أي عبارة عن الحاويات صحيحة؟
OS-level virtualization: no hypervisor, no guest kernel, everything below the system call interface is shared. Sharing the kernel is exactly why containers are fast and also why their boundary is weaker than a VM's.
OS-level virtualization: لا hypervisor ولا نواة ضيفة، وكل ما تحت system call interface مشترك. ومشاركة النواة هي بالضبط سبب سرعة الحاويات، وسبب ضعف حدودها مقارنة بـ VM.
Match the cgroup function: "Measurement of a group's resource usage and logging of metrics".
طابِق وظيفة cgroup: "قياس استهلاك المجموعة للموارد وتسجيل المقاييس".
Slide 28's four functions: limiting sets quotas, prioritization allocates shares, accounting measures and logs usage, control freezes groups and supports checkpoint/restart.
وظائف الشريحة 28 الأربع: limiting يضع الحصص، وprioritization يوزّع الأنصبة، وaccounting يقيس الاستهلاك ويسجّله، وcontrol يجمّد المجموعات ويدعم checkpoint/restart.