Sub topic 10
Controller-specific annotations and portability
This chapter assumes you can write and apply Ingress manifests, know what an Ingress controller is, and understand how IngressClass selects one. It covers how annotations extend Ingress, and where that extension stops being portable.
Why annotations exist
The Ingress spec covers a small set of features: hosts, paths, path types, TLS and a default backend. Real traffic needs more. You may want to rewrite a URL, raise the maximum request body size, set a timeout, redirect HTTP to HTTPS, or enable basic authentication.
The Ingress API has no fields for these. Controllers fill the gap with annotations. An annotation is a key-value pair in metadata.annotations. Kubernetes stores it but does not interpret it. Only the controller that reads it gives it meaning.
A worked example
This Ingress uses annotations understood by the community ingress-nginx controller. It rewrites the request path and raises the body size limit.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: shop-nginx
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
nginx.ingress.kubernetes.io/proxy-body-size: "20m"
spec:
ingressClassName: nginx
rules:
- host: shop.example.com
http:
paths:
- path: /api(/|$)(.*)
pathType: ImplementationSpecific
backend:
service:
name: api
port:
number: 80With this manifest, a request for /api/orders reaches the api Service as /orders. The $2 refers to the second capture group in the path regular expression. The ImplementationSpecific path type is used because the path is a regular expression, and the matching rules for that type belong to the controller.
Annotation values are always strings. Write "20m" and "true" with quotes. An unquoted true or number is a YAML boolean or integer, and the API server rejects it.
How the controller treats annotations
The prefix in the key tells you who owns it. nginx.ingress.kubernetes.io/ belongs to ingress-nginx. Other controllers use their own prefixes, such as traefik.ingress.kubernetes.io/ or alb.ingress.kubernetes.io/.
Three behaviors cause most problems:
- A controller ignores annotations with prefixes it does not know. It gives no error and no warning.
- A misspelled key looks the same as an unknown key, so it silently does nothing.
- Annotation names and accepted values change between controller versions. Some controllers also restrict risky annotations, such as those that inject raw configuration snippets, and disable them by default.
Portability limits
The core fields are portable. A manifest that uses only ingressClassName, rules, tls and defaultBackend should work on any conforming controller, though details such as ImplementationSpecific matching still vary.
Annotations are not portable. If you move from ingress-nginx to another controller:
- The new controller ignores your old annotations.
- Redirects, rewrites and timeouts stop working, often without any error.
- You must find the equivalent setting in the new controller, which may be an annotation, a custom resource or a controller-wide setting.
Because of this, treat annotations as part of your controller choice. Keep them in version control next to the manifest and note which controller they target.
Reducing annotation lock-in
You can limit the damage in a few ways:
- Prefer standard fields when they exist. Use the
tlssection instead of an annotation that forces HTTPS settings, and usepathTypeinstead of a regex annotation where a prefix is enough. - Set cluster-wide behavior in the controller's own configuration, not on every Ingress.
- Keep controller-specific configuration in one place, so a migration has a short list of things to translate.
- Consider the Gateway API for features such as header-based routing and traffic splitting. It expresses many of these as typed fields, so they are validated and portable.
Checking that an annotation took effect
Annotations are visible on the object, and the controller often reports problems in events and logs.
kubectl get ingress shop-nginx -o yaml
kubectl describe ingress shop-nginxLook at the Events section of the describe output for rejected configuration. If nothing appears, read the controller's logs, then send a real request and inspect the response.
Exercise
You migrate an Ingress from ingress-nginx to another controller. kubectl apply succeeds, but HTTP requests no longer redirect to HTTPS and the controller logs show no errors. What is the most likely cause, and what should you do?
Show answer
The Ingress still carries nginx.ingress.kubernetes.io/ annotations, which the new controller silently ignores. Find the equivalent setting in the new controller's documentation, such as its own annotation, a custom resource or a global option, and apply it. Then verify with a test request.
Exercise
Why does nginx.ingress.kubernetes.io/proxy-body-size: 20m fail to apply, and how do you fix it?
Show answer
It fails only if the value is not a string, but here 20m is a valid YAML string, so the unquoted form works. The failure appears with values such as true or 20, which YAML parses as a boolean or integer. Quote them, as in "true" and "20", because annotation values must be strings.