🏫 The School›☸️ Kubernetes›☎️ धडा 04 — Services: शाळेचे स्वागत-कक्ष
🖼️ See the drawing + lab 🏠 Course home 🌿 Branch on GitHub ✏️ View source
🖼️ आकृती आणि labThe drawing + lab पूर्ण पानावर उघडा ↗Open full page ↗

☎️ धडा 04 — Services: शाळेचे स्वागत-कक्ष

📍 तुम्ही इथे आहात: 13 पैकी धडा 04 · मागचा: lesson-03-deployments · पुढचा: lesson-05-namespaces


📦 या ब्रँचमध्ये काय आहे

धडे 01–03, आणि: Service — सतत बदलणाऱ्या pods समोर एक स्थिर नाव आणि पत्ता. खऱ्या files:

🧒 5 वर्षांच्या मुलाला समजावल्यासारखे

तुम्हाला शाळेतल्या "3B वर्गातल्या कोणाशी तरी" बोलायचे आहे. आज प्रत्येक मूल कुठे बसले आहे ते तुम्ही पाठ करता का? नाही! मुले बाक बदलतात, आजारी पडून घरी जातात, नवी मुले येतात… तुम्ही वेडे व्हाल. 🤪

त्याऐवजी तुम्ही स्वागत-कक्षाला ☎️ फोन करता. शाळेचा एकच फोन नंबर आहे जो कधीच बदलत नाही. 3B मधली कोणती मुले आत्ता हजर आहेत ते स्वागत-कक्षाला नेहमी माहीत असते, आणि ते तुम्हाला त्यांच्यापैकी एकाशी जोडून देतात — प्रत्येक वेळी वेगळे मूल, आणि ते ठीक आहे, त्यांच्यापैकी कोणीही तुम्हाला मदत करू शकते.

Service म्हणजे हेच स्वागत-कक्ष:

🗺️ आकृती

flowchart LR
    caller["🐍 school-analytics pod<br/>calls http://school-api"]
    svc["☎️ Service school-api<br/>stable IP + DNS name<br/>port 80 → 3000"]
    p1["🪑 pod ...abc12<br/>IP 10.0.4.7"]
    p2["🪑 pod ...def34<br/>IP 10.0.9.2"]
    pdead["💀 old pod<br/>gone, forgotten"]
    caller --> svc
    svc -->|"picks by label<br/>app: school-api"| p1
    svc --> p2
    svc -.-x pdead

❓ काय

🧠 Pod IP विरुद्ध Service — एका वाक्यात

Pod IP            → temporary (changes on every restart)
Service           → stable DNS name + virtual IP, for as long as you keep it
Service selector  → finds the matching, READY Pods behind the name

Service तुमचे application चालवत नाही; ते चालवणाऱ्या Pods ना स्थिर networking देते. Deployments गोष्टी चालवतात; Services गोष्टी शोधतात.

🤔 का

धडा 03 ने pods टाकाऊ केले — नवा pod, नवा IP, प्रत्येक वेळी. Pod IPs hard-code केले तर दर तासाला काहीतरी तुटेल. Service "मला कोणाशी बोलायचे आहे" हे "ते आत्ता कुठे आहेत" पासून वेगळे करते — जसे phones मध्ये पाठ केलेले नंबर नाही तर contact ची नावे असतात. आपली दोन apps एकमेकांना असेच शोधतात: Python service फक्त http://school-api ला बोलावते आणि बाकीचे Kubernetes DNS करते.

🔧 कसे (या repo मध्ये)

k8s/service.yaml:

spec:
  type: ClusterIP
  selector:
    app: school-api      # ← same sticker the Deployment puts on pods
  ports:
    - port: 80           # ← callers dial 80...
      targetPort: 3000   # ← ...reception forwards to the container's 3000

आणि त्याचा फायदा k8s/analytics-deployment.yaml मध्ये:

env:
  - name: SCHOOL_API_URL
    value: "http://school-api"   # ← a NAME, not an IP. DNS + Service do the rest.

🧪 करून पाहा

kubectl apply -f k8s/service.yaml
kubectl -n school get svc                      # see the stable ClusterIP

kubectl -n school get endpointslices           # the live list reception keeps:
                                               # exactly your current pod IPs!

# Call the service BY NAME from a scratch pod inside the cluster:
kubectl -n school run tester --rm -it --image=curlimages/curl --restart=Never \
  -- curl -s http://school-api

# Delete a pod (lesson 03 chaos!) and check endpointslices again —
# reception updated its list all by itself.

🔧 या धड्यासाठी kubectl

kubectl get svc · kubectl get endpointslices · kubectl port-forward svc/school-api 3000:80

🔗 यावर आधारित: 🪪 AWS L15 (स्वागत-कक्ष = ALB ची कल्पना) · 🍱 Docker L05 (IPs नाही, नावे)

✅ तपासा — तुम्हाला काय दिसायला हवे

दुसऱ्या pod मधून curl http://school-api उत्तर देते; kubectl get endpointslices मध्ये नेमके READY pods चे IPs दिसतात.

🧹 साफसफाई

local cluster: kubectl delete -f k8s/ (किंवा तुम्ही apply केलेली file) — EKS वर चालू राहिलेल्या प्रत्येक गोष्टीचे तासाप्रमाणे बिल लागते

⚠️ नेहमीच्या चुका

⏭️ पुढे

आतापर्यंतचे सगळे school नावाच्या namespace मध्ये राहते — ते नेमके काय आहे?

git checkout lesson-05-namespaces

☎️ Lesson 04 — Services: the school reception desk

📍 You are here: Lesson 04 of 13 · Previous: lesson-03-deployments · Next: lesson-05-namespaces


📦 What's in this branch

Lessons 01–03, plus: the Service — one stable name and address in front of ever-changing pods. Real files:

🧒 Explain like I'm 5

You want to talk to "someone from Class 3B" at school. Do you memorize where every kid is sitting today? No! Kids move desks, go home sick, new kids join… you'd go crazy. 🤪

Instead, you call the reception desk ☎️. The school has ONE phone number that never changes. Reception always knows which kids from 3B are present right now, and connects you to one of them — a different kid each time, and that's fine, any of them can help you.

A Service is that reception desk:

🗺️ Diagram

flowchart LR
    caller["🐍 school-analytics pod<br/>calls http://school-api"]
    svc["☎️ Service school-api<br/>stable IP + DNS name<br/>port 80 → 3000"]
    p1["🪑 pod ...abc12<br/>IP 10.0.4.7"]
    p2["🪑 pod ...def34<br/>IP 10.0.9.2"]
    pdead["💀 old pod<br/>gone, forgotten"]
    caller --> svc
    svc -->|"picks by label<br/>app: school-api"| p1
    svc --> p2
    svc -.-x pdead

❓ What

🧠 Pod IP vs Service — the one sentence

Pod IP            → temporary (changes on every restart)
Service           → stable DNS name + virtual IP, for as long as you keep it
Service selector  → finds the matching, READY Pods behind the name

A Service does not run your application; it provides stable networking to the Pods that do. Deployments run things; Services find things.

🤔 Why

Lesson 03 made pods disposable — new pod, new IP, every time. Hard-coding pod IPs would break hourly. The Service decouples "who I want to talk to" from "where they are right now" — the same reason phones have contact names, not memorized numbers. It's also how our two apps find each other: the Python service just calls http://school-api and Kubernetes DNS does the rest.

🔧 How (in this repo)

k8s/service.yaml:

spec:
  type: ClusterIP
  selector:
    app: school-api      # ← same sticker the Deployment puts on pods
  ports:
    - port: 80           # ← callers dial 80...
      targetPort: 3000   # ← ...reception forwards to the container's 3000

And the payoff in k8s/analytics-deployment.yaml:

env:
  - name: SCHOOL_API_URL
    value: "http://school-api"   # ← a NAME, not an IP. DNS + Service do the rest.

🧪 Try it

kubectl apply -f k8s/service.yaml
kubectl -n school get svc                      # see the stable ClusterIP

kubectl -n school get endpointslices           # the live list reception keeps:
                                               # exactly your current pod IPs!

# Call the service BY NAME from a scratch pod inside the cluster:
kubectl -n school run tester --rm -it --image=curlimages/curl --restart=Never \
  -- curl -s http://school-api

# Delete a pod (lesson 03 chaos!) and check endpointslices again —
# reception updated its list all by itself.

🔧 kubectl for this lesson

kubectl get svc · kubectl get endpointslices · kubectl port-forward svc/school-api 3000:80

🔗 Builds on: 🪪 AWS L15 (reception desk = ALB idea) · 🍱 Docker L05 (names, not IPs)

✅ Verify — what you should see

from another pod curl http://school-api answers; kubectl get endpointslices lists exactly the READY pod IPs.

🧹 Clean up

local cluster: kubectl delete -f k8s/ (or the file you applied) — on EKS, anything left running bills by the hour

⚠️ Common mistakes

⏭️ Next

Everything so far lives in a namespace called school — what IS that?

git checkout lesson-05-namespaces
← PreviousdeploymentsNext →namespaces

This page is the lesson's README from the lesson-04-services branch, shown here so the whole School stays on one site. Code files open on GitHub at the same branch.