https://drive.google.com/file/d/1zDHaAE6VSwacb2Z4Bj1tgvMY2ZysmrDO/view?usp=sharing

๐Ÿ” YAML Breakdown

apiVersion: v1
kind: Service
metadata:
  name: nnappone-service
spec:
  selector:
    app: nnappone          # โ† Must match Pod labels
  ports:
    - protocol: TCP
      port: 8080           # โ† Service port (what clients connect to)
      targetPort: 80       # โ† Pod port (where app listens)

๐Ÿ”‘ Key Insight:

This Service creates a stable virtual IP (ClusterIP) that load-balances traffic to all Pods with label app: nnappone.


๐Ÿ“Œ How ClusterIP Services Work

Component Role
selector Links Service to Pods (app: nnappone)
port: 8080 Port exposed by the Service
targetPort: 80 Port exposed by the Pod (matches containerPort)
ClusterIP Virtual IP (e.g., 10.43.x.x) โ€” only reachable inside the cluster

๐ŸŽฏ Flow:

curl http://nnappone-service:8080

โ†’ Service (10.43.x.x:8080)

โ†’ Random Pod with app: nnappone โ†’ :80

๐Ÿ’ก DNS:

From any Pod in the same namespace:

curl <http://nnappone-service>  # Port 8080 by default

From other namespaces:

curl <http://nnappone-service.default.svc.cluster.local>


๐Ÿงช k3s Lab: Deploy Pod + Service + Test Connectivity

๐Ÿ”ง Step 1: Deploy a Pod with Matching Label

๐Ÿ’ก You need a Pod with app: nnappone label (from earlier chapters):

# pod-for-service.yaml
apiVersion: v1
kind: Pod
meta
  name: nnappone
  labels:
    app: nnappone          # โ† Must match Service selector
spec:
  containers:
  - name: web
    image: nginx
    ports:
    - containerPort: 80

# Apply Pod
kubectl apply -f pod-for-service.yaml

# Apply Service
kubectl apply -f service-for-pod.yml

๐Ÿ”ง Step 2: Verify Service Endpoints

# Check if Service found the Pod
kubectl get endpoints nnappone-service

# โœ… Expected:
# NAME                 ENDPOINTS         AGE
# nnappone-service     10.42.0.10:80     10s

# โŒ If ENDPOINTS = <none> โ†’ label mismatch!

๐Ÿ”ง Step 3: Test Connectivity

Option A: From a Temporary Debug Pod (Inside Cluster)

# Start debug Pod
kubectl run debug --image=curlimages/curl -it --rm -- sh

# Inside shell:
curl <http://nnappone-service:8080>
# โœ… Should return "Welcome to Network Nuts!"

# Exit
exit