在 Kubernetes 集群中与 API 进行前端通信

Frontend communication with API in Kubernetes cluster

在 Kubernetes 集群中,我是 运行 1 个节点和 2 个部署。 React 前端和 .NET Core 应用程序。我还有一个用于前端应用程序的负载均衡器服务。 (一切正常:我可以通过端口转发来查看后端部署是否正常工作。)

问题:我正在尝试让前端和API进行通信。我知道我可以使用面向外部的负载均衡器来做到这一点,但是有没有一种方法可以使用 clusterIP 来做到这一点并且后端没有外部 IP?

我们之所以对此感兴趣,是因为它只是增加了一层安全性。仅将 API 保留为 vnet,我们将再删除一个入口点。

如果有帮助,我们将使用 AKS 在 Azure 中进行部署。我知道他们有时会有一些奇怪的部署。

集群上的

Pods 运行 可以使用默认服务类型 ClusterIP 服务相互通信。您不需要 LoadBalancer 服务即可让两个 pods 相互通信。根据the docs on this topic

ClusterIP exposes the service on a cluster-internal IP. Choosing this value makes the service only reachable from within the cluster. This is the default ServiceType.

the Discovery documentation 中所述,如果 Pods(前端和 API)都在同一命名空间中 运行,则前端只需要将请求发送到后端服务的名称。

如果它们 运行 在不同的命名空间上,前端 API 需要使用完全限定的域名才能与后端通信。

For example, if you have a Service called "my-service" in Kubernetes Namespace "my-ns" a DNS record for "my-service.my-ns" is created. Pods which exist in the "my-ns" Namespace should be able to find it by simply doing a name lookup for "my-service". Pods which exist in other Namespaces must qualify the name as "my-service.my-ns". The result of these name lookups is the cluster IP.

您可以找到有关 DNS 如何在 kubernetes 上工作的更多信息 in the docs

I'm trying to get the front end and api to communicate

api,如果你指的是 Kubernetes API 服务器,首先为前端 pod 设置一个服务帐户和令牌,以便通过以下方式与 Kubernetes API 服务器通信步骤 here, here and here.

is there a way to do that using the clusterIPs and not have an external IP for the back end

是的,如果服务不需要外部访问,这是可能的并且更安全。服务类型 ClusterIP 将没有 ExternalIP,并且 pods 可以在集群内使用 ClusterIP:Port 相互通信。

此配置的问题在于前端应用程序将尝试通过内部集群连接到 API。但它不会。我的应用程序在客户端浏览器上无法访问我的 Kluster 中的服务和 pods。

我的集群需要像 nginx 或其他外部负载均衡器这样的东西来允许我的客户端 api 呼叫到达我的 API。

您也可以使用您的前端应用程序作为您的代理,但强烈不建议这样做!