2

我有一个在 nodejs 上使用 React 和后端构建的前端应用程序。两者都有单独的 Docker 映像,因此在 k8s (gce) 上单独部署。

每个部署都有一个对应的 k8s 服务,比如说fe-sericeand be-service

我正在尝试设置一个入口,以便通过以下方式在单个域上公开这两个服务:

  • /api/*- 被路由到be-service
  • 其他一切都被路由到fe-service

这是我的 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

这是我用 curl 得到的:

curl [ip] --header "Host: my-host"-> React 应用程序(如预期)

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

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

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

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

据我所见,一部分api/工作正常,但我无法弄清楚其他所有内容,我尝试了带/不带通配符的不同组合,但它仍然无法按照我希望的方式工作。

我错过了什么?这甚至可能吗?提前致谢!

4

3 回答 3

2

我无法解释为什么 /foo 不起作用

/api/* 不涵盖 /api,它仅涵盖 /api/ 之后的任何内容

于 2018-03-07T06:32:48.907 回答
1

I hope am not late to respond. Please use kubernetes use-regrex annotations to ensure that it maps to your services endpoints. Please make sure that the react app service mapping is done at the end of the reset. Use the following 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

于 2021-02-25T08:32:12.443 回答
1

我不认为这里的问题是你的入口,而是你的 nginx 设置(没有看到它!)。由于 React 应用程序是单页应用程序,因此您需要告诉 nginx 服务器始终寻找index.html而不是去例如/usr/share/nginx/html/foo可能什么都找不到的地方。

我想您会在此处找到相关信息。祝你好运@Andrey,如果这有帮助,请告诉我!

于 2018-06-07T11:08:36.083 回答