Is it possible to dynamically add hosts to ingress with Kubernetes?

2/4/2019

If you are managing ingress service such as in the following example, instead of updating the ingress file below, is there a means of adding an additional host/service such as echo3.example.com with out needing to apply an updated version of the original file?

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: echo-ingress
spec:
  rules:
  - host: echo1.example.com
    http:
      paths:
      - backend:
          serviceName: echo1
          servicePort: 80
  - host: echo2.example.com
    http:
      paths:
      - backend:
          serviceName: echo2
          servicePort: 80
# NEW HOST/SERVICE

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: echo-ingress
spec:
  rules:
  - host: echo3.example.com ### <= `echo3` addeded
    http:
      paths:
      - backend:
          serviceName: echo3
          servicePort: 80

Is there a way of applying this new host without needing to extend the old file?

-- denski
kubernetes
kubernetes-ingress

3 Answers

2/4/2019

You can make different ingress object with different annotation also.Make to two files of ingress with hosts and services.

-- Harsh Manvar
Source: StackOverflow

2/4/2019

I mean you can just make it a separate Ingress object/file, no need for it to be in the same one. Or you can use something like Kustomize.

-- coderanger
Source: StackOverflow

2/4/2019

If you apply the two files, the second one will overwrite the first one as they have the same name. So, you would need to edit the original every time you add a new rule.

One possible solution to avoid this problem would be to use Contour. In that case you could keep each IngressRoute in a separate resource and avoid conflicts like that.

In your case, you would have something like:

# ingressroute-echo1.yaml
apiVersion: contour.heptio.com/v1beta1
kind: IngressRoute
metadata:
  name: echo-ingress-1
spec:
  virtualhost:
    fqdn: echo1.example.com
  routes:
    - match: /
      services:
        - name: echo1
          port: 80

# ingressroute-echo2.yaml
apiVersion: contour.heptio.com/v1beta1
kind: IngressRoute
metadata:
  name: echo-ingress-2
spec:
  virtualhost:
    fqdn: echo2.example.com
  routes:
    - match: /
      services:
        - name: echo2
          port: 80

# ingressroute-echo3.yaml
apiVersion: contour.heptio.com/v1beta1
kind: IngressRoute
metadata:
  name: echo-ingress-3
spec:
  virtualhost:
    fqdn: echo3.example.com
  routes:
    - match: /
      services:
        - name: echo3
          port: 80
-- Pedreiro
Source: StackOverflow