1

I want to use my hostname as test.com in first namespace and example.com/demo in another namespace. However i am unable to do that since (in my opinion) the nginx-ingress controller always points to the first website (example.com). My nginx-ingress controller is running in the default namespace

#namespace prod
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
  name: ingress
  namespace: production
    annotations:
    nginx.ingress.kubernetes.io/proxy-body-size: 20m
spec:
  rules:
    - host: example.com
      http:
        paths:
          - path: /static
            backend:
              serviceName: app-svc
              servicePort: 80
          - path: /
            backend:
              serviceName: app-svc
              servicePort: 8000
  tls:
  - hosts:
    - example.com
    secretName: cert
status:
  loadBalancer:
    ingress:
      - ip: xx.xxx.xx.xxx


#namespace dev
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
  name: test-ingress
  namespace: dev-env
    annotations:
    nginx.ingress.kubernetes.io/proxy-body-size: 20m
spec:
  rules:
    - host: example.com
      http:
        paths:
          - path: /static
            backend:
              serviceName: app-svc
              servicePort: 80
          - path: /demo
            backend:
              serviceName: app-svc
              servicePort: 8000
  tls:
  - hosts:
    - example.com
    secretName: cert
status:
  loadBalancer:
    ingress:
      - ip: xx.xxx.xx.xxx
 
 
devcloud
  • 391
  • 5
  • 18
  • Can you share your ingress yaml file? – Bimal Sep 22 '20 at 15:47
  • 1
    @Bimal I have deployed separate ingress for each namespace. i just posted my ingress.yamls for both namespaces – devcloud Sep 22 '20 at 15:54
  • https://stackoverflow.com/questions/64005515/kubernetes-having-same-host-name-but-different-paths-in-ingresses-in-different The different answers here. – B P Apr 19 '22 at 08:33

1 Answers1

2

You can use the same hostname/domain, but in order to access distinct backend services, you have to define distinct paths, in your case you can do something like this:

#namespace prod
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
  name: test-ingress
  namespace: prod-env
    annotations:
    nginx.ingress.kubernetes.io/proxy-body-size: 20m
spec:
  rules:
    - host: example.com
      http:
        paths:
          - path: /prod/static
            backend:
              serviceName: app-svc
              servicePort: 80
          - path: /prod/
            backend:
              serviceName: app-svc
              servicePort: 8000
  tls:
  - hosts:
    - example.com
    secretName: selfsigned-cert
status:
  loadBalancer:
    ingress:
      - ip: 80.180.31.153


#namespace dev
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
  name: test-ingress
  namespace: dev-env
    annotations:
    nginx.ingress.kubernetes.io/proxy-body-size: 20m
spec:
  rules:
    - host: example.com
      http:
        paths:
          - path: /dev/static
            backend:
              serviceName: app-svc
              servicePort: 80
          - path: /demo
            backend:
              serviceName: app-svc
              servicePort: 8000
  tls:
  - hosts:
    - example.com
    secretName: selfsigned-cert
status:
  loadBalancer:
    ingress:
      - ip: 80.180.31.153

Like this you have:

example.com/prod/static -> points to app-svc:80 on prod-env namespace  
example.com/prod/ -> points to app-svc:8000 on prod-env namespace  

example.com/dev/static -> points to app-svc:8000 on dev-env namespace  
example.com/demo -> points to app-svc:8000 on dev-env namespace 

If you need to preserve the url after host, you should add a subdomain for each namespace:

dev.example.com/static -> points to app-svc:80 on prod-env namespace  
prod.example.com/static -> points to app-svc:8000 on dev-env namespace  

In order for this one to work you have to have defined this as an A record in your domain DNS administration, pointing at the same IP as your loadBalancer (80.180.31.153 in this case):

example.com A -> 80.180.31.153 (optional)   
dev.exmaple.com A -> 80.180.31.153  
prod.example.com A -> 80.180.31.153  

or the easiest one:

*.example.com A -> 80.180.31.153  
Luis Gonzalez
  • 531
  • 5
  • 16
  • So if I do not want preserve URL after host , then the first setting should be enough right? with paths as /prod/static and /prod ? – devcloud Sep 22 '20 at 16:26
  • 1
    That's correct, if you define more than one ingress with the same path, "If the same path for the same host is defined in more than one Ingress, the oldest rule wins." – Luis Gonzalez Sep 22 '20 at 16:36
  • 1
    you may find useful this link: https://kubernetes.github.io/ingress-nginx/how-it-works/ – Luis Gonzalez Sep 22 '20 at 16:37
  • i tried doing procedure you mentioned but i am stil being redirected to normal example.com everyime and if i type example.com/demo, i get "Not Found: The requested resource was not found on this server" error. – devcloud Sep 23 '20 at 08:19
  • i am deploying a django app, i think i should also configure every distinct paths in every application's settings.py? – devcloud Sep 23 '20 at 08:23
  • 1
    that's correct, that's why is easiest just to use subdomains – Luis Gonzalez Sep 23 '20 at 18:57
  • i defined new paths like /dev/ and /prod/ but i don't know how to change the path for static from /static to /dev/static for example :( I am using /dev and /prod for port 8000 but same path for static i.e. /static in both. So now even my /prod gets redirected to /dev. I assume not having different static paths is the problem now? – devcloud Sep 24 '20 at 07:45
  • That cloud be a problem with your container, are you running nginx? – Luis Gonzalez Oct 01 '20 at 15:06