java - Kubernetes 上微服务之间集群间通信的最佳方式?

标签 java spring-boot kubernetes microservices kubernetes-ingress

我是微服务新手,想了解在 Kubernetes 上部署的微服务中实现以下行为的最佳方式是什么:

有 2 个不同的 K8s 集群。微服务B部署在两个集群上。

现在,如果微服务 A 调用微服务 B,而 B 的 pod 在集群 1 中不可用,则该调用应转到集群 2 的 B。

我本可以想象使用 Netflix OSS 来实现此功能,但这里我没有使用它。

另外,先把集群间通信放在一边,我应该如何在微服务之间进行通信?

我知道的一种方法是为每个微服务创建 NodePort 类型的 Kubernetes 服务,并在调用微服务中使用 IP 和 nodePort。

问题:如果有人删除了目标微服务的 K8s 服务怎么办?在重新创建 K8s 服务时,K8s 将随机分配一个新的 nodePort,然后我将不得不返回到我的调用微服务并更改目标微服务的 nodePort。如何与nodePort解耦?

我研究了 kubedns,但似乎它只能在集群内工作。

我对 Istio 和 Kubernetes Ingress 的了解非常有限。其中任何一项是否提供了我正在寻找的东西?

抱歉问了一个很长的问题。任何类型的指导都会非常有帮助。

最佳答案

您可以使用服务公开您的应用程序,您可以使用某些类型的服务:

  • 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.

  • NodePort: Exposes the Service on each Node’s IP at a static port (the NodePort). A ClusterIP Service, to which the NodePort Service routes, is automatically created. You’ll be able to contact the NodePort Service, from outside the cluster, by requesting <NodeIP>:<NodePort>.

  • LoadBalancer: Exposes the Service externally using a cloud provider’s load balancer. NodePort and ClusterIP Services, to which the external load balancer routes, are automatically created.

  • ExternalName: Maps the Service to the contents of the externalName field (e.g. foo.bar.example.com), by returning a CNAME record

对于内部通信,您可以使用服务类型 ClusterIP ,您可以为您的应用程序配置服务 dns,而不是 IP。 即:名为 my-app-1 的服务可以使用 dns http://my-app-1 在内部进行访问或使用 FQDN http://my-app-1.<namespace>.svc.cluster.local .

对于外部通信,您可以使用NodePortLoadBalancer .

NodePort当您只有很少的节点并且知道所有节点的 IP 时,这是很好的选择。是的,通过 service docs您可以指定特定的端口号:

If you want a specific port number, you can specify a value in the nodePort field. The control plane will either allocate you that port or report that the API transaction failed. This means that you need to take care of possible port collisions yourself. You also have to use a valid port number, one that’s inside the range configured for NodePort use.

LoadBalancer给你更多的灵 active ,因为你不需要知道所有节点的IP,你只需要知道服务IP和端口。但是LoadBalancer仅在云提供商中支持,如果您想在裸机集群中实现,我建议您查看MetalLB .

最后,还有另一个选项是使用 ingress在我看来,这是向外部公开 HTTP 应用程序的最佳方式,因为您可以通过路径和主机创建规则,并且它为您提供了比服务更大的灵 active 。但支持 HTTP/HTTPS,如果您需要 TCP,请转到“服务”。

我建议您查看这些链接,深入了解服务和入口的工作原理:

Kubernetes Services

Kubernetes Ingress

NGINX Ingress

关于java - Kubernetes 上微服务之间集群间通信的最佳方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61110127/

相关文章:

java - 如何通过 filename.jsp 以外的不同 url 模式运行 jsp 文件?

java - 将 JFreeChart 添加到 JPanel

java - 如何使用Java Spring仅在浏览器的一部分中显示pdf文件?

java - 是否可以在 JSP 的类标记中使用数据库中的某些内容?

java - Spring boot @ConfigurationProperties 未加载

java - 在 Arquillian Drone 扩展中使用 BrowserMob 代理时 HAR 不完整

java - 运行时权限不适用于 API 级别 28

rest - 理解 Go 中 Kubernetes API 框架的逻辑

kubernetes - 通过一个API请求从节点中删除特定的污点

tomcat - Kubernetes 单点故障和负载均衡