Labs · DevOps
Lab: Call a Kubernetes Service by Its DNS Name from Another Pod and Keep a File Across Pod Restarts with a PersistentVolumeClaim
Course: DevOps · Chapter 19: Kubernetes Services, Ingress and Volumes · Lesson: 19.12 Lab: name, DNS, one claim
Chapter 19 explains ClusterIP, Service DNS and PV/PVC/StorageClass; this lab proves both on kind.
Chala mitrano! Pod cha IP roj badalto, naav nahi badalat. Aani Pod gela tari data jaayla nako. Aaj dohi baghuya.
Suppose we are…
Suppose we are on an Infosys team running an orders API and a small upload service on Kubernetes. The API must find the upload service even though Pod IPs change on every restart, and uploaded files must not disappear when a Pod is replaced.
In this lab we call a Service by its DNS name from another Pod, then attach a PersistentVolumeClaim and prove a file survives deleting the Pod.
Goal of this lab
By the end you will have:
- A Service resolved as
webandweb.default.svc.cluster.local. - A PVC in
Boundstate using kind's default StorageClass. - A file that is still there after the Pod is deleted and recreated.
What you need
- A kind cluster (
kind create cluster --name lab). - 40 minutes.
Part 1: Service DNS
Create a small Deployment and Service:
kubectl create deployment web --image=nginx:1.27 --replicas=2 kubectl expose deployment web --port=80 kubectl get svc webWhat you should see: a
ClusterIPaddress forweb.Start a throwaway client Pod and look up the name:
kubectl run client --rm -it --image=busybox:1.36 --restart=Never -- shInside the client shell run the next step.
Inside the client:
nslookup web nslookup web.default.svc.cluster.local wget -qO- http://web | head -n 4 exitWhat you should see: both names resolve to the Service ClusterIP, and wget prints the nginx welcome HTML.
Part 2: A claim that keeps data
Check the default StorageClass in kind:
kubectl get storageclassWhat you should see:
standard (default)with provisionerrancher.io/local-path.Save a PVC and a Deployment that mounts it:
data.yaml
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: uploads spec: accessModes: ["ReadWriteOnce"] resources: requests: storage: 1Gi --- apiVersion: apps/v1 kind: Deployment metadata: name: uploader spec: replicas: 1 selector: matchLabels: { app: uploader } template: metadata: labels: { app: uploader } spec: containers: - name: app image: busybox:1.36 command: ["sh", "-c", "sleep 36000"] volumeMounts: - name: data mountPath: /data volumes: - name: data persistentVolumeClaim: claimName: uploadsApply and check the claim:
kubectl apply -f data.yaml kubectl rollout status deploy/uploader kubectl get pvc uploadsWhat you should see:
STATUS BoundandCAPACITY 1Gi.Write a file, delete the Pod, read the file from the new Pod:
kubectl exec deploy/uploader -- sh -c 'echo "invoice-1001" > /data/note.txt' kubectl delete pod -l app=uploader kubectl rollout status deploy/uploader kubectl exec deploy/uploader -- cat /data/note.txtWhat you should see:
invoice-1001from the new Pod.
Clean up
Delete everything, including the claim (this deletes the practice data):
kubectl delete -f data.yaml kubectl delete svc,deploy web # kind delete cluster --name lab
Ravindra Bagale's Tip
Inside the same namespace the short name web is enough. From another namespace use web.<namespace>. Never hard-code Pod IPs.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Using a Pod IP in config | Breaks after every restart | Use the Service name |
PVC stuck in Pending | Pod never starts | Check kubectl describe pvc; a default StorageClass must exist |
| Writing data outside the mount path | File lost on Pod delete | Write only under /data |
| Scaling a ReadWriteOnce claim to many nodes | New Pods cannot mount it | Use one replica or storage that supports ReadWriteMany |
Self-check checklist
0 of 4 done
Try-at-home challenge
Create a namespace shop, deploy another nginx Service there, and resolve it from the default namespace with web.shop and the full name.
Samajla ka? Names for finding, claims for keeping. Aata pudhe jaauya Terraform.