Sub topic 9
The default backend
This chapter assumes you can write a Deployment and a ClusterIP Service, apply manifests with kubectl, and read HTTP status codes. It also assumes you know what an Ingress and an Ingress controller are. Here you will learn what happens to requests that match none of your routing rules, and how to control that.
What the default backend is
An Ingress routes a request by comparing its host and path to a list of rules. Some requests match no rule. A client may use an unknown hostname, or ask for a path you never declared. The default backend is the Service that receives those requests.
Without one, the controller answers by itself. Most controllers return a 404 page from a built-in handler. Setting a default backend lets you replace that page with your own application, such as a branded error page or a catch-all landing site.
The diagram below shows the decision a controller makes for each request.
Declaring a default backend
The spec.defaultBackend field takes the same service reference as a rule backend: a Service name and a port. This Ingress has no rules at all.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: with-default
spec:
ingressClassName: example-class
defaultBackend:
service:
name: web
port:
number: 80The Service web must exist in the same namespace as the Ingress. An Ingress can only point at Services in its own namespace.
A default backend can also sit beside rules. In that case it catches whatever the rules do not. Take the shop Ingress from the routing chapter, which has rules for shop.example.com. If you add defaultBackend, a request for other.example.com goes to the default backend instead of getting a 404. A request for shop.example.com still follows the rules, because the / Prefix rule there already matches every path on that host.
How the default backend behaves in practice
Several points are easy to get wrong.
- It applies to unmatched requests only. A request that matches a rule never reaches the default backend, even when the rule's Service is unhealthy. A broken rule backend gives you a 503, not a fallback.
- Rules without a host match every host. If one of your rules has no
hostfield, it matches any hostname. Requests then rarely fall through to the default backend, because that rule catches them first. - A rule-less Ingress is the usual way to set it. With no
rules, the Ingress exists only to name the default backend. - Controllers differ. Some controllers ship their own built-in default backend and treat the field as an override. Others may ignore it or combine it with an annotation. Check your controller's documentation.
- Several Ingresses may compete. Many controllers accept only one Ingress per class that sets a default backend. If two do, behavior depends on the controller. Some use the oldest, and some report a conflict.
Checking that it works
Confirm that the Service has endpoints, then send a request with a hostname that has no rule. The --resolve flag points an unknown name at your controller's address, which you can read from kubectl get ingress. Replace 203.0.113.10 with that address.
kubectl describe ingress with-default
kubectl get endpoints web
curl -i --resolve unknown.example.com:80:203.0.113.10 http://unknown.example.com/anythingIn the describe output, look for a line such as Default backend: web:80 followed by the Pod addresses. If the endpoints list is empty, the Service selector matches no ready Pods. Requests to the default backend will then fail with a 503.
Common mistakes
| Symptom | Likely cause |
|---|---|
| Unknown hosts still show the controller's 404 page | The controller ignores `defaultBackend`, or the Ingress has the wrong `ingressClassName` |
| 503 from the default backend | The Service has no ready endpoints, or the port does not match the Service |
| Default backend never receives traffic | A rule without a host or with `path: /` matches everything first |
| Ingress is rejected | The Service reference is missing a name or a port |
Exercise
An Ingress has one rule for host app.example.com with path / (Prefix) and a defaultBackend pointing at the Service fallback. A client requests http://app.example.com/missing. Which Service answers, and why? What changes for a request to http://other.example.com/?
Show answer
The rule's Service answers /missing, because the Prefix path / matches every path on app.example.com, so the request is matched and never falls through. The request to other.example.com matches no rule, so it goes to the fallback Service.