PART 6: HANDS-ON LABS
Lab 1: Explore CNI and Pod Networking
Goal: Understand CNI and Pod networking.
bash
# Check CNI plugin
kubectl get pods -n kube-system | grep -E "cilium|calico|flannel"
# Check Pod CIDR
kubectl get nodes -o jsonpath='{.items[*].spec.podCIDR}'
# Create two Pods
kubectl run pod-a --image=busybox --restart=Never -- sleep 3600
kubectl run pod-b --image=busybox --restart=Never -- sleep 3600
# Get their IPs
kubectl get pods -o wide
# Test pod-to-pod connectivity
POD_A_IP=$(kubectl get pod pod-a -o jsonpath='{.status.podIP}')
kubectl exec pod-b -- ping -c 2 $POD_A_IP
# Cleanup
kubectl delete pod pod-a pod-b
Lab 2: Master Service Types
Goal: Practice all Service types.
bash
# Create a Deployment
kubectl create deployment web --image=nginx:1.25-alpine --replicas=3
# ClusterIP
kubectl expose deployment web --port=80 --target-port=80 --name=web-clusterip
kubectl run test --image=busybox --rm -it --restart=Never -- wget -qO- web-clusterip
# NodePort
kubectl expose deployment web --port=80 --target-port=80 --type=NodePort --name=web-nodeport
kubectl get svc web-nodeport
# LoadBalancer
kubectl expose deployment web --port=80 --target-port=80 --type=LoadBalancer --name=web-lb
kubectl get svc web-lb
# Cleanup
kubectl delete svc web-clusterip web-nodeport web-lb
kubectl delete deployment web
Lab 3: Configure CoreDNS and NodeLocal DNSCache
Goal: Deploy NodeLocal DNSCache.
bash
# Check CoreDNS
kubectl get pods -n kube-system -l k8s-app=kube-dns
kubectl get configmap coredns -n kube-system -o yaml
# Deploy NodeLocal DNSCache
wget https://raw.githubusercontent.com/kubernetes/kubernetes/master/cluster/addons/dns/nodelocaldns/nodelocaldns.yaml
sed -i 's/__PILLAR__DNS__SERVER__/10.96.0.10/g' nodelocaldns.yaml
sed -i 's/__PILLAR__DNS__DOMAIN__/cluster.local/g' nodelocaldns.yaml
kubectl apply -f nodelocaldns.yaml
# Verify
kubectl get pods -n kube-system -l k8s-app=node-local-dns
# Test
kubectl run test --image=busybox --rm -it --restart=Never -- \
nslookup kubernetes.default
Lab 4: Implement NetworkPolicy
Goal: Default deny + explicit allow.
bash
kubectl create namespace netpol-lab
# Create frontend and backend
kubectl create deployment frontend --image=nginx -n netpol-lab
kubectl create deployment backend --image=nginx -n netpol-lab
kubectl expose deployment backend --port=80 -n netpol-lab
# Test connectivity (should work — no policies yet)
kubectl run test -n netpol-lab --image=busybox --rm -it --restart=Never -- \
wget -qO- backend
# Apply default deny
kubectl apply -n netpol-lab -f - <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny
spec:
podSelector: {}
policyTypes: [Ingress, Egress]
EOF
# Test (should fail)
kubectl run test -n netpol-lab --image=busybox --rm -it --restart=Never -- \
wget -qO- backend
# Allow frontend → backend
kubectl apply -n netpol-lab -f - <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-frontend-to-backend
spec:
podSelector:
matchLabels:
app: backend
policyTypes: [Ingress]
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- protocol: TCP
port: 80
EOF
# Test from a frontend pod
FRONTEND_POD=$(kubectl get pods -n netpol-lab -l app=frontend -o jsonpath='{.items[0].metadata.name}')
kubectl exec -n netpol-lab $FRONTEND_POD -- wget -qO- backend
# Cleanup
kubectl delete namespace netpol-lab
Lab 5: Cilium L7 NetworkPolicy
Goal: Apply HTTP method-level policy.
Prerequisites: Cilium installed.
bash
# Deploy an HTTP server
kubectl create deployment httpbin --image=kennethreitz/httpbin
kubectl expose deployment httpbin --port=80
# L7 policy: allow only GET /get
kubectl apply -f - <<'EOF'
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: allow-get-only
spec:
endpointSelector:
matchLabels:
app: httpbin
ingress:
- fromEndpoints:
- matchLabels:
app: client
toPorts:
- ports:
- port: "80"
protocol: TCP
rules:
http:
- method: "GET"
path: "/get"
EOF
# Test
kubectl run client --image=busybox --labels=app=client --restart=Never -- sleep 3600
kubectl exec client -- wget -qO- httpbin/get # Should work
kubectl exec client -- wget -qO- httpbin/post # Should fail
# Cleanup
kubectl delete pod client --ignore-not-found
kubectl delete deployment httpbin
kubectl delete svc httpbin
kubectl delete ciliumnetworkpolicy allow-get-only
Lab 6: Deploy and Configure an Ingress Controller
Goal: Deploy NGINX Ingress and route traffic.
bash
# Deploy NGINX Ingress
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml
# Wait for it
kubectl wait --namespace ingress-nginx \
--for=condition=ready pod \
--selector=app.kubernetes.io/component=controller \
--timeout=180s
# Create an app
kubectl create deployment web --image=nginx:1.25-alpine
kubectl expose deployment web --port=80
# Create Ingress
kubectl apply -f - <<'EOF'
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web-ingress
spec:
ingressClassName: nginx
rules:
- host: app.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: web
port:
number: 80
EOF
# Test (with port-forward or /etc/hosts)
kubectl get ingress
kubectl port-forward -n ingress-nginx svc/ingress-nginx-controller 8080:80 &
sleep 2
curl -H "Host: app.local" http://localhost:8080
kill %1
# Cleanup
kubectl delete ingress web-ingress
kubectl delete svc web
kubectl delete deployment web
Lab 7: Migrate from Ingress to Gateway API
Goal: Convert Ingress to HTTPRoute.
Prerequisites: Gateway API CRDs installed.
bash
# Install Gateway API CRDs
kubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.0.0/standard-install.yaml
# Create a GatewayClass
kubectl apply -f - <<'EOF'
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata:
name: my-gateway-class
spec:
controllerName: example.com/gateway-controller
EOF
# Create a Gateway
kubectl apply -f - <<'EOF'
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: my-gateway
spec:
gatewayClassName: my-gateway-class
listeners:
- name: http
protocol: HTTP
port: 80
EOF
# Create an HTTPRoute
kubectl apply -f - <<'EOF'
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: app-route
spec:
parentRefs:
- name: my-gateway
rules:
- matches:
- path:
type: PathPrefix
value: /
backendRefs:
- name: web
port: 80
EOF
# Check
kubectl get gateways,httproutes
# Cleanup
kubectl delete httproute app-route
kubectl delete gateway my-gateway
kubectl delete gatewayclass my-gateway-class
Lab 8: Debug DNS Resolution Failure
Goal: Diagnose and fix DNS issues.
bash
# Create a Pod
kubectl run dns-debug --image=busybox --restart=Never -- sleep 3600
# Check resolv.conf
kubectl exec dns-debug -- cat /etc/resolv.conf
# Test resolution
kubectl exec dns-debug -- nslookup kubernetes.default
kubectl exec dns-debug -- nslookup google.com
# Test with trailing dot
kubectl exec dns-debug -- nslookup google.com.
# Check CoreDNS
kubectl get pods -n kube-system -l k8s-app=kube-dns
kubectl logs -n kube-system -l k8s-app=kube-dns --tail=20
# Check NodeLocal DNSCache (if installed)
kubectl get pods -n kube-system -l k8s-app=node-local-dns
# Cleanup
kubectl delete pod dns-debug
Lab 9: Build a Complete Secure Networking Stack
Goal: Default deny + allow + Ingress + DNS.
bash
kubectl create namespace secure-net
# Deploy app
kubectl create deployment web --image=nginx:1.25-alpine -n secure-net --replicas=2
kubectl expose deployment web --port=80 -n secure-net
# Default deny
kubectl apply -n secure-net -f - <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny
spec:
podSelector: {}
policyTypes: [Ingress, Egress]
EOF
# Allow ingress from ingress-nginx
kubectl apply -n secure-net -f - <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-ingress
spec:
podSelector:
matchLabels:
app: web
policyTypes: [Ingress]
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: ingress-nginx
ports:
- protocol: TCP
port: 80
EOF
# Allow egress DNS
kubectl apply -n secure-net -f - <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns
spec:
podSelector: {}
policyTypes: [Egress]
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: kube-system
ports:
- protocol: UDP
port: 53
EOF
# Ingress
kubectl apply -n secure-net -f - <<'EOF'
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web
spec:
ingressClassName: nginx
rules:
- host: secure.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: web
port:
number: 80
EOF
# Verify
kubectl get all,netpol,ingress -n secure-net
# Cleanup
kubectl delete namespace secure-net
1:1 copy of the class Google Doc, code in code blocks.