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
Virtual Machine: Create an AWS instance with the following specs:
Type:
t2.medium
CPU: 2
RAM: 4 GB
Kind Setup: Install and configure Kind on the virtual machine.
Kubernetes CLI (kubectl): Ensure
kubectl
is installed and configured.
๐ Step-by-Step Implementation
Step 1: Install Kind and Create a Cluster
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
Create a Kind cluster:
kind create cluster --name ingress-demo
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.
Install the ingress-nginx controller:
kubectl apply -f https://kind.sigs.k8s.io/examples/ingress/deploy-ingress-nginx.yaml
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
Ingress Behavior Control: Modify how requests are routed using annotations like rewrite rules or timeouts.
Service Discovery: Help external tools identify services.
Monitoring & Logging: Add metadata for log aggregation tools.
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:
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! ๐