Skip to main content

Command Palette

Search for a command to run...

kubernetes ingress

Updated
โ€ข3 min read

Below is a clean, working NGINX Ingress demo that routes:

  • /httpd โžœ httpd service

  • /nginx โžœ nginx service

This is a classic interview + lab demo ๐Ÿ‘
(Path-based routing)


โœ… Prerequisites (IMPORTANT)

  1. Ingress Controller must be installed

     kubectl create ns ingress-nginx
     kubectl get pods -n ingress-nginx
    

    If not installed (quick install):

     kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml
    
  2. Kubernetes cluster running (minikube / EKS / kubeadm)


๐Ÿ”น Step 1: Deploy HTTPD App

httpd-deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd-deploy
spec:
  replicas: 2
  selector:
    matchLabels:
      app: httpd
  template:
    metadata:
      labels:
        app: httpd
    spec:
      containers:
        - name: httpd
          image: httpd
          ports:
            - containerPort: 80

httpd-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: httpd-svc
spec:
  selector:
    app: httpd
  ports:
    - port: 80
      targetPort: 80

Apply:

kubectl apply -f httpd-deploy.yaml
kubectl apply -f httpd-svc.yaml

๐Ÿ”น Step 2: Deploy NGINX App

nginx-deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deploy
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx
          ports:
            - containerPort: 80

nginx-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  selector:
    app: nginx
  ports:
    - port: 80
      targetPort: 80

Apply:

kubectl apply -f nginx-deploy.yaml
kubectl apply -f nginx-svc.yaml

๐Ÿ”น Step 3: Create Ingress (Path-Based Routing)

ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: app-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
    - http:
        paths:
          - path: /httpd
            pathType: Prefix
            backend:
              service:
                name: httpd-svc
                port:
                  number: 80
          - path: /nginx
            pathType: Prefix
            backend:
              service:
                name: nginx-svc
                port:
                  number: 80

Apply:

kubectl apply -f ingress.yaml

๐Ÿ”น Step 4: Verify

kubectl get ingress
kubectl describe ingress app-ingress
kubectl get svc
kubectl get pods -o wide
kubectl logs -n ingress-nginx deploy/ingress-nginx-controller

๐Ÿ”น Step 5: Access Application

Get Ingress IP

kubectl get ingress

Example:

ADDRESS: 192.168.49.2

Access URLs

http://<INGRESS-IP>/httpd   โ†’ Apache HTTPD page
http://<INGRESS-IP>/nginx   โ†’ NGINX welcome page

๐ŸŽฏ Interview Explanation (Perfect Answer)

Ingress performs L7 routing and routes traffic based on URL paths.
In this demo,
/httpd traffic is routed to the httpd service, and /nginx traffic is routed to the nginx service using a single LoadBalancer IP.


๐Ÿง  Key Learning Summary

FeatureExplanation
IngressLayer-7 HTTP routing
ControllerNGINX Ingress Controller
Routing TypePath-based
BenefitSingle LB, multiple services
Rewrite TargetRemoves /httpd & /nginx

๐Ÿš€ Want Next?

  • Convert this to EKS + AWS ALB Ingress

  • Add TLS / HTTPS

  • Add host-based routing