DEV Community

Arif Hossain
Arif Hossain

Posted on

Kubernetes-Nginx Ingress controller with Path based routing

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
Enter fullscreen mode Exit fullscreen mode

Extract the archive

tar -C /tmp/ -zxvf /tmp/helm.tar.gz
Enter fullscreen mode Exit fullscreen mode

Move helm binary to path

mv /tmp/linux-amd64/helm /usr/local/bin/helm
Enter fullscreen mode Exit fullscreen mode

Make it executable

chmod +x /usr/local/bin/helm
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Image description

Set Version Variables:

CHART_VERSION="4.4.0"
APP_VERSION="1.5.1"
Enter fullscreen mode Exit fullscreen mode

Version Selection Guide:

The CHART_VERSION refers to the Helm chart version
The APP_VERSION refers to the NGINX Ingress Controller version
Always check the compatibility matrix
For production environments, use stable versions
Generate the Installation Manifest:

# Create directory structure
`mkdir -p ./ingress-controller/nginx/manifests/`

# Generate manifest using helm template
`helm template ingress-nginx ingress-nginx \
--repo https://kubernetes.github.io/ingress-nginx \
--version ${CHART_VERSION} \
--namespace ingress-nginx \
> ./ingress-controller/nginx/manifests/nginx-ingress.${APP_VERSION}.yaml`
Enter fullscreen mode Exit fullscreen mode

Modify Service Configuration:

Open the generated manifest file
Locate the Service configuration
Change the service type to NodePort
Configure ports:
HTTP: 30080
HTTPS: 30443
Deploy the Controller:

# Create namespace
`kubectl create namespace ingress-nginx`

# Apply the configuration
`kubectl apply -f ./ingress-controller/nginx/manifests/nginx-ingress.${APP_VERSION}.ya`ml
Enter fullscreen mode Exit fullscreen mode

Image description

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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:

  1. 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>

  1. Routing Problems

Verify ingress resource-

kubectl describe ingress <ingress-name>

Check endpoints-

kubectl get endpoints

Validate service-

kubectl describe service <service-name>

  1. 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

Top comments (0)