skip to Main Content

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


  1. Chosen as BEST ANSWER

    As @jordanm suggested in the comment, I went with multiple Ingress objects on one IngressController, being sure I get rid of nginx.ingress.kubernetes.io/default-backend annotation:

    ---
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: ingress-x-host
      namespace: default
      annotations:
        kubernetes.io/ingress.class: nginx
    spec:
      rules:
      - host: x.example.com
        http:
          paths:
          - pathType: Prefix
            path: /
            backend:
              service:
                name: x
                port:
                  number: 80
    

    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. If metadata.name is the same for every object, then it just gets replaced as I apply the new configuration. This works perfectly.


  2. Hostname wildcards

    Hosts can be precise matches (for example “foo.bar.com”) or a wildcard (for example “*.foo.com”).

    Or use template system, such as helm or Kustomize

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search