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

πŸ” Why Declare ports in a Pod?

🚨 Important:

Declaring containerPort does NOT publish the port outside the cluster (like -p in Docker).

It’s documentation + integration aid for:

Kubernetes does not block traffic if you omit ports β€” your app can still listen on any port. But best practice = always declare it.


πŸ”§ YAML Comparison

βœ… pod-with-ports.yml (Minimal)

apiVersion: v1
kind: Pod
metadata:
  name: nnwebserver
spec:
  containers:
    - name: nnwebserver
      image: lovelearnlinux/webserver:v1
      ports:
        - containerPort: 80
          name: http
          protocol: TCP

βœ… pod-simple-with-ports.yml (Realistic + Namespace)

apiVersion: v1
kind: Pod
metadata:
  name: nnappone
  namespace: learning        # ← New! Isolates resources
  labels:
    app: nnappone            # ← Used by Services/selectors
spec:
  containers:
    - name: networknuts-app
      image: lovelearnlinux/webserver:v1
      ports:
        - containerPort: 80
          name: http
          protocol: TCP

πŸ’‘ Key Additions in pod-simple-with-ports.yml:


πŸ“Œ Core Concepts Covered

Concept Why It Matters
containerPort Tells Kubernetes which port the app listens on (for introspection).
name: http Allows referencing the port by name (e.g., in Services: targetPort: http).
protocol: TCP Default is TCP, but you can use UDP (e.g., for DNS, streaming).
Namespace Logical cluster partition β€” avoids naming conflicts, enables multi-tenancy.
Labels Foundation for Service discovery (selector: { app: nnappone }).

πŸ§ͺ Unified Lab: Deploy Pod with Ports & Test Connectivity

We’ll use pod-simple-with-ports.yml since it includes namespace + labels.

πŸ”§ Step-by-Step

# 1. Create the 'learning' namespace
kubectl create namespace learning

# 2. Deploy the Pod
kubectl apply -f pod-simple-with-ports.yml

# 3. Verify it's running
kubectl get pods -n learning
# Should show: nnappone   1/1     Running

# 4. Get Pod IP and Node
kubectl describe pod nnappone -n learning | grep -E "IP:|Node:"

# Example output:
# IP:           10.244.0.5
# Node:         minikube/192.168.49.2

# 5. OPTION A: Test from inside the cluster (using another Pod)
kubectl run tester --image=curlimages/curl -it --rm -- sh -n learning
# Inside shell:
curl <http://10.244.0.5>
# β†’ Should return "Welcome to Network Nuts!"

# 6. OPTION B: Port-forward to your local machine
kubectl port-forward pod/nnappone 8080:80 -n learning
# Then open browser or run:
curl <http://localhost:8080>

# 7. Clean up
kubectl delete pod nnappone -n learning
kubectl delete namespace learning

πŸ’‘ Why not curl directly from your laptop to Pod IP?

Pod IPs are only reachable from inside the cluster (unless you’re on the node and have network access). That’s why we use port-forward or a temporary tester Pod.