简体   繁体   English

NGINX Ingress 到 Microk8s 裸机集群未按预期工作

[英]NGINX Ingress to Microk8s Bare metal cluster not working as expected

First a little background:首先介绍一下背景:

We currently have several websites and services hosted on a Plesk server and I am setting up a bare-metal development server to provide an area where we can test updates, etc. before going to production.我们目前有几个网站和服务托管在 Plesk 服务器上,我正在设置一个裸机开发服务器,以提供一个区域,我们可以在投入生产之前测试更新等。 I am using a 3 node kubernetes cluster running microk8s on Ubunutu 20.04.01.我正在使用在 Ubunutu 20.04.01 上运行 microk8s 的 3 节点 kubernetes 集群。 The services we host are pretty diverse: we have a couple Moodle sites, a few Wordpress sites, a site running limesurvery, an instance of Mantis bugtracker, and a few more.我们托管的服务非常多样化:我们有几个 Moodle 站点、几个 Wordpress 站点、一个运行 limsurvery 的站点、一个 Mantis bugtracker 实例,等等。 I have successfully gotten most of the sites containerized and running on k8s.我已经成功地将大多数站点容器化并在 k8s 上运行。 I can also access each individual site either through a NodePort or a MetalLB load balancer.我还可以通过 NodePort 或 MetalLB 负载均衡器访问每个单独的站点。

However, I'd really like to use the NGINX Ingress Controller on top of the load balancer so that I can have a consistent way to access the sites without using a bunch of IP addresses (or in the NodePort case, ports that change).但是,我真的很想在负载均衡器之上使用 NGINX 入口控制器,这样我就可以以一致的方式访问站点,而无需使用一堆 IP 地址(或者在 NodePort 的情况下,端口会发生变化)。 No matter what I've done, I cannot seem to get the Ingress to do what I want.无论我做了什么,我似乎都无法让 Ingress 做我想做的事。 I simply want to do the following: http://<LB IP Address>/bugtracker to access the Mantis Bug Tracker site http://<LB IP Address>/moodle1 to access one of the Moodle sites http://<LB IP Address>/limesurvey to access the limesurvey, etc. I seem to be able to get to the main page of the site (eg index.html, index.php, etc.), but any references from there on do not work - ie they give path not found errors or 404 errors.我只想执行以下操作: http://<LB IP Address>/bugtracker访问 Mantis Bug Tracker 站点http://<LB IP Address>/moodle1访问 Moodle 站点之一http://<LB IP Address>/limesurvey以访问limesurvey 等。我似乎能够访问该站点的主页(例如index.html、index.php 等),但是从那里开始的任何引用都不起作用-即他们给出路径未找到错误或 404 错误。

Here's a sample of my Ingress file:这是我的 Ingress 文件的示例:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
  - http:
      paths:
      - path: /limesurvey(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: limesurvey-svc
            port:
              number: 80
      - path: /moodle(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: moodle-svc
            port:
              number: 8080

This ingress does not work (I get the 404s).这个入口不起作用(我得到了 404)。 However, if I only have one path in the file and just use '/' it works (but I can only use it for one service):但是,如果我在文件中只有一个路径并且只使用 '/' 它可以工作(但我只能将它用于一项服务):

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: limesurvey-svc
            port:
              number: 80

I think what I need is for the path (limesurvey) to continue to be pre-pended onto each URL request, but I cannot seem to make this happen.我认为我需要的是让路径 (limesurvey) 继续预先添加到每个 URL 请求中,但我似乎无法做到这一点。 The reason I think this is because when I navigate to http://<LB IP Address>/limesurvey and click on a survey, I get a 404 error at http://<LB IP Address>/index.php/<survey number> .我认为这是因为当我导航到http://<LB IP Address>/limesurvey并单击调查时,我在http://<LB IP Address>/index.php/<survey number>收到 404 错误http://<LB IP Address>/index.php/<survey number> . However, if I manually change the URL in my browser to http://<LB IP Address>/limesurvey/index.php/<survey number> it will access the survey (but still have 404s with supporting assets).但是,如果我手动将浏览器中的 URL 更改为http://<LB IP Address>/limesurvey/index.php/<survey number> ,它将访问调查(但仍然有 404 和支持资产)。

Am I trying to do something outside of what the Ingress controller was designed for?我是否试图做一些 Ingress 控制器的设计目的之外的事情? I feel like I should be able to use the rewrite-target to accomplish this but I'm missing something critical.我觉得我应该能够使用 rewrite-target 来完成这个,但我错过了一些关键的东西。

I appreciate any help.我很感激任何帮助。

Some applications relay on static content served from different URL webserver locations and moreover do internal path routing (eg you hit "/" path but get served "/admin" section immediately).一些应用程序中继从不同URL webserver位置提供的static content ,此外还进行内部path routing (例如,您点击"/"路径但立即获得"/admin"部分的服务)。

In such cases creation of right Ingress rules gets more tricky, and requires you to better understand behavior and constructs of your web application, to predict all possible URL path locations that user may visit (these forced by app internal redirects too), and these sourced by html code as well.在这种情况下,正确的Ingress规则的创建会变得更加棘手,并且需要您更好地理解 Web 应用程序的行为和构造,预测用户可能访问的所有可能的 URL 路径位置(这些也是由应用程序内部重定向强制的),以及这些来源也可以通过html代码。

Seems like your case with limesurvey app falls into that category:似乎您使用limesurvey应用程序的情况属于该类别:

Why do I think that?我为什么这么认为?
Just try to open limesurvey public demo ( https://demo.limesurvey.org/ ) and inspect site content.只需尝试打开limesurvey 公共演示( https://demo.limesurvey.org/ )并检查站点内容。

You will learn that main page is using a lot of static files (eg css , javascrpt files ), referenced from absolute path starting with: /tmp/assets/...您将了解到主页使用了大量静态文件(例如cssjavascrpt files ), javascrpt files从以/tmp/assets/...开头的绝对路径引用/tmp/assets/...

<link rel="stylesheet" type="text/css" href="/tmp/assets/2d523ae6/survey.css" />

of course variants of different path locations can be matched with single smart reg-ex pattern, to avoid creation of dozen of individual ingress rules (what you tried).当然,不同路径位置的变体可以与单个智能reg-ex表达式模式匹配,以避免创建数十个单独的入口规则(您尝试过的)。

What's the issue?有什么问题?

rules:
  - http:
      paths:
      - path: /limesurvey(/|$)(.*) <---- it won't match "/tmp/assets/..." location
        pathType: Prefix
        backend:
          service:
            name: limesurvey-svc
            port:
              number: 80

Please try to create additional Ingress rule to support static file location (watch out, I'm using old syntax of Ingress resource, adjust it to your needs):请尝试创建额外的Ingress规则以支持静态文件位置(注意,我使用的是 Ingress 资源的旧语法,请根据您的需要进行调整):

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$1
  name: ingress-limesurvey-static
spec:
  rules:
  - http:
      paths:
      - backend:
          serviceName: limesurvey-svc
          servicePort: 80
        path: /?(.*)

Best Solution (in my opinion)最佳解决方案(在我看来)

You should define custom public URL within your application directly.您应该直接在您的应用程序中定义自定义公共 URL。 Detailed information can be found in Advanced path settings , see publicurl option.详细信息可以在高级路径设置中找到,请参阅publicurl选项。

This way you wouldn't need to define internal reference for static files, however it should be done during installation.这样你就不需要为静态文件定义内部引用,但是它应该在安装过程中完成。

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

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