Routing External Traffic with Ingress Controllers
When you first deploy a web application to Kubernetes, you need a way to let external users access it. Beginners often reach for NodePort or create a LoadBalancer service for every single microservice. While this works in a tiny test environment, it quickly becomes an expensive, unmanageable mess in production. To build a truly scalable architecture, we need a smart traffic cop: the Ingress Controller.
In this guide, we will stop using messy ports and start deploying the open-source NGINX Ingress Controller. You will learn how to route external HTTP and HTTPS traffic intelligently based on hostnames and URL paths.
- Why
NodePortandLoadBalancerservices fail at scale. - The difference between an Ingress Resource and an Ingress Controller.
- How to write YAML configurations for path-based and host-based routing.
- The exact architectural flow of a web request entering your cluster via NGINX.
1. The Problem with Standard Kubernetes Services
Before understanding Ingress, we must understand the pain points of the native Kubernetes service types when exposing applications to the internet.
| Service Type | How it works | The Architectural Problem |
|---|---|---|
| NodePort | Opens a specific port (30000-32767) on every node's IP address. | Requires users to type messy ports like example.com:31456. You must constantly modify firewall rules for new ports. |
| LoadBalancer | Provisions a dedicated Cloud Load Balancer (like AWS ELB) pointing to your nodes. | If you have 10 microservices, you pay for 10 separate cloud load balancers. Costs spiral out of control instantly. |
| Ingress | A single smart entry point that reads HTTP headers to route traffic to internal services. | Requires initial setup (Deploying a Controller), but provides infinite scalability and massive cost savings. |
An Ingress provides a single IP address (and a single cloud load balancer bill) that can route traffic to hundreds of different internal microservices using smart rules.
2. Practical Implementation & Code
To use Ingress, you need two things. First, an Ingress Controller (like NGINX, Traefik, or HAProxy) running in your cluster. Second, an Ingress Resource (a YAML file) that tells the controller how to route the traffic.
Assuming you have installed the NGINX Ingress Controller using Helm, let's create the routing rules. We want to route myapp.com/api to our backend service, and myapp.com/ to our frontend UI.
Writing the Ingress Resource
Example 1: Path-based routing in Kubernetes
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: main-application-ingress
annotations:
# Tells Kubernetes to use the NGINX controller
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- host: myapp.com
http:
paths:
# Route 1: Backend API
- path: /api
pathType: Prefix
backend:
service:
name: backend-api-service
port:
number: 8080
# Route 2: Frontend UI (Catch-all)
- path: /
pathType: Prefix
backend:
service:
name: frontend-ui-service
port:
number: 80
Expected outcome
$ kubectl apply -f ingress.yaml
ingress.networking.k8s.io/main-application-ingress created
$ curl http://myapp.com/api
{"status": "success", "data": "Backend API reached"}
$ curl http://myapp.com/
<html>Frontend UI loaded</html>
pathType: Prefix. This means any URL starting with /api (like /api/users or /api/orders) will correctly route to the backend service.3. Architecture Visualized
Let's look under the hood. How does a request actually travel from a user's browser, through the NGINX Ingress Controller, and into your application pod?
Traffic Routing via NGINX Ingress Controller
The NGINX Ingress Controller acts as a reverse proxy. It inspects the incoming HTTP request and actively forwards the connection to the correct backend pod based on the rules defined in your Ingress Resource.
4. Step-by-Step Execution Breakdown
To truly master Kubernetes networking, you need to know exactly how the packet flows. Here is what happens chronologically when a user types your URL into their browser.
myapp.com. DNS resolves this to the public IP of a single Cloud Load Balancer. This Cloud LB was provisioned automatically when you installed the NGINX Ingress Controller.Host: myapp.com and Path: /api.kube-proxy for maximum performance.5. Hands-On Practice
Test your knowledge with these configuration challenges. Write the YAML configurations in your local environment.
Write an Ingress resource that routes traffic based on subdomains instead of paths. Route api.myapp.com to the backend service and ui.myapp.com to the frontend service.
Hint: Use multiple - host: blocks under the rules: section.
Update your Ingress resource to accept HTTPS traffic for myapp.com.
Expected result: You will need to add a tls: block to your spec that references a Kubernetes Secret containing your TLS certificate and private key.
Your backend application expects traffic at the root path /, but your Ingress routes traffic from /api. Write an annotation that strips the /api prefix before sending the request to the pod.
Hint: Look into the nginx.ingress.kubernetes.io/rewrite-target annotation.
6. Interview Questions
If you are applying for a DevOps, Cloud, or Senior Backend Architect role, you are guaranteed to be asked about Kubernetes networking. Here is how you answer.
Answer: A LoadBalancer service operates at Layer 4 (TCP/UDP) and provisions a dedicated external cloud load balancer (and IP) for one specific service, which is expensive. An Ingress operates at Layer 7 (HTTP/HTTPS). It uses a single external IP and load balancer to route traffic to dozens or hundreds of internal services based on smart rules like URL paths and hostnames, drastically saving costs.
Answer: No, it typically bypasses the Kubernetes Service IP. The Ingress Controller acts as a reverse proxy. It continually watches the Kubernetes API for changes to Endpoints (or EndpointSlices). When a request matches a rule, NGINX load-balances and forwards the traffic directly to the pod IPs. This avoids the extra network hop of going through kube-proxy.
Conclusion
Transitioning from basic NodePorts to a fully configured NGINX Ingress Controller is the dividing line between a beginner Kubernetes deployment and a production-grade architecture. By utilizing Ingress, you gain granular control over URL routing, consolidate your SSL/TLS termination into a single location, and massively reduce your cloud infrastructure costs.
Mastering this flow ensures your microservices remain cleanly isolated internally while presenting a unified, professional API to the outside world.
Official Documentation
Upgradix by Shrash Studio – Learn Technology. Build Your Future.