Variables in ingress host carried over to service name

8/16/2019

My current ingress looks something like

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-ingress
spec:
  rules:
  - host: web1.dev.cloud
    http:
      paths:
      - path: /
        backend:
          serviceName: web1
          servicePort: 8080

Meaning that the first part of the host will always match the serviceName. So for every web pod I would need to repeat the above like:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-ingress
spec:
  rules:
  - host: web1.dev.cloud
    http:
      paths:
      - path: /
        backend:
          serviceName: web1
          servicePort: 8080
  - host: web2.dev.cloud
    http:
      paths:
      - path: /
        backend:
          serviceName: web2
          servicePort: 8080

I was just wondering if there is some support for doing the following:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-ingress
spec:
  rules:
  - host: $1.dev.cloud
    http:
      paths:
      - path: /
        backend:
          serviceName: $1
          servicePort: 8080
-- panpan
amazon-web-services
kubernetes

2 Answers

8/16/2019

This is not possible if you use kubectl to deploy your kubernetes manifests. However if you write a helm chart for your application it is possible. Helm uses a packaging format called charts. A chart is a collection of files that describe a related set of Kubernetes resources in the form for templates. There in the inress.yaml template you can write such config using range block and putting the variable values in values.yaml

In your case it will look something like below

spec:
  rules:
  {{- range .Values.ingress.hosts }}
  - host: {{ .name }}.dev.cloud
    http:
      paths:
      - path: {{ default "/" .path | quote }}
        backend:
          serviceName: {{ .name }}
          servicePort: 8080
  {{- end }}

and the values.yaml will have

ingress:
      hosts:
        - name: abc
        - name: xyz
-- RAMNEEK GUPTA
Source: StackOverflow

8/19/2019

Thanks to RAMNEEK GUPTA post, you have solution how it can be automated.

According to the documentation:

Regular expressions and wild cards are not supported in the spec.rules.host field. Full hostnames must be used.

So please try as in your example:

1. Request based on the HTTP URI being requested "Simple fanout"

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: fanout
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: dev.com
    http:
      paths:
      - path: /web1
        backend:
          serviceName: web1
          servicePort: 8080
      - path: /web2
        backend:
          serviceName: web2
          servicePort: 8080

2. Requests based on the Host header "Named based virtual hosting"

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: Named
spec:
  rules:
  - host: web1.dev.com
    http:
      paths:
      - backend:
          serviceName: web1
          servicePort: 8080
  - host: web2.dev.com
    http:
      paths:
      - backend:
          serviceName: web2
          servicePort: 8080
-- Hanx
Source: StackOverflow