TL;DR
You can split your Ingress
resource on multiple objects (which will work together) to add Annotations
to only specific hosts
.
Annotations
can only be set on the whole kubernetes resource, as they are part of the resource metadata
. The ingress spec
doesn't include that functionality at a lower level.
-- Stackoverflow.com: Questions: Apply nginx-ingress annotations at path level
Extending on the answer to give an example of how such setup could be created. Let's assume (example):
- All required domains pointing to the
Service
of type LoadBalancer
of nginx-ingress-controller
:
hello.kubernetes.docker.internal
- used in host
.spec
hello-two.kubernetes.docker.internal
- used in annotations
.metadata
- --
goodbye.kubernetes.docker.internal
- used in host
.spec
goodbye-two.kubernetes.docker.internal
- used in annotations
.metadata
Skipping the Deployment
and Service
definitions, the Ingress
resources should look like below:
hello-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: hello-ingress
annotations:
nginx.ingress.kubernetes.io/server-alias: "hello-two.kubernetes.docker.internal"
spec:
rules:
- host: hello.kubernetes.docker.internal # <-- IMPORTANT
http:
paths:
- path: /
backend:
service:
name: hello-service
port:
number: 80
pathType: Prefix
goodbye-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: goodbye-ingress
annotations:
nginx.ingress.kubernetes.io/server-alias: "goodbye-two.kubernetes.docker.internal"
spec:
rules:
- host: goodbye.kubernetes.docker.internal # <-- IMPORTANT
http:
paths:
- path: /
backend:
service:
name: goodbye-service
port:
number: 80
pathType: Prefix
Above definitions will create 2 Ingress
resources that will be merged:
hello-service
will respond for:
hello.kubernetes.docker.internal
hello-two.kubernetes.docker.internal
goodbye-service
will respond for:
goodbye.kubernetes.docker.internal
goodbye-two.kubernetes.docker.internal
Running:
NAME CLASS HOSTS ADDRESS PORTS AGE
goodbye-ingress <none> goodbye.kubernetes.docker.internal localhost 80 36m
hello-ingress <none> hello.kubernetes.docker.internal localhost 80 36m
Additional resources: