Pods, Deployments and Services
🎯You'll be able to
Describe RouteRani as a Deployment and a Service, apply it with kubectl, and explain how labels connect them.
🏠The everyday picture
A taxi company keeps "5 cars on the airport route" (the plan), whichever drivers are on shift. Passengers call one phone number for the airport route, not a particular driver. The cars carry a sticker saying "airport", and the dispatcher sends calls to any car with that sticker.
📘What it really is
-
A Pod is the smallest unit: one or more containers that run together on one node and share a network address. Usually one app container per pod. Pods are disposable, like containers.
-
A Deployment keeps a number of identical pods running (through a ReplicaSet it manages) and handles updates.
- A Service gives a stable name and address to a changing set of pods, and spreads traffic across them. Types:
ClusterIP(default): reachable only inside the cluster, by its name (for examplerouterani-api);-
NodePortandLoadBalancer: reachable from outside (aLoadBalancerservice asks the cloud for a load balancer). -
Labels and selectors link them: pods carry
app: routerani-api, and the Service'sselectorpicks every pod with that label. - Namespaces group resources (
routerani-staging,routerani-prod) so names don't collide and permissions can differ. - RouteRani as YAML (not verified): ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: routerani-api spec: replicas: 2 selector: matchLabels: { app: routerani-api } template: metadata: labels: { app: routerani-api } spec: containers: - name: api image: routerani:1.0.3 ports: [{ containerPort: 3000 }] env: - { name: APP_VERSION, value: "1.0.3" }
apiVersion: v1 kind: Service metadata: name: routerani-api spec: selector: { app: routerani-api } ports: [{ port: 80, targetPort: 3000 }] ```
- Commands:
kubectl apply -f routerani.yamlcreates or updates;kubectl get deploy,pods,svc;kubectl describe pod <name>explains problems;kubectl logs <pod>;kubectl port-forward svc/routerani-api 8080:80lets you test from your computer.
📝A worked example
Input (not verified): load the image into kind with kind load docker-image routerani:1.0.3, run kubectl apply -f routerani.yaml (with the
database from Lesson 4 also applied), then kubectl get pods -l app=routerani-api and kubectl delete pod <one of them>.
Expected output: two pods Running; after the delete, kubectl get pods shows a new pod with a different name starting within seconds,
because the Deployment noticed only one was left. That's the control loop at work.
🌍Where it's used
- Every Kubernetes app: a Deployment for stateless services, a Service in front.
-
StatefulSets are used instead of Deployments for databases and queues that need stable names and storage, though many teams use a managed database instead.
-
Jobs and CronJobs run tasks to completion, such as nightly reports.
📝Case scenario
A team's new Service sent no traffic to their pods. kubectl describe svc showed Endpoints: <none>. The Deployment's pods were labelled
app: routerani while the Service selected app: routerani-api. Fixing the label made the endpoints appear. Checking endpoints is the
first step whenever a Service "doesn't work".
🖱️Try it
If you have a cluster, apply the YAML and run kubectl get endpoints routerani-api. Then change the Service's selector to a wrong label,
apply, and check again. Success: the endpoint list empties when labels don't match and fills when they do. If you don't have a cluster, write
down which line in each object makes the link.
⚠️Common mistakes
- Mismatched labels. Sign: a Service with no endpoints. Fix: the selector must match the pod template's labels exactly.
- Running bare pods. Sign: a crashed pod never comes back. Fix: run pods through a Deployment.
latestimages in Kubernetes. Sign: pods on different versions after restarts. Fix: exact version tags, as in CD1.
🛠️For your project
Not required in the projects. As a capstone stretch, write k8s/routerani.yaml for RouteRani and mark in your write-up whether you ran it.
✅Quick check
What does a Deployment do that a single pod doesn't?
Show the answer
Keeps the requested number of pods running and replaces them during updates.
How does a Service find its pods?
Show the answer
Its selector matches the pods' labels.
