Hands-on Lab

#Lab: Kubernetes Ingress

Configure external access to services with Ingress.

#๐ŸŽฏ Objectives

  • Install Ingress Controller
  • Create Ingress resources
  • Configure path and host-based routing
  • Enable TLS termination

#๐Ÿ“‹ Prerequisites

  • Kubernetes cluster
  • kubectl configured

#โฑ๏ธ Duration: 30 minutes


#Task 1: Install Ingress Controller (5 min)

#Minikube

bash
minikube addons enable ingress
kubectl get pods -n ingress-nginx

#Standard Kubernetes

bash
1kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.8.2/deploy/static/provider/cloud/deploy.yaml
2
3# Wait for controller
4kubectl wait --namespace ingress-nginx \
5  --for=condition=ready pod \
6  --selector=app.kubernetes.io/component=controller \
7  --timeout=120s

#Task 2: Deploy Sample Applications (5 min)

diagram
# App 1 - Blue
cat << 'EOF' | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-blue
spec:
  replicas: 2
  selector:
    matchLabels:
      app: blue
  template:
    metadata:
      labels:
        app: blue
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        command: ["/bin/sh", "-c"]
        args: ["echo 'Blue App' > /usr/share/nginx/html/index.html && nginx -g 'daemon off;'"]
---
apiVersion: v1
kind: Service
metadata:
  name: app-blue
spec:
  selector:
    app: blue
  ports:
  - port: 80
EOF

# App 2 - Green
cat << 'EOF' | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-green
spec:
  replicas: 2
  selector:
    matchLabels:
      app: green
  template:
    metadata:
      labels:
        app: green
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        command: ["/bin/sh", "-c"]
        args: ["echo 'Green App' > /usr/share/nginx/html/index.html && nginx -g 'daemon off;'"]
---
apiVersion: v1
kind: Service
metadata:
  name: app-green
spec:
  selector:
    app: green
  ports:
  - port: 80
EOF

#Task 3: Create Path-Based Ingress (10 min)

bash
1cat << 'EOF' | kubectl apply -f -
2apiVersion: networking.k8s.io/v1
3kind: Ingress
4metadata:
5  name: path-ingress
6  annotations:
7    nginx.ingress.kubernetes.io/rewrite-target: /
8spec:
9  ingressClassName: nginx
10  rules:
11  - http:
12      paths:
13      - path: /blue
14        pathType: Prefix
15        backend:
16          service:
17            name: app-blue
18            port:
19              number: 80
20      - path: /green
21        pathType: Prefix
22        backend:
23          service:
24            name: app-green
25            port:
26              number: 80
27EOF
28
29# Get ingress IP
30kubectl get ingress
31
32# For minikube
33minikube ip

#Test Path Routing

bash
1# Replace IP with your ingress IP
2INGRESS_IP=$(kubectl get ingress path-ingress -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
3
4curl http://$INGRESS_IP/blue
5curl http://$INGRESS_IP/green
6
7# Or for minikube
8curl http://$(minikube ip)/blue
9curl http://$(minikube ip)/green

#Task 4: Host-Based Ingress (5 min)

bash
1cat << 'EOF' | kubectl apply -f -
2apiVersion: networking.k8s.io/v1
3kind: Ingress
4metadata:
5  name: host-ingress
6spec:
7  ingressClassName: nginx
8  rules:
9  - host: blue.local
10    http:
11      paths:
12      - path: /
13        pathType: Prefix
14        backend:
15          service:
16            name: app-blue
17            port:
18              number: 80
19  - host: green.local
20    http:
21      paths:
22      - path: /
23        pathType: Prefix
24        backend:
25          service:
26            name: app-green
27            port:
28              number: 80
29EOF
30
31# Test with host header
32curl -H "Host: blue.local" http://$INGRESS_IP/
33curl -H "Host: green.local" http://$INGRESS_IP/

#Task 5: View Ingress Status (5 min)

bash
1# List all ingress
2kubectl get ingress
3
4# Describe ingress
5kubectl describe ingress path-ingress
6
7# Check ingress controller logs
8kubectl logs -n ingress-nginx -l app.kubernetes.io/component=controller --tail=20

#โœ… Success Criteria

  • Ingress controller running
  • Path-based routing works (/blue, /green)
  • Host-based routing works
  • Services accessible via Ingress

#๐Ÿงน Cleanup

bash
kubectl delete ingress path-ingress host-ingress
kubectl delete deployment app-blue app-green
kubectl delete service app-blue app-green