简体   繁体   中英

Hosting two web apps with nginx ingress in Kubernetes

I have a kubernetes cluster running in Azure Kubernetes Service (AKS). I've been following a series of workshops and I've set up an NGINX ingress controller. Right now I'm using nip.io in order to access my site. They had me create an ingress resource to expose the front end:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ratings-web-ingress
  annotations: 
    kubernetes.io/ingress.class: nginx
  spec:
    rules:
    - host: frontend.<redacted ip>.nip.io
    http:
      paths:
      - backend:
        serviceName: ratings-web
        servicePort: 80
      path: /

This works fine. I can brows the nip.io address and everything works right. I wanted to extend this and create a separate site. I want it at the same address, just served at /foo

I tried to create an ingress resource:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: foo-web-ingress
  annotations: 
    kubernetes.io/ingress.class: nginx
  spec:
    rules:
    - host: frontend.<redacted ip>.nip.io
    http:
      paths:
      - backend:
        serviceName: foo-web
        servicePort: 80
      path: /foo

When I browse frontend.<redacted ip>.nip.io/foo I get a 404 not found from nginx. I tried added the following annotation based on another SO post:

nginx.ingress.kubernetes.io/rewrite-target: /

Now when I browse /foo it responds, but all the content is blank. In the web app it's referencing everything at the root level like:

<link href="/assets/css/bootstrap.min.css" rel="stylesheet" type="text/css" />

When it sees this path, I think it's going to the original website so I get strict MIME type errors or 404 errors.

What do I need to do to host two websites on the same nginx ingress controller at different paths?

Ingress controller will be confused when merging these two ingresses, what the correct order should be because more than one ingress resource is defined for same host.

Referring from docs you could use a single ingress resource as fanout.

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: simple-fanout-example
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    kubernetes.io/ingress.class: nginx
spec:
  rules:
  - host: <redacted ip>.nip.io
    http:
      paths:
      - path: /foo
        backend:
          serviceName: foo-web
          servicePort: 80
      - path: /bar
        backend:
          serviceName: bar-web
          servicePort: 80

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM