Monday, January 30, 2023

Ingress to support multiple hosts

The Nginx ingress controller allows you to specify ingress in the helm chart.  For each ingress, you define the ingress rules in the corresponding yaml file.  You can specify each host and its rules. 

But to make the ingress dynamically apply the same rules for multiple hosts, you will need to specify the list of hosts from values.yaml file.  In this case, you are not allowed to pass a list of hosts as comma-separated hosts or other delimiter-separated hosts.  Even all hosts follow a pattern, the wild card is not supported.

To allow you to pass a list of hosts in values.yaml file, there is a way to do this.

In your values.yaml file, you pass the hosts like this: 

ingress:
  dnshosts:
    - host-1
    - host-2

 

  In your ingress yaml file, you use 'range' to iterate each host:

{{- range .Values.ingress.dnshosts }}
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress-service1-{{ . | quote | sha1sum | trunc 5}}
  namespace: {{ template "my.namespace" $ }}
  annotations:
    nginx.org/mergeable-ingress-type: "minion"
spec:
  ingressClassName: "nginx"
  rules:
  - host: {{ . | quote }}
    http:
      paths: 
      - path: /service1-endpoint
        pathType: Prefix
        backend:
          service:
            name: service1
            port:
              number: 80
{{- end }}


Reference: