Distributed systems / Kubernetes ingress

Kubernetes ingress

How a request from the internet finds the right service inside a cluster: one public door, rules by host and path, the proxy that enforces them, and where HTTPS ends.

Intermediate7 steps~6 min
User — a browser out on the internet
Ingress controller — the proxy at the door
Ingress — the list of host and path rules
Service — stable address in front of pods
// THE SHORT VERSION

In 30 seconds

Services in a Kubernetes cluster are reachable only from inside it. An ingress is the way in for web traffic: one public address in front of many services.

It is a list of rules. Each rule says: requests for this host name, and this path, go to that service.

The rules do nothing alone. An ingress controller, a proxy running in the cluster, reads them and forwards each request. It usually also handles HTTPS, decrypting at the door: TLS termination.

Kubernetes ingress, step by step

STEPS01 / 07

Simplified: a cloud load balancer usually sits in front of the controller, and traffic inside can be encrypted again. Kubernetes’ newer Gateway API does the same job with more features.

STEP 01 / 07 Services stay inside
THE MATH
web 10.96.0.20 internal api 10.96.0.21 internal blog 10.96.0.22 internal
  1. Step 1: Services stay inside. A cluster runs three services: a shop front (web), its API and a blog. Each has a stable address, but those addresses only mean something inside the cluster. A browser out on the internet has no route to 10.96.0.20, so as things stand nobody outside can use any of them.
  2. Step 2: One door for HTTP. You could give every service its own public load balancer, each with its own address and its own cost. An ingress takes the other route: one entry point for web traffic, on one public address, that can reach every service inside. The door is a proxy called the ingress controller.
  3. Step 3: Rules by host. Both site names point at the same public address, so the controller needs another way to tell requests apart. Every HTTP request names the site it wants in its Host header. An ingress is a list of rules that maps hosts to services: blog.example.com to the blog service, shop.example.com to web.
  4. Step 4: Rules by path. Rules can also split one site by the path of the URL. On shop.example.com, anything starting with /api goes to the api service and everything else to web. When several paths match, the longest one wins, so /api/cart is sent to api even though it also starts with /.
  5. Step 5: The ingress controller. The ingress itself is only a list stored in the cluster; it moves no traffic. The work is done by an ingress controller, a reverse proxy such as NGINX, Traefik or HAProxy running in pods of its own. It watches the rules, reconfigures itself when they change, and forwards each request. No controller, no effect.
  6. Step 6: TLS termination. Browsers speak HTTPS, so the request arrives encrypted and its path cannot be read. The controller holds the site’s certificate and private key, completes the TLS handshake and decrypts. Now it can see the path, apply the rules and pass the request on. The encryption ends, or terminates, at the door.
  7. Step 7: A request, end to end. Put together: DNS turns shop.example.com into the one public address. The controller ends the TLS connection there, reads host and path, and finds the matching rule. It forwards the request to the api service, which hands it to one of its pods. The reply returns the same way, encrypted again at the door.
VALUES SO FAR
web10.96.0.20:80internal
api10.96.0.21:8080internal
door····public
by host····rule
by path····rule
proxy····enforces
TLS····port 443

Spotted a mistake on this page?