Ravindra BagaleCourses & study guides Track your progress

Labs · DevOps

Lab: Call a Kubernetes Service by Its DNS Name from Another Pod and Keep a File Across Pod Restarts with a PersistentVolumeClaim

Intermediate40 minkind · kubectl

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 web and web.default.svc.cluster.local.
  • A PVC in Bound state 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

  1. 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 web
    

    What you should see: a ClusterIP address for web.

  2. Start a throwaway client Pod and look up the name:

    kubectl run client --rm -it --image=busybox:1.36 --restart=Never -- sh
    

    Inside the client shell run the next step.

  3. Inside the client:

    nslookup web
    nslookup web.default.svc.cluster.local
    wget -qO- http://web | head -n 4
    exit
    

    What you should see: both names resolve to the Service ClusterIP, and wget prints the nginx welcome HTML.

Part 2: A claim that keeps data

  1. Check the default StorageClass in kind:

    kubectl get storageclass
    

    What you should see: standard (default) with provisioner rancher.io/local-path.

  2. 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: uploads
    
  3. Apply and check the claim:

    kubectl apply -f data.yaml
    kubectl rollout status deploy/uploader
    kubectl get pvc uploads
    

    What you should see: STATUS Bound and CAPACITY 1Gi.

  4. 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.txt
    

    What you should see: invoice-1001 from the new Pod.

Clean up

  1. 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

MistakeWhat happensFix
Using a Pod IP in configBreaks after every restartUse the Service name
PVC stuck in PendingPod never startsCheck kubectl describe pvc; a default StorageClass must exist
Writing data outside the mount pathFile lost on Pod deleteWrite only under /data
Scaling a ReadWriteOnce claim to many nodesNew Pods cannot mount itUse 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.