I am trying to automate the hosts in Ingress Controller and I’m facing the problem of generating many hosts into one file. What I mean is, I have this ingress.yaml:
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-host
namespace: default
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/default-backend: a
spec:
rules:
- host: a.example.com
http:
paths:
- pathType: Prefix
path: /
backend:
service:
name: a
port:
number: 80
- host: b.example.com
http:
paths:
- pathType: Prefix
path: /
backend:
service:
name: b
port:
number: 80
...
- host: x.example.com
http:
paths:
- pathType: Prefix
path: /
backend:
service:
name: x
port:
number: 80
...
in this example I have multiple instances: a
, b
, all the way to x
and I anticipate a lot more. Right now I am programmatically regenerating the whole ingress.yaml to add/remove certain hosts. This is prone to errors and hard to maintain, as I must constantly be aware about ingress.yaml
to be broken for one reason or another.
What would really help me is to put every host
into a separate file and (maybe) just tell ingress.yaml
to scan the whole directory where those files are to be stored. This way, I can just add/remove a single file and reload Ingress
Is there an option for that? I found somewhere that IngressSpec
could be somehow defined, but I do not see any usefull link with a valid example. Maybe someone found a solution to that already and can point me to the right direction?
2
Answers
As @jordanm suggested in the comment, I went with multiple Ingress objects on one
IngressController
, being sure I get rid ofnginx.ingress.kubernetes.io/default-backend
annotation:I generate a unique file for each of hosts, replacing
x
with my unique name.I also have to make sure that
metadata.name
is unique. Ifmetadata.name
is the same for every object, then it just gets replaced as Iapply
the new configuration. This works perfectly.Hostname wildcards
Or use template system, such as helm or Kustomize