10. Setting up Kubernetes Ingress Controller on a Kind Cluster

10. Setting up Kubernetes Ingress Controller on a Kind Cluster

ยท

4 min read

In this tutorial, weโ€™ll set up a Kubernetes Ingress Controller on a Kind (Kubernetes in Docker) cluster to route traffic between two services: Apache and NGINX. The Ingress controller will handle routing based on URL paths.


๐Ÿ“‹ Pre-requisites

  1. Virtual Machine: Create an AWS instance with the following specs:

    • Type: t2.medium

    • CPU: 2

    • RAM: 4 GB

  2. Kind Setup: Install and configure Kind on the virtual machine.

  3. Kubernetes CLI (kubectl): Ensure kubectl is installed and configured.


๐ŸŒŸ Step-by-Step Implementation

Step 1: Install Kind and Create a Cluster

  1. Install Kind:

     curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.20.0/kind-linux-amd64
     chmod +x ./kind
     sudo mv ./kind /usr/local/bin/kind
    
  2. Create a Kind cluster:

     kind create cluster --name ingress-demo
    
  3. Check the cluster status:

     kubectl cluster-info
    

Step 2: Create the Notes App Deployment and Service

Create a file named notes-deployment.yml with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: notes-app-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: notes-app
  template:
    metadata:
      labels:
        app: notes-app
    spec:
      containers:
      - name: notes-app
        image: imkiran13/notes-app
        ports:
        - containerPort: 8000
---
apiVersion: v1
kind: Service
metadata:
  name: notes-app-service
spec:
  selector:
    app: notes-app
  ports:
    - protocol: TCP
      port: 8000
      targetPort: 8000
  type: ClusterIP

Apply the manifest:

kubectl apply -f notes-deployment.yml -n nginx

Verify deployment and service:

kubectl get all -n nginx


Step 3: Create the NGINX Deployment and Service

Create a file named nginx-deployment.yaml with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: ClusterIP

Apply the manifest:

kubectl apply -f nginx-deployment.yml -n nginx

Verify deployment and service:

kubectl get all -n nginx


Step 4: Enable Ingress on Kind

Kind clusters donโ€™t come with an Ingress controller pre-installed. Letโ€™s install the NGINX Ingress Controller.

  1. Install the ingress-nginx controller:

     kubectl apply -f https://kind.sigs.k8s.io/examples/ingress/deploy-ingress-nginx.yaml
    

  2. Verify the ingress controller is running:

     kubectl get pods -n ingress-nginx
    


Step 5: Configure the Ingress Resource

Create a file named ingress.yml with the following content:

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

Apply the manifest:

kubectl apply -f ingress.yml -n nginx

Step 6: Verify Ingress

After applying the Ingress resource, verify its status:

kubectl get ingress -n nginx

step 7:Get the Ingress-Nginx Service

The command:

kubectl get svc -n ingress-nginx

step 8:Port-Forward the Service

The command:

sudo -E kubectl port-forward service/ingress-nginx-controller -n ingress-nginx 8080:80 --address=0.0.0.0

This forwards traffic from localhost:8080 to the service's port 80 in the cluster.

Annotations in Kubernetes

Annotations in Kubernetes are key-value pairs attached to objects, like Pods, Services, or Ingress, to store non-identifying metadata. Unlike labels, annotations are not used for selection but can convey configuration details, debugging information, or additional functionality.

Common Use Cases

  1. Ingress Behavior Control: Modify how requests are routed using annotations like rewrite rules or timeouts.

  2. Service Discovery: Help external tools identify services.

  3. Monitoring & Logging: Add metadata for log aggregation tools.

  4. Custom Metadata: Store information like deployment timestamps, team ownership, or versioning.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-notes-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /

Explanation of Annotations Used:

  1. nginx.ingress.kubernetes.io/rewrite-target: Ensures the URL path is correctly rewritten when routing to backends.

๐ŸŽ‰ Congratulations!

Youโ€™ve successfully set up a Kind cluster with an Ingress controller to route traffic between two services (notes-app and NGINX). This setup demonstrates how Kubernetes Ingress makes it easy to manage and route traffic in a cluster.

Would you like to dive deeper into Ingress rules, TLS setup, or advanced configurations? Let me know! ๐Ÿš€

ย