简体   繁体   English

在 Kubernetes 中将 React 前端连接到 Node JS 后端

[英]Connecting React Frontend to Node JS backend within Kubernetes

What is the recommended way to make API requests from a React Frontend Application to a Node JS backend application within Kubernetes?从 React 前端应用程序向 Kubernetes 中的 Node JS 后端应用程序发出 API 请求的推荐方法是什么? They are both hosted on the same container, with the dockerfile as such:它们都托管在同一个容器上,dockerfile如下:

COPY frontend/package.json .
COPY frontend/yarn.lock .
RUN npm install
COPY frontend/tsconfig.json .
COPY frontend/src ./src
COPY frontend/public/ ./public

WORKDIR /app/backend
COPY backend/package.json .
RUN npm install
COPY backend/index.js .
COPY backend/controllers ./controllers
COPY backend/models ./models
COPY backend/routes ./routes
EXPOSE 3000
EXPOSE 3001

WORKDIR /app/frontend
RUN npm run build
RUN npm install -g serve

WORKDIR /app
RUN mkdir newImages
RUN mkdir exportedData
COPY image/* newImages/

COPY startup.sh /app
CMD /app/startup.sh

startup.sh navigates to both the frontend and backend folders and starts both servers. startup.sh 导航到前端和后端文件夹并启动两个服务器。 Running each of these locally on my machine works fine, and api requests go through correctly.在我的机器上本地运行这些都可以正常工作,并且 api 请求可以正确通过。 However, when I deploy to Kubernetes, API requests made from my browser all time out.但是,当我部署到 Kubernetes 时,我的浏览器发出的 API 请求都会超时。 I attempt to use the friendly name of the pod - so these requests are being made to http://podname:PORT/api/XXX.我尝试使用 pod 的友好名称 - 所以这些请求是向 http://podname:PORT/api/XXX 发出的。 If I exec into the pod and run the requests through curl, the api requests work fine.如果我执行到 pod 并通过 curl 运行请求,则 api 请求工作正常。 Similarly, if I expose the API port with a LoadBalancer and replace "podname" with a hardcoded external IP for the service, everything works fine.同样,如果我使用 LoadBalancer 公开 API 端口并将“podname”替换为服务的硬编码外部 IP,则一切正常。 However, that is not a viable long term solution as External IP's can change easily if pods go down/are restarted.但是,这不是一个可行的长期解决方案,因为如果 pod 关闭/重新启动,外部 IP 可以轻松更改。

Is there some easy way to route these requests properly through a proxy within the frontend, or will I need to set up an NGINX controller to handle everything here?有没有一些简单的方法可以通过前端的代理正确路由这些请求,或者我需要设置一个 NGINX 控制器来处理这里的所有事情? I assume the issue is that React is running directly within the browser I access the frontend from - causing requests to come from my computer, meaning that IP's are not properly resolved (or even accessible, given that the friendly name resolves to the internal cluster IP)我认为问题是 React 直接在我访问前端的浏览器中运行 - 导致请求来自我的计算机,这意味着 IP 没有正确解析(或者甚至无法访问,因为友好名称解析为内部集群 IP )

Any help would be appreciated!任何帮助,将不胜感激!

TL;DR Two Things TL;DR 两件事

  1. You cannot use pod names to connect with services from outside the cluster.您不能使用 pod 名称来连接集群外部的服务。 That is because pod names are DNS names that are resolved into IP addresses that are internal to your cluster and this subnet cannot be reached from the outside.这是因为 pod 名称是解析为集群内部 IP 地址的 DNS 名称,并且无法从外部访问此子网。 And you SHOULDN'T use pod names to connect to your pods from the outside, that's what services are for.而且您不应该使用 pod 名称从外部连接到您的 pod,这就是服务的用途。 Because once your application grows to a larger size, 100s of pods, it will be impossible to maintain pod names on the client side.因为一旦您的应用程序增长到更大的规模,即 100 个 pod,就不可能在客户端维护 pod 名称。

  2. With a LoadBalancer service type, your design will work perfectly fine.使用LoadBalancer服务类型,您的设计将完美运行。 You don't have to worry about IP addresses changing and not 'mapping to pods' anymore.您不必担心 IP 地址发生变化,也不必再担心“映射到 pod”。 That is because your pods are being interfaced by a service.那是因为您的 pod 正在由服务接口。 The connectivity between this service and your pods is consistent even if your LoadBalancer IP address keeps changing.即使您的LoadBalancer IP 地址不断变化,此服务与您的 Pod 之间的连接也是一致的。 All that will change is the way you will connect to your Service object.所有这一切都会改变的是您将连接到您的Service对象的方式。 The internal connectivity between a service and its pods is not your concern at all.服务和它的 pod 之间的内部连接根本不是你关心的。

Also, please note that it is a very bad idea to design an application this way, where two separate logical components sit on the same container.另外,请注意,以这种方式设计应用程序是一个非常糟糕的主意,因为两个独立的逻辑组件位于同一个容器上。 It's a purely monolithic design and it is very hard to scale and maintain.这是一个纯粹的单体设计,很难扩展和维护。 You shouldn't even use different containers, but rather different pods for this, to begin with.首先,您甚至不应该使用不同的容器,而应该使用不同的 pod。 There will be no issues since all pods can communicate with each other very transparently in Kubernetes and no extra configuration will be needed.不会有任何问题,因为所有 pod 都可以在 Kubernetes 中非常透明地相互通信,并且不需要额外的配置。

Ideally, you will have to setup two different services that will connect with each other.理想情况下,您必须设置两个相互连接的不同服务。

Bottom line is, there is no need to setup a proxy to directly connect your pods to your clients.底线是,无需设置代理即可将您的 pod 直接连接到您的客户端。 Instead, simply use a service of LoadBalancer type that exposes an endpoint your clients can use.相反,只需使用LoadBalancer类型的服务,该服务公开您的客户端可以使用的端点。 And there is no need to worry about changing externalIPs.并且无需担心更改外部IP。 If you are using the application internally, you can fix a private IP address which can easily be used.如果您在内部使用该应用程序,您可以修复一个易于使用的私有 IP 地址。 If you are deploying this application to a larger scale, you really need to rethink your design and also invest into static public IP addresses.如果您要更大规模地部署此应用程序,您确实需要重新考虑您的设计并投资于静态公共 IP 地址。

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

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