Worked examples — Containers — namespaces, cgroups, difference from VMs
4.2.41 · D3· Coding › Operating Systems › Containers — namespaces, cgroups, difference from VMs
Yeh page Containers — namespaces, cgroups, difference from VMs ka drill floor hai. Hum parent se do ideas lete hain — namespaces (ek process kya DEKH sakta hai) aur cgroups (ek process kya USE kar sakta hai) — aur unhe har us case se guzarte hain jo ek real machine ya exam throw kar sakti hai.
Shuru karne se pehle, ek reminder taaki kuch bhi use hone se pehle build ho jaye:
Scenario matrix
Containers ke baare mein har sawaal inhi cells mein se ek mein aata hai. Neeche diye worked examples mein se har ek us cell ke saath tagged hai jo woh cover karta hai, aur saath mein yeh sab hit karte hain.
| # | Case class | Tricky part | Example |
|---|---|---|---|
| A | CPU quota < period (fraction of a core) | share below 1 | Ex 1 |
| B | CPU quota = period (exactly one core) | boundary value | Ex 2 |
| C | CPU quota > period (multiple cores) | share above 1 | Ex 3 |
| D | Degenerate quota = max (unlimited) |
division by "infinity" | Ex 4 |
| E | Memory limit + OOM decision | byte arithmetic, over/under | Ex 5 |
| F | PID namespace numbering | "PID 1" ka surprise | Ex 6 |
| G | User namespace UID mapping | root-inside ≠ root-outside | Ex 7 |
| H | Container vs VM word problem | kaunsa, aur kyun | Ex 8 |
| I | Exam twist: cgroup without namespace | isolation vs limits confusion | Ex 9 |
CPU share: cases A, B, C, D
Agla figure ek CPU ki timeline hai. Time left se right chalti hai; har period ek box hai; har box ka shaded part woh quota hai jo group ko run karne ki allowed hai. Dekho ki fraction chaaron cases mein kaise badlta hai — woh fraction hi answer hai. (Case C ko zyada se zyada ek core chahiye, isliye usse ek se zyada aise timelines chahiye — hum usse Ex 3 mein words mein handle karte hain.)

Memory: case E
Namespaces: cases F and G
Agla figure do process tables side by side dikhata hai: host ka table (bahut saare PIDs) aur container ka private PID-namespace table (1 se renumbered). Red arrow follow karo — yeh wahi running process hai jo do alag ID badges pehne hai.

Host par, ps tumhara shell PID 4021 dikhata hai. Tum unshare --pid --fork --mount-proc bash run karte ho aur andar echo $$ run karte ho. Woh kaunsa PID print karta hai, aur kya host-4021 abhi bhi exist karta hai?
Forecast: ek fresh PID namespace counting kahaan se shuru karta hai?
unshare --pidnaye bash ko uska apna PID table deta hai. Yeh step kyun? Parent se yaad karo: harCLONE_NEWPIDek global PID list ko privatise karta hai (built via Processes and the clone/fork syscall).- Ek naya PID namespace 1 se number karta hai, isliye pehla process is mein PID 1 hai.
echo $$1print karta hai. Yeh step kyun? Figure ka right table 1 se shuru hota hai; abhi simply koi aur entry nahi hai. - Host view unchanged hai: wahi process host namespace mein still PID 4021 hai (red arrow — ek process, do numbers). Yeh step kyun? Namespaces views relabel karte hain; woh process ko host ki accounting se delete nahi karte.
Verify karo: do truths saath exist karte hain — andar PID , bahar PID . Koi contradiction nahi kyunki ek process ka ek ID per namespace hota hai jisme woh belong karta hai. ✓
Ek rootless container container UIDs ko host UIDs par map karta hai jo se shuru hote hain. Andar, whoami root (UID 0) kehta hai. Host UID actually kya hai, aur kya woh host par /etc/passwd delete kar sakta hai?
Forecast: agar inside-0, outside-100000 par map hota hai, toh outside-100000 ke paas actually kya powers hain?
- Prerequisite check karo: host ke paas launching user ko subordinate UIDs allocated hone chahiye, jaise
/etc/subuidmein ek linealice:100000:65536. Tabhi user namespace ko host par map kar sakta hai. Yeh step kyun? Kernel ek aise range mein mapping refuse karta hai jo user ko kabhi delegated nahi hua;/etc/subuidgrant ke binaunshare --user --map-rootsimply fail ho jaata hai, isliye poora example iska presuppose karta hai. - Mapping apply karo: container UID host UID . Yeh step kyun? Ek user namespace ek shift table hai: delegated range ke andar.
- Host UID ek ordinary, unprivileged account hai —
/etcka kuch bhi uska nahi hai. Yeh step kyun? Sirf host UID real root hai; , isliye koi host-root powers nahi. - Isliye container-root host ka
/etc/passwddelete nahi kar sakta — write host UID 100000 ke against check hoti hai aur deny ho jaati hai.
Verify karo: ; aur delegated top, isliye mapping legal hai. # prompt aur whoami=root inside-namespace truths hain; file-permission check host UID use karta hai. Sab consistent hai. ✓
Container vs VM: cases H and I
Ek team ko ek host par ek small Linux web service ki 1000 copies run karni hain, har ek ko RAM chahiye, milliseconds mein start hona chahiye, sab ek hi company par trust karte hain. Container ya VM? RAM difference dikhao.
Forecast: 1000× par "own kernel" ka cost sabse zyada kahaan bite karta hai?
- Har VM ko apna guest kernel + base OS boot karna padta hai, roughly sirf OS ke liye app se pehle. Total OS overhead , jo hai (gibibytes — upar defined). Yeh step kyun? Ek VM ek full OS duplicate karta hai (dekho Virtualization and Hypervisors (Type 1 vs Type 2)); woh fixed tax count se multiply hota hai.
- Har container ek host kernel share karta hai — uska OS overhead hai; sirf app ka count karta hai. Total app RAM , koi per-instance kernel tax nahi. Yeh step kyun? Ek kernel = container; kernel cost ek baar pay hoti hai, 1000 baar nahi.
- Decide karo: same OS, same trust domain, ms startup, huge count → containers.
Verify karo: VM OS tax ; container OS tax . Container plan poore duplicate kernels bachata hai — scale par decisive. ✓
Ek process ko ek cgroup mein rakha jaata hai jisme pids.max = 100 aur memory.max = 256M hai, lekin usse koi namespace nahi diya jaata. Us process se, kya tum (a) host ko fork-bomb kar sakte ho, (b) host PID 4021 dekh aur kill kar sakte ho?
Forecast: do features mein se kaunsa visibility control karta hai, aur kya humne use turn on kiya?
- (a) Fork-bomb:
pids.max = 100group ko 100 tasks par cap karta hai → bomb 100 par ruk jaata hai, 101vaanforkrefuse ho jaata hai. Yeh step kyun? cgroups quantity limit karte hain;pidscontroller group ke tasks count karta hai aur cap ke baad aglaforkrefuse karta hai (from Linux Kernel — syscalls). - (b) Visibility: koi PID namespace nahi hone se, yeh process host ki single global PID table share karta hai. Yeh step kyun? View ki isolation sirf namespaces se aati hai — aur humne koi add nahi kiya.
- (b) Result: yeh host PID 4021 shared table mein dekh sakta hai, aur agar yeh host par sufficient privilege ke saath run karta hai (jaise host root ke roop mein, ya PID 4021 ke owner ke roop mein) toh yeh use
killkar sakta hai — cgroup ne kabhi process chupaaya nahi aur na signal block kiya.pids.max/memory.maxlimits constrain karte hain ki woh kitna consume kar sakta hai, kise dekh sakta hai ya signal kar sakta hai nahi. Yeh step kyun? Yeh twist ka poora point hai: caps aur isolation orthogonal hain. Ek resource limit kisi target ko view se remove nahi karta aur na hi ekkillsyscall ko deny karta hai jo permission check otherwise allow karta.
Verify karo: cgroup caps satisfied ( pids, MiB) phir bhi host PID 4021 visible aur killable rehta hai — parent ka rule prove karta hai: cgroups = limits, namespaces = isolation; dono chahiye. ✓
Recall Poore matrix par self-test
cpu.max = "50000 200000" kitne cores deta hai? ::: cores
memory.max = 134217728 kitne MiB hai? ::: MiB
Naya PID namespace: pehle process ko kaunsa PID milta hai? ::: 1
User-ns container-0 ko base 100000 par map karta hai → host UID? ::: 100000 (unprivileged, root nahi)
Us mapping ke allow hone ke liye /etc/subuid mein kya hona chahiye? ::: user ke liye ek delegated range, jaise alice:100000:65536
Cgroup alone, koi namespace nahi: kya woh host processes hide kar sakta hai? ::: Nahi — sirf namespaces view isolate karte hain
memory.current RSS ke saath kya charge karta hai? ::: reclaimable page cache (isliye charged memory > RSS)
CPU ke liye Ratio (quota/period), memory ke liye bytes (÷ 1048576 for MiB), PID ke liye rename (1 se shuru), UID ke liye shift (base + id). Dekhna = namespaces, Use karna = cgroups.
Parent topic par wapas jao · prerequisite: Processes and the clone/fork syscall · related: Scheduling — CFS and CPU shares, Networking — virtual interfaces and bridges, Filesystems — mount and chroot.