https://drive.google.com/file/d/1Ga-gnT2z7j17YMyyRHL_8qb0t3XJdNa-/view?usp=sharing

๐Ÿ” YAML Breakdown

apiVersion: v1
kind: Service
meta
  name: website-svc
spec:
  type: NodePort          # โ† Exposes on ALL node IPs
  selector:
    app: nginx            # โ† Must match Deployment labels
  ports:
    - port: 80            # โ† Service port (internal)
      targetPort: 80      # โ† Pod port (where app listens)
      nodePort: 30007     # โ† Fixed external port (30000-32767)

๐Ÿ”‘ Key Insight:

This Service creates a stable external endpoint:

๐Ÿ’ก Why nodePort: 30007?


๐Ÿ“Œ How It Works with Your PVC-Backed App

Component Role
Deployment (webapp) Runs 1 Pod with app: nginx label + PVC-mounted /var/www/html
Service (website-svc) Routes traffic to that Pod via selector: app: nginx
PVC (myclaim) Persists web content across Pod restarts
NodePort Exposes the app externally on :30007

๐ŸŽฏ End-to-End Flow:

curl http://<NODE-IP>:30007

โ†’ NodePort (30007)

โ†’ Service (website-svc:80)

โ†’ Pod (webapp-xxxx:80)

โ†’ Persistent content from NFS/PV


๐Ÿงช k3s Lab: Test End-to-End Persistence + External Access

๐Ÿ”ง Step 1: Ensure All Components Are Deployed

# 1. PVC (from earlier)
kubectl get pvc myclaim

# 2. Deployment (with PVC)
kubectl apply -f deployment-using-pvc.yaml

# 3. Service
kubectl apply -f service-node-port.yaml

๐Ÿ”ง Step 2: Write Persistent Content

# Get Pod name
POD=$(kubectl get pods -l app=nginx -o jsonpath='{.items[0].metadata.name}')

# Write file
kubectl exec $POD -- sh -c "echo '<h1>Hello from Persistent NFS!</h1>' > /var/www/html/index.html"

๐Ÿ”ง Step 3: Test External Access

๐Ÿ’ก Get k3s node IP(s):

kubectl get nodes -o wide
# NAME         INTERNAL-IP
# k3s-master   192.168.1.10
# k3s-node1    192.168.1.11

๐ŸŒ From your laptop:

curl <http://192.168.1.10:30007>
# โœ… "Hello from Persistent NFS!"

curl <http://192.168.1.11:30007>
# โœ… Same response! (kube-proxy forwards to Pod anywhere in cluster)