Sub topic 5
Choosing a controller with IngressClass
This chapter assumes you can write and apply YAML manifests with kubectl. It also assumes you know what a ClusterIP Service is and the difference between an Ingress resource and an Ingress controller, which earlier chapters covered.
The problem IngressClass solves
An Ingress resource does nothing on its own. A controller watches Ingress objects and configures a proxy to match them. A cluster can run more than one controller, for example one for public traffic and one for internal traffic. Each Ingress therefore needs a way to say which controller should handle it. IngressClass is that mechanism.
The IngressClass resource
An IngressClass is a cluster-scoped object. It has no namespace. Its spec.controller field holds a string that identifies the controller implementation. Each controller documents the string it looks for.
apiVersion: networking.k8s.io/v1
kind: IngressClass
metadata:
name: example-class
spec:
controller: example.com/ingress-controllerThe name example-class is what Ingress objects refer to. The controller value is what the controller matches against. Many controllers create their own IngressClass when you install them, so check what already exists before writing one.
kubectl get ingressclassSelecting a class from an Ingress
An Ingress picks its class with spec.ingressClassName. The value must match the metadata.name of an IngressClass.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: with-default
spec:
ingressClassName: example-class
defaultBackend:
service:
name: web
port:
number: 80The controller that owns example-class reconciles this Ingress. Controllers that own other classes ignore it.
How the pieces connect
The default class
You can mark one IngressClass as the cluster default with an annotation on the IngressClass itself:
apiVersion: networking.k8s.io/v1
kind: IngressClass
metadata:
name: example-class
annotations:
ingressclass.kubernetes.io/is-default-class: "true"
spec:
controller: example.com/ingress-controllerAn Ingress that omits ingressClassName is then assigned to that class. Without a default, such an Ingress is not claimed by any controller, and some controllers may adopt it on their own terms. Mark at most one class as default. If several are marked, the API server rejects new Ingresses that have no class.
Common errors
| Symptom | Likely cause | Check |
|---|---|---|
| The `ADDRESS` column stays empty | No controller owns the class, or the class name is misspelled | `kubectl get ingress` and `kubectl get ingressclass` |
| Ingress works in one cluster but not another | The class name differs between clusters | Compare `ingressClassName` with the output of `kubectl get ingressclass` |
| Two controllers fight over one Ingress | Legacy annotation and `ingressClassName` disagree, or both controllers watch all classes | `kubectl describe ingress` and the controller flags |
| New Ingress is rejected | More than one default class | Look for the default annotation on each class |
A typo in ingressClassName produces no error. Kubernetes accepts the object, and no controller picks it up. The Ingress sits there without an address. Always verify after applying.
kubectl get ingress shop
kubectl describe ingress shop
kubectl get ingressclassIn the describe output, look at the Ingress Class field and the Events section. A controller that accepted the Ingress usually records an event such as a sync message. No events at all often means no controller matched.
Exercise
You apply an Ingress with ingressClassName: nginx-internal. It is accepted without error, but kubectl get ingress shows an empty ADDRESS column after several minutes. kubectl get ingressclass lists only nginx. What is the most likely cause, and how do you fix it?
Show answer
No IngressClass named nginx-internal exists, so no controller claims the Ingress. Kubernetes does not validate the class name when you create the Ingress. Fix it by changing ingressClassName to nginx, or by creating an IngressClass named nginx-internal whose spec.controller matches a running controller.