5.3.9 · AI-ML › MLOps & Deployment
Intuition Ek-sentence mental model
Kubernetes ek cluster operating system hai: tum ise ek desired state dete ho ("mere model server ke 3 replicas chalao, har ek ko 1 GPU aur 8 GB RAM chahiye"), aur iska control loop reality ko us desire se match karta rehta hai — containers ko automatically restart, reschedule, aur scale karta hai.
Intuition Yeh kis problem ko solve karta hai
Ek trained model apne laptop par rakha hua bekar hai. Ise serve karne ke liye tumhe chahiye: kaafi saare identical copies (traffic ke liye), ek crash hone par automatic restart, GPU allocation, bina downtime ke rolling updates, aur traffic spike hone par autoscaling. Yeh kaam dozens of machines par haath se karna impossible hai. Kubernetes (K8s) "servers ki ek dher" ko ek bada machine bana deta hai jise tum declaratively program karte ho.
Core idea KYA hai? Tum end state YAML mein describe karte ho. K8s ek reconciliation loop chalata hai: continuously desired state vs actual state compare karo, aur gap close karne ke liye actions lo.
action = reconcile ( desired , observed )
Yeh exactly ek control system hai — jaise ek thermostat. Woh analogy hi poori design hai.
Definition Kubernetes ke atoms
Pod : sabse chhota deployable unit — ek ya zyada containers jo network + storage share karte hain. Tum directly Pods rarely banate ho.
Deployment : ek ReplicaSet manage karta hai, jo N identical Pods ko alive rakhta hai; rolling updates enable karta hai.
Service : ek stable virtual IP + DNS name jo (hamesha badalte) Pods ke beech load-balance karta hai.
Node : ek worker machine (VM ya physical) jo actually Pods run karta hai.
Namespace : cluster ka ek logical partition (jaise dev, prod).
Yeh kaise nest hote hain:
Deployment → ReplicaSet → Pods → Containers, sab Nodes par schedule hote hain, Service ke through expose hote hain.
Intuition Requests & limits kyun hote hain
Scheduler ko decide karna hota hai ki Pod ko kaunse Node par rakha jaye. Woh memory/GPU needs guess nahi kar sakta, isliye tum unhe declare karte ho . Ek Pod sirf usi Node par place hota hai jiska free capacity ≥ Pod ka request ho.
Definition Requests vs Limits
request : scheduler jo guaranteed minimum reserve karta hai. Placement ke liye use hota hai.
limit : hard ceiling; CPU exceed karo → throttled, memory exceed karo → Pod OOM-killed ho jata hai.
Derivation — kya ek Pod fit hoga? Maano ek Node ka allocatable resource C hai (jaise 16 GB RAM). Existing Pods ∑ i r i request karte hain. Ek naya Pod request r ke saath fit hoga agar:
r ≤ C − ∑ i r i
Derivation — ek cluster mein kitne replicas fit honge? N identical nodes ke saath, har node ki capacity C hai, aur har replica r request karta hai:
max replicas = N ⌊ r C ⌋
Hum ⌊ ⋅ ⌋ per node use karte hain, globally nahi, kyunki ek single Pod do machines mein split nahi ho sakta — resources node-by-node pack hote hain.
Worked example GPU packing
Node = 4 GPUs. Har training Pod request karta hai nvidia.com/gpu: 1. Ek node ⌊ 4/1 ⌋ = 4 Pods hold kar sakta hai.
Yeh step kyun? GPUs stock K8s mein indivisible hain — tumhe whole GPUs milte hain, isliye floor exact hai aur koi fractional sharing nahi hai (jab tak MIG/time-slicing enable na karo).
Worked example Bin-packing waste
Node = 16 GB. Pod 5 GB request karta hai → ⌊ 16/5 ⌋ = 3 Pods 15 GB use karte hain, 1 GB stranded ho jata hai.
Yeh step kyun? Kyunki Pods whole units hain, ek request se kam leftover unusable hai → yeh motivate karta hai ki request sizes aisi choose karo jo node capacity ko divide karen.
Intuition Autoscale kyun karte hain
ML inference traffic bursty hoti hai. Peak ke liye provision karna paisa waste karta hai; average ke liye provision karna requests drop karta hai. Horizontal Pod Autoscaler (HPA) replica count adjust karta hai taaki ek chosen metric target ke paas rahe.
Derivation ek conservation idea se. Maano current replicas R c u r collectively metric value M c u r dikhate hain (jaise average CPU% ya requests/sec), aur tum chahte ho ki har replica target M t a r par ho. Maano load evenly shared hai aur replicas ke saath linearly scale karta hai. Total load L = R c u r ⋅ M c u r . Per-replica load M t a r ke equal banane ke liye:
R c u r ⋅ M c u r = R d es ⋅ M t a r ⇒ R d es = R c u r ⋅ M t a r M c u r
Kubernetes up round karta hai (ceil) taaki tum kabhi under-provision na karo:
4 replicas, avg CPU = 90%, target = 50%. R d es = ⌈ 4 ⋅ 90/50 ⌉ = ⌈ 7.2 ⌉ = 8 .
Yeh step kyun? Replicas double karne par per-replica load half ho jata hai (linear assumption), isliye humein itne replicas chahiye ki 90 ko 50 tak laaya ja sake.
8 replicas, avg CPU = 48%, target = 50%. Ratio = 0.96 → default 10% tolerance ke andar hai, isliye K8s kuch nahi karta.
Yeh step kyun? Ek dead-band choti fluctuations se oscillation rokta hai — same reason ki ek thermostat mein hysteresis hoti hai.
Definition K8s par key ML workload types
Job : run-to-completion (batch training). Failure par restart hota hai jab tak ek baar succeed na ho jaye.
CronJob : scheduled Jobs (nightly retraining).
StatefulSet : stable identity + persistent volume (distributed training ranks ke liye zaroori, jaise worker-0).
Deployment + Service : online inference server (stateless).
nodeSelector / taints & tolerations : GPU Pods ko sirf GPU nodes par force karo.
PersistentVolumeClaim (PVC) : datasets/checkpoints mount karo jo Pod restarts ke baad bhi survive karen.
Worked example Minimal GPU inference Deployment (YAML skeleton)
apiVersion : apps/v1
kind : Deployment
spec :
replicas : 3
template :
spec :
containers :
- name : model-server
image : myrepo/model:v2
resources :
requests : { cpu : "2" , memory : "8Gi" , nvidia.com/gpu : 1 }
limits : { cpu : "4" , memory : "8Gi" , nvidia.com/gpu : 1 }
GPU/memory ke liye request == limit kyun? GPUs over-commit nahi ho sakte, aur memory over-commit se OOM-kills ka risk hai — unhe match karna predictable, "Guaranteed" QoS deta hai.
Common mistake "Koi memory limit nahi rakhna safer hai — zaroorat padne par zyada use kar sakta hai."
Kyun sahi lagta hai: zyada headroom sunne mein lagta hai crashes se bachata hai.
Kyun galat hai: limit ke bina, ek leaking Pod poore Node ki RAM kha jata hai aur kernel neighbouring Pods ko bhi OOM-kill kar deta hai. Fix: memory limit set karo taaki blast radius sirf ek Pod tak rahe.
Common mistake "Safe rehne ke liye request = limit = peak usage set karo."
Kyun sahi lagta hai: guarantee hai ki kabhi khatam nahi hoga.
Kyun galat hai: scheduler request reserve karta hai chahe idle ho → tum peak ke liye 24/7 pay karte ho aur per node bahut kam Pods pack hote hain. Fix: typical usage ke liye request karo, peak ke liye limit set karo (CPU/memory ke liye jo bursting tolerate karte hain).
Common mistake "Ek Service un specific Pods ko load-balance karta hai jinhe maine name diya."
Kyun sahi lagta hai: tum un Pods ki taraf point karte ho jo tumne banaye.
Kyun galat hai: ek Service Pods ko label se select karta hai, dynamically. Pods mar jaate hain aur naye IPs le lete hain; Service jo bhi currently selector se match karta hai use track karta hai. Fix: IPs mein nahi, labels mein socho.
Common mistake "HPA ek crash-looping Pod ko fix kar dega."
Kyun sahi lagta hai: autoscaling sunne mein lagta hai ki sab problems handle karta hai.
Kyun galat hai: HPA metrics ke based par count scale karta hai; ek broken image sirf zyada replicas mein crash-loop karta rahega. Fix: HPA load ke liye hai, livenessProbe/readinessProbe health ke liye hain.
Recall Khud test karo (answers chhupao)
K8s kin do states ko reconcile karta hai? → desired vs actual/observed.
Scheduling kaunsa field drive karta hai, request ya limit? → request .
Desired replicas ka formula? → ⌈ R c u r M c u r / M t a r ⌉ .
Memory limit exceed karne par kya hota hai? → Pod OOM-killed ho jata hai.
Batch training ko completion tak kaunsa object run karta hai? → Job .
Service labels kyun use karta hai, IPs kyun nahi? → Pod IPs constantly change hoti hain.
Recall Feynman: 12-saal ke bachche ko explain karo
Socho tum ek pizza shop chalate ho jisme kaafi saare identical chefs (Pods) hain. Tum manager (Kubernetes) ko kehte ho: "hamesha 3 chefs kaam karte rahen, har ek ko ek oven chahiye." Agar ek chef quit kar de, manager turant naya hire kar leta hai. Agar badi rush aaye, manager aur chefs add kar deta hai (autoscaling). Customers ek number (Service) par phone karte hain aur manager unhe jo bhi chef free ho use route kar deta hai — unhe kabhi nahi pata kaunsa chef hai. Tum kabhi chefs ko haath se manage nahi karte; tum sirf manager ko goal batate ho, aur woh cheezein sahi rakhta hai.
Mnemonic Hierarchy yaad karo
"Do Read People, Now Serve" → D eployment → R eplicaSet → P ods → N odes → S ervice.
Requests vs limits ke liye: "Request to Reserve, Limit to Lockdown."
Kubernetes ko describe karne ke liye best mental model kya hai? Ek cluster OS jo ek reconciliation loop chalata hai jo actual state ko declared desired state ke equal rakhta hai.
Scheduler Pod ko place karne ke liye kaunsa resource field use karta hai? request (guaranteed reserved minimum), limit nahi.
Jab ek Pod memory limit aur CPU limit exceed kare toh kya hota hai? Memory over-limit → Pod OOM-killed ho jata hai; CPU over-limit → Pod throttled ho jata hai (killed nahi).
HPA desired-replica formula do. R_des = ceil(R_cur * M_cur / M_tar).
N nodes of capacity C par per-Pod request r ke saath max replicas kitne? N * floor(C / r), kyunki Pods per-node pack hote hain aur split nahi ho sakte.
Service kaise jaanta hai ki kaunse Pods ko route karna hai? Label selector se, dynamically — Pod IPs change hoti hain isliye yeh unhe kabhi hardcode nahi karta.
Batch training job completion tak kaunsa K8s object run karta hai? Ek Job (ya scheduled runs ke liye CronJob); yeh tab tak restart karta hai jab tak ek success na mil jaye.
Distributed training ke liye StatefulSet kyun use karte hain? Yeh stable network identity (worker-0, worker-1) aur stable persistent volumes deta hai jo har rank ko chahiye.
GPU inference Pods ke liye request=limit kyun set karte hain? GPUs over-commit nahi ho sakte aur memory over-commit se OOM ka risk hai; unhe match karna predictable Guaranteed QoS deta hai.
HPA ko tiny metric changes par oscillate karne se kya rokta hai? Ek tolerance dead-band (default ~10%) jiske neeche koi scaling nahi hoti.