TExcelTAD
Home › Kubernetes › Ingress & Gateway API

Ingress & Gateway API

A LoadBalancer Service per application gets expensive and gives you only layer-4 (TCP) routing. Real websites need HTTP routing: shop.example.com/api to the API, / to the front end, TLS certificates, redirects. Kubernetes offers two APIs for that:

  • Ingress (networking.k8s.io/v1): the original, simple and universally supported.
  • Gateway API (gateway.networking.k8s.io/v1): the newer, more expressive successor, with roles split between platform and app teams.

Both are only configuration. A controller (Traefik, HAProxy, Envoy Gateway, Istio, Cilium, NGINX Gateway Fabric, a cloud load balancer, …) must be installed to read them and actually proxy traffic, usually behind one LoadBalancer Service.

Warning: ingress-nginx is retired. For years the community ingress-nginx controller (kubernetes/ingress-nginx) was the default answer, and countless tutorials still install it. In November 2025 Kubernetes SIG Network and the Security Response Committee announced its retirement: best-effort maintenance ran until March 2026, and since then there are no releases, bug fixes or security patches, and the GitHub repositories are read-only. Existing installs keep working and the old charts and images stay downloadable, which is exactly why it is easy to miss. Don't choose it for new clusters. Check whether you run it with kubectl get pods --all-namespaces --selector app.kubernetes.io/name=ingress-nginx, and plan a move to Gateway API (see Gateway API) or to another, actively maintained Ingress controller. None of the alternatives is a drop-in replacement: ingress-nginx annotations (rewrites, snippets, auth) must be translated and tested.

An Ingress

yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: shop
spec:
  ingressClassName: traefik          # which controller handles it (an IngressClass name)
  tls:
    - hosts: [shop.example.com]
      secretName: shop-tls           # a Secret of type kubernetes.io/tls
  rules:
    - host: shop.example.com
      http:
        paths:
          - path: /api
            pathType: Prefix
            backend:
              service: {name: api, port: {number: 80}}
          - path: /
            pathType: Prefix
            backend:
              service: {name: frontend, port: {number: 80}}
    - host: admin.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service: {name: admin, port: {number: 8080}}
bash
kubectl get ingress
curl -H 'Host: shop.example.com' http://<ingress-ip>/api/orders

Path matching rules

pathType Matches
Exact Only that exact path (/api but not /api/)
Prefix Path elements split by /: /api matches /api, /api/, /api/orders, but not /apiv2
ImplementationSpecific Up to the controller (some accept regexes)

When several paths match, the longest one wins, and Exact beats Prefix. Here is a router that implements those rules and parses the Ingress YAML above:

python · try it yourself
import yaml

ingress = yaml.safe_load("""
rules:
  - host: shop.example.com
    http:
      paths:
        - {path: /api, pathType: Prefix, backend: {service: {name: api, port: {number: 80}}}}
        - {path: /api/health, pathType: Exact, backend: {service: {name: health, port: {number: 8081}}}}
        - {path: /, pathType: Prefix, backend: {service: {name: frontend, port: {number: 80}}}}
  - host: admin.example.com
    http:
      paths:
        - {path: /, pathType: Prefix, backend: {service: {name: admin, port: {number: 8080}}}}
""")

def prefix_match(rule_path, request_path):
    rule = [p for p in rule_path.split("/") if p]
    req = [p for p in request_path.split("/") if p]
    return req[:len(rule)] == rule

def route(host, path):
    candidates = []
    for rule in ingress["rules"]:
        if rule["host"] != host:
            continue
        for p in rule["http"]["paths"]:
            if p["pathType"] == "Exact" and p["path"] == path:
                candidates.append((len(p["path"]), 1, p))
            elif p["pathType"] == "Prefix" and prefix_match(p["path"], path):
                candidates.append((len(p["path"]), 0, p))
    if not candidates:
        return "404 (default backend)"
    best = max(candidates, key=lambda c: (c[0], c[1]))[2]["backend"]["service"]
    return f"{best['name']}:{best['port']['number']}"

tests = [("shop.example.com", "/"), ("shop.example.com", "/api/orders"), ("shop.example.com", "/apiv2"),
         ("shop.example.com", "/api/health"), ("admin.example.com", "/users"), ("blog.example.com", "/")]
for host, path in tests:
    print(f"{host + path:30} -> {route(host, path)}")
shop.example.com/              -> frontend:80
shop.example.com/api/orders    -> api:80
shop.example.com/apiv2         -> frontend:80
shop.example.com/api/health    -> health:8081
admin.example.com/users        -> admin:8080
blog.example.com/              -> 404 (default backend)

Note that /apiv2 went to the front end: Prefix compares whole path segments, not characters.

TLS

The tls section names a Secret holding tls.crt and tls.key. Most clusters automate certificates with cert-manager, which watches Ingresses (or Gateways) with an annotation and obtains Let's Encrypt certificates:

yaml
metadata:
  annotations:
    cert-manager.io/cluster-issuer: letsencrypt-prod

Gateway API

Gateway API splits the configuration by role:

Resource Owned by Purpose
GatewayClass Infrastructure provider Which implementation (like an IngressClass)
Gateway Platform team Listeners: ports, protocols, hostnames, TLS
HTTPRoute (also GRPCRoute, TLSRoute, …) App teams Matching rules and backends, attached to a Gateway
yaml
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: public
  namespace: infra
spec:
  gatewayClassName: envoy
  listeners:
    - name: https
      protocol: HTTPS
      port: 443
      hostname: "*.example.com"
      tls:
        certificateRefs: [{name: wildcard-tls}]
      allowedRoutes:
        namespaces: {from: All}
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: api
  namespace: shop
spec:
  parentRefs: [{name: public, namespace: infra}]
  hostnames: [shop.example.com]
  rules:
    - matches:
        - path: {type: PathPrefix, value: /api}
          headers: [{name: x-canary, value: "true"}]
      backendRefs: [{name: api-v2, port: 80}]
    - matches:
        - path: {type: PathPrefix, value: /api}
      backendRefs:
        - {name: api-v1, port: 80, weight: 90}
        - {name: api-v2, port: 80, weight: 10}

Things Ingress needs controller-specific annotations for are built in: header matching, traffic weights (canary releases), redirects, rewrites and cross-namespace attachment. A model of weighted backends, spreading 20 requests deterministically in proportion to the weights:

python · try it yourself
def weighted_schedule(backends, n):
    # smooth weighted round-robin (the algorithm nginx uses)
    current = {b: 0 for b, _ in backends}
    total = sum(w for _, w in backends)
    picks = []
    for _ in range(n):
        for b, w in backends:
            current[b] += w
        best = max(current, key=current.get)
        current[best] -= total
        picks.append(best)
    return picks

picks = weighted_schedule([("api-v1", 90), ("api-v2", 10)], 20)
print(" ".join("1" if p == "api-v1" else "2" for p in picks))
print({b: picks.count(b) for b in ["api-v1", "api-v2"]})
1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 2 1 1 1 1
{'api-v1': 18, 'api-v2': 2}

Tip: New projects should prefer Gateway API: it is the recommended path off ingress-nginx and is supported by all the major controllers. The Ingress API itself is stable and isn't going away, but its feature set is frozen, so if you keep Ingress objects, run them on a maintained controller.

Test yourself

With path: /api, pathType: Prefix, which request path matches?

  1. /apiv2
  2. /api/orders
  3. /ap
  4. /v1/api

In Gateway API, which resource does an application team usually own?

  1. GatewayClass
  2. Gateway
  3. HTTPRoute
  4. Node

Your cluster still runs the community ingress-nginx controller. What is the right plan?

  1. Nothing: existing installs keep working, so it is safe
  2. Plan a migration to Gateway API or another maintained controller: since March 2026 it gets no security fixes
  3. Pin an older image tag
  4. Delete all Ingress objects at once