Pablo Alejandro
Pablo Alejandro

Reputation: 597

How to create subdomains automatically with Nginx ingress

We're searching a way to use subdomains in a master-minion nginx ingress implementation. We've tried a lot of different approaches but we haven't got it. The documentation example works fine (https://github.com/nginxinc/kubernetes-ingress/tree/v1.8.1/examples/mergeable-ingress-types), but this example is with paths. Is there anyway to do it with subdomains or it's not possible?

I will have a different subdomain like the wordpress structure every time you create a new page, I would like to know what is the best way to do it.

Thank you very much!

Upvotes: 1

Views: 6333

Answers (2)

Jakub
Jakub

Reputation: 8830

@paltaa already showed one of the paths that you can choose, with 2 differents hosts.


As far as I understand by automatically you want a wildcard hosts, there is github issue about that.

Kubernetes 1.18 has been released with some enhancements, like wildcard in hostname and better handling of Path.

Quoted from kubernetes documentation.

Hosts can be precise matches (for example “foo.bar.com”) or a wildcard (for example “*.foo.com”). Precise matches require that the HTTP host header matches the host field. Wildcard matches require the HTTP host header is equal to the suffix of the wildcard rule.

Host        Host header       Match?    
*.foo.com   bar.foo.com       Matches based on shared suffix    
*.foo.com   baz.bar.foo.com   No match, wildcard only covers a single DNS label  
*.foo.com   foo.com No match, wildcard only covers a single DNS label

And there are 2 examples.


apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-wildcard-host
spec:
  rules:
  - host: "foo.bar.com"
    http:
      paths:
      - pathType: Prefix
        path: "/bar"
        backend:
          service:
            name: service1
            port:
              number: 80
  - host: "*.foo.com"
    http:
      paths:
      - pathType: Prefix
        path: "/foo"
        backend:
          service:
            name: service2
            port:
              number: 80

apiVersion: "networking.k8s.io/v1beta1"
kind: "Ingress"
metadata:
  name: "example-ingress"
spec:
  rules:
  - host: "*.example.com"
    http:
      paths:
      - path: "/example"
        pathType: "Prefix"
        backend:
          serviceName: "example-service"
          servicePort: 80

Upvotes: 0

paltaa
paltaa

Reputation: 3244

The Nginx ingress controller documentation (https://kubernetes.github.io/ingress-nginx/user-guide/basic-usage/) got a pretty simple example working with different hosts:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-myservicea
  annotations:
    # use the shared ingress-nginx
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: myservicea.foo.org
    http:
      paths:
      - path: /
        backend:
          serviceName: myservicea
          servicePort: 80
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-myserviceb
  annotations:
    # use the shared ingress-nginx
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: myserviceb.foo.org
    http:
      paths:
      - path: /
        backend:
          serviceName: myserviceb
          servicePort: 80

What have you tried or trying to achieve that is not working ?

Upvotes: 5

Related Questions