简体   繁体   English

如何在单个域上为 React app + Express 设置 k8s ingress?

[英]How do you setup k8s ingress for React app + Express on single domain?

I have a frontend application built with React and backend on nodejs.我有一个用 React 构建的前端应用程序和在 nodejs 上构建的后端。 Both have a separate Docker image and therefore a separate deployment on k8s (gce).两者都有一个单独的 Docker 映像,因此在 k8s (gce) 上有一个单独的部署。

Each deployment has a corresponding k8s service, let's say fe-serice and be-service .每个部署都有对应的 k8s 服务,比方说fe-sericebe-service

I am trying to setup an Ingress so that both services are exposed on a single domain in the following manner:我正在尝试设置一个 Ingress,以便通过以下方式在单个域中公开这两个服务:

  • /api/* - are routed to be-service /api/* - 路由到be-service
  • everything else is routed to fe-service其他一切都路由到fe-service

Here is my yaml file:这是我的 yaml 文件:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: my-host
    http:
      paths:
      - path: /*
        backend:
          serviceName: fe-service
          servicePort: 80
      - path: /api/*
        backend:
          serviceName: be-service
          servicePort: 5000

Here is what I get with curl:这是我使用 curl 得到的结果:

curl [ip] --header "Host: my-host" -> React app (as expected) curl [ip] --header "Host: my-host" -> React 应用程序(如预期的那样)

curl [ip]/foo --header "Host: my-host" -> nginx 404 (why?) curl [ip]/foo --header "Host: my-host" -> nginx 404(为什么?)

curl [ip]/api --header "Host: my-host" -> nginx 404 (why?) curl [ip]/api --header "Host: my-host" -> nginx 404(为什么?)

curl [ip]/api/ --header "Host: my-host" -> nodejs app curl [ip]/api/ --header "Host: my-host" -> nodejs 应用程序

curl [ip]/api/foo --header "Host: my-host" -> nodejs app curl [ip]/api/foo --header "Host: my-host" -> nodejs 应用程序

As far as I can see a part with api/ works fine, but I can't figure out everything else, I tried different combinations with/without wildcards, but it still does not work in the way I want it to work.据我所知,带有api/的部分工作正常,但我无法弄清楚其他所有内容,我尝试了带/不带通配符的不同组合,但它仍然无法按照我希望的方式工作。

What am I missing?我错过了什么? Is this even possible?这可能吗? Thanks in advance!提前致谢!

I can't explain why /foo is not working我无法解释为什么 /foo 不起作用

But

/api/* does not cover /api, it covers only anything after /api/ /api/* 不涵盖 /api,它只涵盖 /api/ 之后的所有内容

I don't think the issue here is your ingress, but rather your nginx setup (without having seen it.), Since React apps are Single Page Applications, you need to tell the nginx server to always look for index.html instead of going to eg /usr/share/nginx/html/foo where there's probably nothing to find.我不认为这里的问题是你的入口,而是你的 nginx 设置(没有看到它。),因为 React 应用程序是单页应用程序,你需要告诉 nginx 服务器总是寻找index.html而不是去例如/usr/share/nginx/html/foo可能什么也找不到。

I think you'll find relevant information eg here .我想您会在这里找到相关信息。 I wish you good luck @Andrey, and let me know if this was at all helpful!祝你好运@Andrey,让我知道这是否有帮助!

I hope am not late to respond.我希望不会迟到回应。 Please use kubernetes use-regrex annotations to ensure that it maps to your services endpoints.请使用kubernetes use-regrex注释来确保它映射到您的服务端点。 Please make sure that the react app service mapping is done at the end of the reset.请确保在重置结束时完成 React 应用程序服务映射。 Use the following yaml使用以下 yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  rules:
  - host: my-host
    http:
      paths:
      - path: /api/?(.*)
        backend:
          serviceName: be-service
          servicePort: 5000
       - path: /?(.*)
        backend:
          serviceName: fe-service
          servicePort: 80

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

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