SPA、微服务和 Kubernetes(架构问题)

SPA, Microservices and Kubernetes (Architectural Question)

首先,我要说的是,我对 Kubernetes 和微服务架构总体上还很陌生。我还要说,这更像是一个高级架构问题,而不是寻求与技术相关的规范性操作建议。我想我可以弄清楚实现细节,但我不确定存在哪些机制可以让我到达我想去的地方。

应用概览

K8s 集群上的简单 "e-commerce" 示例应用程序 运行。 SPA 前端,带有 API.

的 .NET Core 服务

客户端(前端)

Angular SPA 已构建并部署到 NGinx 容器,该容器将 Angular 应用程序作为静态站点提供服务。它是 运行 作为集群上的 LoadBalancer 服务。

服务(后端)

两个简单服务 运行 作为集群中的 ClusterIP。我们称它们为 Product 和 Order。

问题

部署 SPA 时,它会从 用户的浏览器 而非容器向服务发出请求。这些服务不是 LoadBalancer 配置中的 运行,因此它们不会暴露在集群之外。让客户端应用程序与服务对话的最佳实践是什么?具体来说:

我搜索过 SO 并发现了类似的问题,但 none 正是问这个问题。如果有人指出我现有的启发我的问题,我会很乐意删除这个问题。

Do I really need to expose every microservice externally?

不,你不应该。

Is there some kind of proxy technique, where I can expose a single /api endpoint, that routes to the appropriate backend service?

标准方法是使用 nginx 作为代理。

Any referenceable repos I can look at for an example?

你可以查看我的玩具项目:

具体来说,这是您从上面的玩具项目中引用 nginx 后端 api 的方式(注意它使用的是 websockets,这对您来说可能不是这种情况):

    location /api {
        proxy_pass http://backend;
        proxy_set_header Host            $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header        X-Forwarded-Proto $http_x_forwarded_proto; # aws version - essentially this sets https schema

        # enable WebSockets
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

一个是 Angular 应用程序,您可以将其用作静态网站,或者可以部署到另一个 CDP ..另一个是:

Is there some kind of proxy technique, where I can expose a single /api endpoint, that routes to the appropriate backend service?

我认为你应该使用kubernetes ingress(默认nginx,但你可以使用traefik等。),这里的重点是你只需要将kubernetes ingress暴露给负载均衡器服务,然后你就可以使用入口规则路由您的流量。

我不知道您在哪里工作...云端还是本地。我必须在内部执行此操作,并且我正在使用 MetalLb 作为负载平衡器。 MetalLb

我推荐您观看这个很棒的频道的视频:justmeandopensource

nginx ingress with metallb

nginx ingress bare Metal with ha proxy as load balancer

抱歉,如果我遗漏了什么,请快速回复! :)