[英]Why can I not open a React web app in a browser through an ingress when deploying it in a Kubernetes cluster
I have a very simple React web app (using Argon Dashboard Pro Template) deployed in a Kube.netes cluster.我在 Kube.netes 集群中部署了一个非常简单的 React web 应用程序(使用 Argon Dashboard Pro 模板)。 The Docker image of it works locally as well as in the cluster when exposing it via nodeport.
当通过节点端口公开它时,它的 Docker 图像在本地和集群中工作。 But exposing it via NGINX ingress doesn't work, although the ingress itself is tested for other services and applications which expose REST endpoints.
但是通过 NGINX 入口暴露它是行不通的,尽管入口本身已经针对暴露 REST 端点的其他服务和应用程序进行了测试。 The content of the web page is not built correctly, because some js files are not found, although this is the case when they are exposed via nodeport.
web页面的内容没有正确构建,因为有些js文件没有找到,虽然通过nodeport暴露出来是这样的。
Kube.netes Deployment: Kube.netes 部署:
apiVersion: apps/v1
kind: Deployment
metadata:
name: react-deployment
namespace: support
labels:
app: react
stage: dev
spec:
replicas: 1
template:
metadata:
labels:
app: react
spec:
containers:
- name: react
image: fmaus/react-test:argon
ports:
- containerPort: 3000
name: react-port
imagePullPolicy: Always
restartPolicy: Always
selector:
matchLabels:
app: react
Kube.netes Ingress Kube.netes 入口
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: react-ingress
namespace: support
annotations:
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/rewrite-target: /$2
nginx.ingress.kubernetes.io/configuration-snippet: |
proxy_set_header Accept-Encoding "";
more_set_headers "Content-Type: text/javascript; charset=UTF-8";
nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
ingressClassName: nginx
rules:
- http:
paths:
- path: /test(/|$)(.*)
pathType: Prefix
backend:
service:
name: react-service
port:
number: 3000
Kube.netes Service: Kube.netes 服务:
apiVersion: v1
kind: Service
metadata:
name: "react-service"
namespace: support
spec:
selector:
app: "react"
ports:
- port: 3000
type: ClusterIP
This Code can also be found in my GitHub Repository: https://github.com/fmaus/react-kube.netes-test此代码也可以在我的 GitHub 存储库中找到: https://github.com/fmaus/react-kube.netes-test
To reproduce the problem, just apply these Kube.netes resources to a cluster and try to reach the web app through a browser via ingress (http://host/subpath).要重现该问题,只需将这些 Kube.netes 资源应用到集群并尝试通过浏览器通过入口(http://host/subpath)访问 web 应用程序。 I have the resources deployed here: http://c105-164.cloud.gwdg.de:31600/test The error messages can be visited in the console of the browser (F12 when using Firefox):
我这里部署了资源: http://c105-164.cloud.gwdg.de:31600/test错误信息可以在浏览器的控制台访问(使用Firefox时F12):
Loading failed for the <script> with source “http://c105-164.cloud.gwdg.de:31600/static/js/bundle.js”. subpath:61:1
Loading failed for the <script> with source “http://c105-164.cloud.gwdg.de:31600/static/js/vendors~main.chunk.js”. subpath:61:1
Loading failed for the <script> with source “http://c105-164.cloud.gwdg.de:31600/static/js/main.chunk.js”.
I use Mozilla Firefox and the following NGINX ingress controller: https://kube.netes.github.io/ingress-nginx/我使用 Mozilla Firefox 和以下 NGINX 入口 controller: https://kube.netes.github.io/ingress-nginx/
I think you have two issues in place here:我认为你在这里有两个问题:
For example例如
<script src="/static/js/bundle.js"></script><script src="/static/js/vendors~main.chunk.js"></script><script src="/static/js/main.chunk.js"></script></body>
Needs to load the bundle from /subpath/static/js/bundle.js since it is an absolute link.需要从 /subpath/static/js/bundle.js 加载包,因为它是一个绝对链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.