Kubernetes Ingress Controller Setup Guide
A comprehensive guide for setting up and configuring NGINX Ingress Controller in Kubernetes, supporting both domain-based and path-based routing configurations.
Project Structure
.
├── README.md
├── ingress/
│ └── controller/
│ └── nginx/
│ ├── manifests/
│ │ └── nginx-ingress.1.5.1.yaml
│ └── config/
│ ├── domain-based-ingress.yaml
│ └── path-based-ingress.yaml
└── scripts/
└── install-helm.sh
Getting Started
Installing Helm
Helm is required for managing the NGINX Ingress Controller installation. Follow these steps to install Helm:
Download Helm
curl -o /tmp/helm.tar.gz -LO https://get.helm.sh/helm-v3.10.1-linux-amd64.tar.gz
Extract the archive
tar -C /tmp/ -zxvf /tmp/helm.tar.gz
Move helm binary to path
mv /tmp/linux-amd64/helm /usr/local/bin/helm
Make it executable
chmod +x /usr/local/bin/helm
Setting up Ingress Controller
Add the NGINX Ingress repository:
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm search repo ingress-nginx --versions
Configuration Guide
Deploy an app
deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: blog-app
labels:
app: blog-app
spec:
replicas: 1
selector:
matchLabels:
app: blog-app
template:
metadata:
labels:
app: blog-app
spec:
containers:
- name: blog-app
image: <YOUR-DOCKER-HUB>/blog-app:1.7
ports:
- containerPort: 80
service.yml
apiVersion: v1
kind: Service
metadata:
name: blog-app-service
spec:
selector:
app: blog-app
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
Path-based Routing
For routing based on URL paths:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: blog-service
spec:
ingressClassName: nginx
rules:
- host: <YOUR-DOMAIN-NAME>
http:
paths:
- path: /blog
pathType: Prefix
backend:
service:
name: blog-service
port:
number: 80
Rewrite Rules
For advanced path rewriting:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: blog-service
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
ingressClassName: nginx
rules:
- host: <YOUR-DOMAIN-NAME>
http:
paths:
- path: /path-a(/|$)(.*)
pathType: Prefix
backend:
service:
name: blog-service
port:
number: 80
Local Development
For local development and testing:
Port Forwarding Setup:
kubectl port-forward -n ingress-nginx service/ingress-nginx-controller 8080:80
For development and testing at poridhi lab, make sure you are using load balancer. Expose the ingress controller.
Testing Configuration:
Test the endpoint-
curl -H "Host: blog.<YOUR-DOMAIN-NAME>" http://localhost:8080/blog
For HTTPS-
curl -k -H "Host: blog.<YOUR-DOMAIN-NAME>" https://localhost:8443/blog
Troubleshooting
Common issues and their solutions:
- Controller Pod Issues
Check pod status-
kubectl get pods -n ingress-nginx
View controller logs-
kubectl logs -n ingress-nginx deploy/ingress-nginx-controller
Describe pod for events-
kubectl describe pod -n ingress-nginx <pod-name>
- Routing Problems
Verify ingress resource-
kubectl describe ingress <ingress-name>
Check endpoints-
kubectl get endpoints
Validate service-
kubectl describe service <service-name>
- Common Error Solutions:
404 Not Found:
Verify path configuration and service endpoint
503 Service Unavailable:
Check if backend service is running
502 Bad Gateway:
Validate service port configuration
SOCIAL SHARE CARD GENERATOR