Home KubernetesKubernetes Ingress with NGINX: Routing Traffic to Your Apps

Kubernetes Ingress with NGINX: Routing Traffic to Your Apps

by Knowledge Base Linux
Kubernetes Ingress with NGINX — Kubernetes Ingress with NGINX: Routing Traffic to Your Apps

Once your application runs inside Kubernetes, a new problem appears: how does traffic from the internet actually reach your Pods? You could expose every Service as a NodePort or pay for a cloud LoadBalancer per Service, but both approaches get messy fast. Kubernetes Ingress solves this cleanly — it is a single API object that defines how external HTTP and HTTPS traffic is routed to Services inside the cluster, by hostname and URL path, with TLS termination included. In this guide you will install the NGINX Ingress Controller with Helm, write Ingress resources with host and path rules, add TLS with cert-manager, and debug the 404 errors that trip up almost everyone the first time.

NGINX Ingress controller terminating TLS and load balancing illustration

Prerequisites – Kubernetes Ingress with NGINX

  • A working Kubernetes cluster (v1.25+) with kubectl configured. Check the guide to Kubernetes liveness and readiness probes if your Pods are not yet healthy — Ingress routes to unhealthy Pods will fail.
  • Helm 3 installed.
  • A demo application deployed as a Deployment + ClusterIP Service (any web app listening on port 80 works).
  • A domain name pointing at one of your nodes, or willingness to test with curl -H "Host: ..." locally.

What Is Kubernetes Ingress? – Kubernetes Ingress with NGINX

It helps to place Ingress next to the other exposure options. A NodePort opens a high port (30000–32767) on every node — fine for testing, ugly for production. A LoadBalancer Service provisions a cloud load balancer per Service — simple, but expensive when you have dozens of microservices. Kubernetes Ingress instead gives you one entry point: an Ingress Controller (a reverse proxy running as Pods, here NGINX) watches Ingress resources and configures itself from their rules. One controller, one external IP, and host/path-based routing to any number of backend Services — plus a single place to terminate TLS.

Important nuance: creating an Ingress object does nothing by itself. You need an Ingress Controller deployed in the cluster to implement it. NGINX is the most common choice; others include Traefik, HAProxy, and cloud-provider controllers.

Step 1 — Install the NGINX Ingress Controller with Helm

The community-maintained ingress-nginx chart is the standard install path:

# Add the chart repository
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update

# Install the controller into its own namespace
helm install ingress-nginx ingress-nginx/ingress-nginx \
  --namespace ingress-nginx --create-namespace

# Wait for the controller pods to be ready
kubectl wait --namespace ingress-nginx \
  --for=condition=ready pod \
  --selector=app.kubernetes.io/component=controller \
  --timeout=120s

On bare metal or local clusters (kind, k3s), the controller Service will stay Pending for an external IP unless you install something like MetalLB — that is normal. For testing, you can port-forward the controller and use curl -H "Host:" as shown in Step 4.

Step 2 — Write an Ingress Resource with Host and Path Rules

Assume you have two Services in the default namespace: web (port 80) and api (port 8080). This Kubernetes Ingress manifest routes myapp.example.com to web and myapp.example.com/api to api:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: myapp-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  ingressClassName: nginx
  rules:
  - host: myapp.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: web
            port:
              number: 80
      - path: /api(/|$)(.*)
        pathType: ImplementationSpecific
        backend:
          service:
            name: api
            port:
              number: 8080

Key points:

  • ingressClassName: nginx tells the cluster which controller should handle this Ingress. (The older kubernetes.io/ingress.class annotation still works but the field is preferred.)
  • pathType: Prefix matches / and everything under it, matching on path segments — /api would match /api/users but not /apix.
  • pathType: Exact matches only the literal path. ImplementationSpecific defers to the controller — NGINX treats it as a regex, which is why the /api(/|$)(.*) pattern above works with the rewrite-target: /$2 annotation to strip the /api prefix before forwarding.
  • The backend Service port number must match the Service’s port, not the container port.

Apply and inspect:

kubectl apply -f myapp-ingress.yaml
kubectl get ingress
kubectl describe ingress myapp-ingress

If your app reads configuration or secrets, keep them out of the Ingress manifest — see the Kubernetes ConfigMaps and Secrets guide for the right way to inject them.

Step 3 — Add TLS to Your Kubernetes Ingress with cert-manager (Outline)

Terminating TLS at the Ingress is one of the biggest wins of Kubernetes Ingress: your Pods keep serving plain HTTP while the controller handles certificates. The standard stack is cert-manager plus Let’s Encrypt:

  1. Install cert-manager: kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.15.0/cert-manager.yaml (check for the latest release tag first).
  2. Create a ClusterIssuer for Let’s Encrypt production (use the staging issuer first while testing to avoid rate limits).
  3. Add to your Ingress: the annotation cert-manager.io/cluster-issuer: letsencrypt-prod, a tls section listing the host and a secretName where the certificate will be stored.
  4. cert-manager will complete the HTTP-01 challenge through the same Ingress and store the certificate in the named Secret.
spec:
  ingressClassName: nginx
  tls:
  - hosts:
    - myapp.example.com
    secretName: myapp-tls
  rules:
  - host: myapp.example.com
    ...

Verify with kubectl get certificate and kubectl describe certificate myapp-tls — look for Ready: True.

Step 4 — Test Routing with curl

Before touching DNS, test the routing logic of your Kubernetes Ingress directly against the controller. Port-forward the controller Service, then send requests with an explicit Host header:

# Terminal 1: forward local 8080 to the ingress controller
kubectl port-forward -n ingress-nginx \
  svc/ingress-nginx-controller 8080:80

# Terminal 2: host-based routing
curl -H "Host: myapp.example.com" http://127.0.0.1:8080/

# Path-based routing
curl -H "Host: myapp.example.com" http://127.0.0.1:8080/api/health

# TLS (once cert-manager has issued the cert; -k skips verification for testing)
curl -k -H "Host: myapp.example.com" https://127.0.0.1:8443/

If you have real DNS pointed at the controller’s external IP, the same requests work without the -H flag.

Troubleshooting: Common 404 Causes and Debug Steps

A 404 from a Kubernetes Ingress controller almost always means “no rule matched” rather than “your app is broken”. Work through these in order:

  • No ingress class match: kubectl get ingress -o wide — the CLASS column must say nginx (or match your controller). An Ingress without ingressClassName is ignored by default when a default class is not set. Check kubectl get ingressclass.
  • Wrong Service port: the backend port in the Ingress must equal the Service’s port, not targetPort. Verify with kubectl get svc web.
  • Service has no endpoints: kubectl get endpoints web — if the endpoint list is empty, the Service selector does not match any ready Pods. Fix the selector or the Pod labels.
  • Path does not match: remember Prefix matches on segments. A rule for /api will not match /apiv2. Use kubectl describe ingress to re-read the effective rules.
  • Host header mismatch: requests with a Host that matches no rule fall through to the default backend, which returns 404. Test with explicit -H "Host:" to rule this out.
  • Read the controller logs: they show exactly which upstream was chosen (or why none was):
    kubectl logs -n ingress-nginx -l app.kubernetes.io/component=controller --tail=100
    
  • Check generated NGINX config: kubectl exec -n ingress-nginx deploy/ingress-nginx-controller -- cat /etc/nginx/nginx.conf | grep -A5 "myapp.example.com" confirms your rules actually made it into the proxy config.

Conclusion

Kubernetes Ingress with the NGINX controller gives you a single, declarative entry point for HTTP and HTTPS traffic: define host and path rules in one YAML manifest, terminate TLS in one place, and route to any number of backend Services. Install the controller with Helm, set ingressClassName: nginx, double-check that backend ports match your Services, and test with curl -H "Host:" before relying on DNS. When a 404 appears, check the class, the endpoints, and the controller logs in that order — the answer is in the routing rules far more often than in the application. For the authoritative reference, see the official Kubernetes Ingress documentation.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.