https://drive.google.com/file/d/15f1qmvb0fFdgfy36AyHULdF8q7y14OP1/view?usp=sharing

๐Ÿ” YAML Breakdown

# deployment-webserver-with-service.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nn-web
  namespace: learning
spec:
  selector:
    matchLabels:
      run: nn-web          # โ† Must match Pod template labels
  replicas: 2
  template:
    metadata:
      labels:
        run: nn-web         # โ† Labels for Pods
    spec:
      containers:
      - name: nn-webserver
        image: lovelearnlinux/webserver:v1
        ports:
        - containerPort: 80  # โ† Good practice (though optional)

๐Ÿ”‘ Key Insight:

This YAML only defines the Deployment.

The Service is created separately via:

kubectl expose deployment/nn-web --namespace=learning


๐Ÿ“Œ How Kubernetes Services Work

Component Role
Deployment Manages replicated Pods with label run=nn-web
Service Provides a stable IP/DNS name to access those Pods
Selector run=nn-web โ†’ links Service to Pods
Endpoints Auto-generated list of Pod IPs that match the selector

๐ŸŽฏ Flow:

curl <http://nn-web.learning.svc.cluster.local>

โ†’ Service (ClusterIP)

โ†’ Random Pod with run=nn-web label


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

๐Ÿ”ง Step 1: Create Namespace & Deploy

# Create namespace
kubectl create namespace learning

# Apply Deployment
kubectl apply -f deployment-webserver-with-service.yml

# Verify Pods
kubectl get pods -n learning -l run=nn-web -o wide

๐Ÿ”ง Step 2: Expose as Service

# Create ClusterIP Service (default)
kubectl expose deployment/nn-web --port=80 --target-port=80 -n learning

# Verify Service
kubectl get svc nn-web -n learning
# NAME     TYPE        CLUSTER-IP      PORT(S)
# nn-web   ClusterIP   10.43.123.45    80/TCP

# Check Endpoints (Pod IPs)
kubectl get endpoints nn-web -n learning
# NAME     ENDPOINTS                         AGE
# nn-web   10.42.0.10:80,10.42.1.15:80       10s

๐Ÿ” --port=80 = Service port

๐Ÿ”ง Step 3: Test Connectivity

๐Ÿ’ก In k3s, you can curl from any node or from a debug Pod.

Option A: Test from a Temporary Debug Pod

# Start a debug Pod in the same namespace
kubectl run debug -n learning --image=curlimages/curl -it --rm -- sh

# Inside the shell:
curl <http://nn-web>
# โœ… Should return "Welcome to Network Nuts!"

# Exit
exit

Option B: Test from a k3s Node (SSH into node)