Cloud Load Balancing
Google Cloud Networking (PCA track) GCP console — free tier (GUI + CLI)
مقصدObjectiveObjective
اس سبق کے بعد آپ global اور regional لوڈ بیلنسرز کے فرق سمجھیں گے، اور ایک HTTP لوڈ بیلنسر کے اجزا (backend service، health check، forwarding rule) بنا سکیں گے۔Is lesson ke baad aap global aur regional load balancers ke farq samajh sakenge, aur ek HTTP load balancer ke ajza (backend service, health check, forwarding rule) bana sakenge.After this lesson you will understand the difference between global and regional load balancers, and be able to build the components of an HTTP load balancer (backend service, health check, forwarding rule).
آسان مثالSimple AnalogySimple Analogy
لوڈ بیلنسر ایک ریسٹورنٹ کے مینیجر کی طرح ہے — آنے والے مہمانوں (ریکویسٹس) کو خالی میزوں (صحت مند سرورز) پر بٹھاتا ہے۔ Global LB دنیا بھر کی برانچز والا مینیجر ہے، regional LB صرف ایک شہر کی برانچ سنبھالتا ہے۔Load balancer ek restaurant ke manager ki tarah hai — aane wale mehmaanon (requests) ko khaali mezon (sehatmand servers) par bithata hai. Global LB duniya bhar ki branches wala manager hai, regional LB sirf ek shehar ki branch sambhalta hai.A load balancer is like a restaurant manager — it seats arriving guests (requests) at free tables (healthy servers). A global LB is the manager for branches worldwide; a regional LB manages one city's branch.
سیٹ اپLab SetupLab Setup
lab-vpc استعمال کریں۔ دو چھوٹی VMs بنائیں جن پر سادہ ویب سرور چل رہا ہو (مثلاً python3 http.server)۔ Health check کے لیے VMs پر پورٹ 80 کھلا ہونا چاہیے۔lab-vpc use karein. Do chhoti VMs banayein jin par saada web server chal raha ho (masalan python3 http.server). Health check ke liye VMs par port 80 khula hona chahiye.Use lab-vpc. Create two small VMs running a simple web server (e.g. python3 http.server). Port 80 must be open on the VMs for the health check.
اقداماتStepsSteps
Step 1
پہلے global LB کے لیے ایک global IP ریزرو کریں۔ یہی IP بعد میں forwarding rule سے جڑے گا اور دنیا بھر سے قابل رسائی ہوگا۔Pehle global LB ke liye ek global IP reserve karein. Yehi IP baad mein forwarding rule se jurega aur duniya bhar se qaabil-e-rasai hoga.First reserve a global IP for the global LB. This IP will later attach to the forwarding rule and be reachable worldwide.
gcloud compute addresses create lb-ip --global
🖱️ VPC network > IP addresses > Reserve external address — Type: GlobalVPC network > IP addresses > Reserve external address — Type: GlobalVPC network > IP addresses > Reserve external address — Type: Global
Step 2
اب health check بنائیں — یہ LB کو بتاتا ہے کہ کون سی VM زندہ اور جواب دینے کے قابل ہے۔ بیمار VM کو ٹریفک نہیں ملے گا۔Ab health check banayein — yeh LB ko batata hai ke kaun si VM zinda aur jawab dene ke qaabil hai. Beemar VM ko traffic nahi milega.Now create a health check — it tells the LB which VM is alive and able to respond. An unhealthy VM gets no traffic.
gcloud compute health-checks create http web-hc --port=80 --request-path=/ --check-interval=10s
🖱️ Compute Engine > Health checks > Create health check — پروٹوکول HTTP، پورٹ 80Compute Engine > Health checks > Create health check — protocol HTTP, port 80Compute Engine > Health checks > Create health check — protocol HTTP, port 80
Step 3
Backend service بنائیں — یہ VMs کے گروپ (instance group) کو health check سے جوڑتی ہے۔ LB دراصل اسی backend service کی طرف ٹریفک بھیجتا ہے۔Backend service banayein — yeh VMs ke group (instance group) ko health check se jorti hai. LB darasal isi backend service ki taraf traffic bhejta hai.Create a backend service — it binds a group of VMs (instance group) to the health check. The LB actually sends traffic to this backend service.
gcloud compute backend-services create web-backend --global --protocol=HTTP --health-checks=web-hc --timeout=30s
🖱️ Network services > Load balancing > Backends > Create backend serviceNetwork services > Load balancing > Backends > Create backend serviceNetwork services > Load balancing > Backends > Create backend service
Step 4
اب URL map اور target proxy: URL map طے کرتا ہے کہ کون سا راستہ (/api، /images) کس backend کو جائے گا۔ سادہ کیس میں سب کچھ ایک ہی backend پر جاتا ہے۔Ab URL map aur target proxy: URL map tay karta hai ke kaun sa rasta (/api, /images) kis backend ko jayega. Saada case mein sab kuch ek hi backend par jata hai.Now the URL map and target proxy: the URL map decides which path (/api, /images) goes to which backend. In the simple case everything goes to one backend.
gcloud compute url-maps create web-map --default-service=web-backend gcloud compute target-http-proxies create web-proxy --url-map=web-map
🖱️ Network services > Load balancing > Create load balancer — HTTP(S)، پھر وزڈ فالو کریںNetwork services > Load balancing > Create load balancer — HTTP(S), phir wizard follow kareinNetwork services > Load balancing > Create load balancer — HTTP(S), then follow the wizard
Step 5
آخری قدم: global forwarding rule — یہ ریزرو کردہ IP کو target proxy سے جوڑتی ہے۔ یہی وہ نقطہ ہے جہاں سے صارفین کی ریکویسٹس LB میں داخل ہوتی ہیں۔Aakhri qadam: global forwarding rule — yeh reserve karda IP ko target proxy se jorti hai. Yehi woh nuqta hai jahan se users ki requests LB mein dakhil hoti hain.Final step: the global forwarding rule — it binds the reserved IP to the target proxy. This is the entry point where user requests enter the LB.
gcloud compute forwarding-rules create web-fr --global --target-http-proxy=web-proxy --ports=80 --address=lb-ip
🖱️ Network services > Load balancing — نئے LB کی Frontend configuration میں IP چیک کریںNetwork services > Load balancing — naye LB ki Frontend configuration mein IP check kareinNetwork services > Load balancing — check the IP in the new LB's Frontend configuration
تصدیقVerifyVerify
LB کے IP پر براؤزر یا curl سے ریکویسٹ بھیجیں — جواب VMs میں سے کسی ایک سے آنا چاہیے۔ ایک VM بند کر کے دیکھیں کہ ٹریفک صرف صحت مند VM پر جاتا ہے۔LB ke IP par browser ya curl se request bhejein — jawab VMs mein se kisi ek se aana chahiye. Ek VM band kar ke dekhein ke traffic sirf sehatmand VM par jata hai.Send a request to the LB's IP with a browser or curl — the response should come from one of the VMs. Stop one VM and see that traffic goes only to the healthy VM.
gcloud compute backend-services get-health web-backend --global curl -s http://$(gcloud compute addresses describe lb-ip --global --format='value(address)')
خرابی دور کرناTroubleshootingTroubleshooting
⚠️ LB کا IP کھلتا ہے مگر "no healthy upstream" یا 502 کی خرابی آتی ہے۔LB ka IP khulta hai magar "no healthy upstream" ya 502 ki kharabi aati hai.The LB IP opens but you get a "no healthy upstream" or 502 error.
✅ Health check کی سیٹنگز چیک کریں — غلط پورٹ یا راستہ سب VMs کو unhealthy کر دیتا ہے۔ ساتھ فائر وال میں Google کے health-check رینجز (130.211.0.0/22، 35.191.0.0/16) کی اجازت ہونی چاہیے۔Health check ki settings check karein — ghalat port ya rasta sab VMs ko unhealthy kar deta hai. Saath firewall mein Google ke health-check ranges (130.211.0.0/22, 35.191.0.0/16) ki ijazat honi chahiye.Check the health check settings — a wrong port or path marks all VMs unhealthy. The firewall must also allow Google's health-check ranges (130.211.0.0/22, 35.191.0.0/16).
⚠️ ٹریفک صرف ایک VM پر جا رہا ہے، دوسری پر نہیں۔Traffic sirf ek VM par ja raha hai, doosri par nahi.Traffic goes only to one VM, not the other.
✅ دوسری VM کی health چیک کریں — ممکن ہے وہ unhealthy ہو۔ اگر دونوں healthy ہیں تو session affinity کی سیٹنگ دیکھیں؛ ڈیفالٹ پر ٹریفک تقسیم ہونا چاہیے۔Doosri VM ki health check karein — mumkin hai woh unhealthy ho. Agar dono healthy hain to session affinity ki setting dekhein; default par traffic taqseem hona chahiye.Check the second VM's health — it may be unhealthy. If both are healthy, look at the session affinity setting; on default, traffic should be distributed.
انٹرویو سوالاتInterview Q&AInterview Q&A
❓ Global اور regional لوڈ بیلنسر میں کیا فرق ہے؟Global aur regional load balancer mein kya farq hai?What is the difference between a global and a regional load balancer?
Global LB کا ایک anycast IP دنیا بھر میں کام کرتا ہے اور قریب ترین بیک اینڈ کو ٹریفک دیتا ہے؛ regional LB صرف ایک ریجن کے اندر کام کرتا ہے اور اس کا IP بھی ریجنل ہوتا ہے۔Global LB ka ek anycast IP duniya bhar mein kaam karta hai aur qareebi tareen backend ko traffic deta hai; regional LB sirf ek region ke andar kaam karta hai aur us ka IP bhi regional hota hai.A global LB has one anycast IP that works worldwide and sends traffic to the nearest backend; a regional LB works only inside one region and its IP is regional too.
❓ Backend service کیا ہے؟Backend service kya hai?What is a backend service?
یہ VMs یا کنٹینرز کے گروپ کو health check اور لوڈ بیلنسنگ سیٹنگز سے جوڑتی ہے۔ LB کا فرنٹ اینڈ اسی backend service کی طرف ٹریفک بھیجتا ہے۔Yeh VMs ya containers ke group ko health check aur load balancing settings se jorti hai. LB ka front end isi backend service ki taraf traffic bhejta hai.It binds a group of VMs or containers to a health check and load-balancing settings. The LB's front end sends traffic to this backend service.