Networking: Services, DNS, Ingress and policies · 6 MIN READ

Ingress: route HTTP by host and path through an ingress controller

Write an Ingress with host and path rules for an IngressClass, route two Services behind one entry point, and tell a controller 404 from a controller 503.

A LoadBalancer per Service is expensive and knows nothing about HTTP. An Ingress describes HTTP routing, meaning hosts, paths and TLS, for many Services at once, and an ingress controller turns that description into proxy configuration. Without a controller an Ingress object does nothing at all.

kubernetes · A MAP OF THE IDEA

  1. Run a controller
  2. Pick its IngressClass
  3. Route by host and path
  4. Read 404 versus 503

Useful when

  • One entry point and one load balancer for many HTTP Services.
  • Hosts, paths and TLS live next to the app in plain YAML.

Watch out

  • Without a matching controller an Ingress is silently ignored.
  • Annotations and ImplementationSpecific paths are not portable.

Implementation & operation cost: A controller to run, upgrade and patch, a pressing choice now that ingress-nginx is retired, plus a certificate per host.

01

The resource and the controller

An Ingress is only data in the API server. A controller such as ingress-nginx, Traefik, HAProxy or a cloud load balancer controller watches Ingresses of its class and configures a proxy, which is itself exposed by a LoadBalancer or NodePort Service, or on kind by host ports 80 and 443. An IngressClass names the controller in spec.controller; an Ingress picks a class with spec.ingressClassName, and the annotation ingressclass.kubernetes.io/is-default-class marks the class used when that field is empty. An Ingress for a class that no controller watches is accepted and silently ignored. The Ingress API is stable but frozen, and new routing features go into the Gateway API. The community ingress-nginx controller, which the lab runs and many tutorials use, was retired in March 2026 and no longer receives security fixes.

02

Host and path rules

Each rule matches one host, or every host when host is omitted, and lists paths, each with a backend Service name and port. The controller proxies to the Service's endpoints, usually straight to pod IPs, bypassing kube-proxy. The backend port is the Service port, not the container port, and the Service must live in the Ingress's namespace. Requests keep their path: /api/orders reaches the api Service as /api/orders, so the application must serve under that prefix unless you configure a controller-specific rewrite. A host may be a wildcard that covers exactly one DNS label, such as *.shop.example.com.

yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: shop
spec:
  ingressClassName: nginx
  rules:
  - host: shop.local
    http:
      paths:
      - path: /api
        pathType: Prefix
        backend:
          service:
            name: api
            port:
              number: 80
      - path: /
        pathType: Prefix
        backend:
          service:
            name: web
            port:
              number: 80
Read the code

One host, shop.local, with two paths: /api goes to the api Service on port 80 and everything else goes to web. Listing /api first only helps the reader, because the controller orders paths by length itself, so / never swallows /api. Both backends are named by Service and Service port, so moving pods or changing container ports never touches the Ingress.

03

pathType: Prefix, Exact and ImplementationSpecific

Prefix matches whole path elements split by slashes: /api matches /api, /api/ and /api/orders, but not /apiv2. Exact matches a single path, and /api does not match /api/. ImplementationSpecific leaves matching to the controller, which ingress-nginx treats as a prefix or, with an annotation, a regular expression; such manifests are less portable. When several paths match, the longest wins, and Exact beats Prefix at the same length, whatever the order in the YAML. A request that matches no rule goes to spec.defaultBackend if the Ingress sets one, and otherwise to the controller's built-in default backend, which for ingress-nginx answers 404.

04

TLS termination

spec.tls lists hosts and a Secret of type kubernetes.io/tls, holding tls.crt and tls.key, in the Ingress's namespace. The controller terminates TLS: it presents the certificate that matches the SNI name, decrypts, and forwards plain HTTP to the pods, which learn the original scheme from X-Forwarded-Proto. ingress-nginx also redirects HTTP to HTTPS for TLS hosts by default. cert-manager is the usual way to issue and renew these Secrets from Let's Encrypt or an internal CA. If the Secret is missing or does not cover the host, clients get the controller's self-signed fallback certificate, a classic cause of certificate errors right after a deploy. The lab has no TLS: it stores the tls section but serves plain HTTP on port 80 only.

yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: shop-tls
spec:
  ingressClassName: nginx
  tls:
  - hosts:
    - shop.example.com
    secretName: shop-example-com-tls
  rules:
  - host: shop.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: web
            port:
              number: 80
Read the code

The tls entry ties the host shop.example.com to the Secret shop-example-com-tls, and the rule below routes the same host; the host must appear in both places. Create the Secret with kubectl create secret tls NAME --cert=tls.crt --key=tls.key, or let cert-manager create and renew it from an annotation on the Ingress.

05

404 or 503: reading the controller's answers

An nginx-branded 404 page comes from the controller: no rule matched the Host header and path, or the Ingress uses a class this controller ignores. A plain 404 from the application, such as "404 page not found" from a Go server, means routing worked and the app does not serve that path, often because the prefix was not stripped. A 503 from the controller means a rule matched but its Service does not exist or has no ready endpoints. A 502 means the controller could not get a response from the pod, for example because of a wrong targetPort, and a 504 means the pod accepted the request but answered too slowly. The status code tells you what failed; the page body tells you who answered.

bash
curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: shop.local' http://localhost/api/orders
kubectl get ingress shop
kubectl get endpoints api web
Read the code

The first command prints only the status code, which is handy in scripts and loops. get ingress shows the class and the address the controller published; an empty ADDRESS usually means no controller has picked the Ingress up. get endpoints for both backends separates a 503 caused by no ready pods from one caused by a missing Service.

06

Try it in the lab terminal

Create a cluster with ingress-nginx, run gopherforge/web and an http-echo pod that plays the API, and route shop.local/ and shop.local/api through one Ingress. The lab's controller listens on localhost port 80, and curl -H 'Host: ...' chooses the virtual host, as you would against a real load balancer before DNS exists. Then send a request for a host no rule knows, and finally scale the API to zero to see the 503.

bash
cluster create --ingress
kubectl get ingressclass
kubectl create deployment web --image=gopherforge/web:v1 --replicas=2 --port=8080
kubectl expose deployment web --port=80 --target-port=8080
kubectl create deployment api --image=hashicorp/http-echo -- /http-echo -text=orders-api
kubectl expose deployment api --port=80 --target-port=5678
cat > shop-ingress.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: shop
spec:
  ingressClassName: nginx
  rules:
  - host: shop.local
    http:
      paths:
      - path: /api
        pathType: Prefix
        backend:
          service:
            name: api
            port:
              number: 80
      - path: /
        pathType: Prefix
        backend:
          service:
            name: web
            port:
              number: 80
EOF
kubectl apply -f shop-ingress.yaml
kubectl rollout status deploy/api
kubectl get ingress shop
curl -s -H 'Host: shop.local' http://localhost/
curl -s -H 'Host: shop.local' http://localhost/api/orders
curl -s -H 'Host: blog.local' http://localhost/
kubectl scale deploy/api --replicas=0
curl -s -H 'Host: shop.local' http://localhost/api/orders
kubectl get endpoints api
Read the code

get ingressclass shows the class nginx with controller k8s.io/ingress-nginx, and get ingress shop lists HOSTS shop.local and ADDRESS localhost. With Host: shop.local, / answers "gopherforge web v1 served by web-<hash>-<id>" and /api/orders answers "orders-api" from http-echo. Host: blog.local matches no rule and gets nginx's "404 Not Found" page. Scaling api to zero leaves the rule in place but empties its endpoints, so the same request now gets "503 Service Temporarily Unavailable" from nginx, and get endpoints api shows <none>.

TAKE THIS WITH YOU

An Ingress is routing data; the controller of its IngressClass does the work. The longest matching path wins, Prefix matches whole path elements, and the error page tells you which hop failed: an nginx 404 means no rule matched, an nginx 503 means the rule matched but no ready pods back it.

Check your understanding

After a deploy, curl -H "Host: shop.local" http://localhost/api returns an nginx-branded 503 page, while / on the same host still works. What is the most likely state of the cluster?

Go to the source

Progress stays in this browser. No account needed.