简体   繁体   English

在 Kubernetes 中使用 nginx 入口托管两个 web 应用程序

[英]Hosting two web apps with nginx ingress in Kubernetes

I have a kubernetes cluster running in Azure Kubernetes Service (AKS).我有一个 kubernetes 集群在 Azure Kubernetes 服务 (AKS) 中运行。 I've been following a series of workshops and I've set up an NGINX ingress controller.我一直在关注一系列研讨会,并设置了 NGINX 入口 controller。 Right now I'm using nip.io in order to access my site.现在我正在使用 nip.io 来访问我的网站。 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.我可以浏览 nip.io 地址,一切正常。 I wanted to extend this and create a separate site.我想扩展它并创建一个单独的站点。 I want it at the same address, just served at /foo我想要它在同一个地址,只是在/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.当我浏览frontend.<redacted ip>.nip.io/foo我从 nginx 中找不到 404。 I tried added the following annotation based on another SO post:我尝试根据另一个 SO 帖子添加以下注释:

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

Now when I browse /foo it responds, but all the content is blank.现在,当我浏览/foo时,它会响应,但所有内容都是空白的。 In the web app it's referencing everything at the root level like:在 web 应用程序中,它引用了根级别的所有内容,例如:

<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.当它看到这条路径时,我认为它会转到原始网站,所以我得到严格的 MIME 类型错误或 404 错误。

What do I need to do to host two websites on the same nginx ingress controller at different paths?我需要做什么才能在不同路径的同一 nginx 入口 controller 上托管两个网站?

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. Ingress controller 在合并这两个入口时会感到困惑,正确的顺序应该是什么,因为为同一主机定义了多个入口资源。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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