Sub topic 3
Anatomy of an Ingress resource
This chapter assumes you can write and apply YAML with kubectl. It also assumes you know how a ClusterIP Service selects Pods and how HTTP hosts and paths work. Here you will read an Ingress manifest field by field and learn which parts are required.
What an Ingress is
An Ingress is a namespaced API object in the networking.k8s.io/v1 group. It is a declaration of routing rules: "requests for this host and path go to that Service." It does not move any traffic itself. An Ingress controller reads the object and configures a proxy to follow it. Controllers are covered in the next chapter.
The Services named by an Ingress are ordinary ClusterIP Services, such as this one:
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 80
targetPort: 8080A complete example
This Ingress sends /api to the api Service and everything else to web.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: shop
spec:
ingressClassName: example-class
rules:
- host: shop.example.com
http:
paths:
- path: /api
pathType: Prefix
backend:
service:
name: api
port:
number: 80
- path: /
pathType: Prefix
backend:
service:
name: web
port:
number: 80The fields, from the top
The spec section has four main parts.
| Field | Purpose |
|---|---|
| `ingressClassName` | Names the IngressClass, which selects the controller that handles this Ingress. |
| `rules` | A list of host and path matches, each with a backend. |
| `tls` | Hostnames and the Secret holding their certificate. Covered in a later chapter. |
| `defaultBackend` | Receives requests that match no rule. |
Rules
Each entry in rules has an optional host and an http block with a list of paths. If you omit host, the rule applies to requests for any host. A path entry has three parts:
path: the URL path to match.pathType: how to match it. The values areExact,Prefix, andImplementationSpecific. This field is required, and the API server rejects a path without it.backend: where to send the request.
Backends
In the networking.k8s.io/v1 API, a backend is normally a service with a name and a port. The port is given either as number or as name, not both. The number is the Service's port, which is 80 above. It is not the targetPort of the Pods. The Service must be in the same namespace as the Ingress, because an Ingress cannot reference a Service in another namespace.
How a request flows
The Ingress object is only configuration. The traffic path goes through the controller.
The solid arrows show where traffic flows. The dotted arrow shows configuration, which the controller reads from the API server.
Inspecting an Ingress
After the controller accepts an Ingress, it writes an address into the object's status.loadBalancer.ingress field. You can check it with:
kubectl get ingress shop
kubectl describe ingress shopdescribe shows each rule and the backend it resolved to. It also lists the Pod IPs behind each backend Service. If a backend shows <error: endpoints "api" not found>, the Service does not exist or has no ready Pods.
Common mistakes in the manifest
- Leaving out
pathType, which fails validation. - Using the Pod's
targetPort(8080) inbackend.service.portinstead of the Service port (80). - Referencing a Service that lives in a different namespace.
- Omitting
ingressClassNameon a cluster with several controllers, so that no controller, or the wrong one, picks up the Ingress. - Setting both
numberandnamefor a backend port.
Exercise
In the shop Ingress, the web Service has port: 80 and targetPort: 8080. A teammate changes the backend to number: 8080. What happens, and why?
Show answer
Requests to / fail, typically with a 503 from the controller. The Ingress backend port must match a port exposed by the Service, which is 80. The targetPort 8080 belongs to the Pods and is not a Service port, so the controller cannot resolve the backend. Setting the number back to 80 fixes it.