spring-cloud-k8s 跨 NS 的坑

本文探讨了在 Kubernetes 环境下,Spring Cloud Kubernetes 面临跨命名空间服务访问的问题。通过分析 ClusterIP 类型 Service 的限制,展示了如何通过 Springcloud k8s 社区和 Kubernetes 原生策略解决跨命名空间服务发现和负载均衡的挑战。案例中,Ribbon 的负载均衡在不同命名空间下失效,提出了两种解决方案:社区提供的简单策略和直接利用 K8s 负载均衡。最后,提到了相关的开源项目和社区资源。

04a2bb776a24a6125cd7dddebb25a6c3.png

回顾

前面文章 (Spring Cloud Kubernetes 之实战服务注册与发现) 中,讲述了 spring-cloud-k8s 在微服务实践中,带来了多大的优势。介绍了 k8s 中资源 Service,其如何来实现服务的注册与发现。

其实在 k8s 中,Service 资源的类型比较多,有四种:

  • ExternalName:创建一个 DNS 别名指向 service name,这样可以防止 service name 发生变化,但需要配合 DNS 插件使用。

  • ClusterIP:默认的类型,用于为集群内 Pod 访问时,提供的固定访问地址,默认是自动分配地址,可使用 ClusterIP 关键字指定固定 IP。

  • NodePort:基于 ClusterIp,用于为集群外部访问 Service 后面 Pod 提供访问接入端口。

  • LoadBalancer:它是基于 NodePort。

我们一般会默认使用的类型:ClusterIP,但此时会出现一种问题,那就是此类型的 Service 被用来访问非同一 NS 下的 pods,即<servicename>.<namespace>.svc.cluster.local形式访问 pod,只能通过 servicename 直接访问同一 namespace 下的 pod。

案例

下面,我们来看案例:假设我这里有三个服务:cas-server、rest-service、diff-ns-service 等,我通过 deployment 来部署这些服务的 pod。

a7cf8bf6121c132d6397cda9629bf840.png
image.png

可以看到这些 pod 处于 不同的 namespace 下,同样的对应的 service 也是处于对应的 namespace 下:

ns-app          diff-ns-service-service   ClusterIP   10.16.178.187   <none>        2008/TCP        6h39m   app=diff-ns-service
system-server   cas-server-service        ClusterIP   10.16.134.168   <none>        2000/TCP        16d     app=cas-server
system-server   rest-service-service      ClusterIP   10.16.128.58    <none>        2001/TCP        16d     app=rest-service

这里的 Service 类型都是 ClusterIp,在前面,我们验证过基于这样的服务,我们可以利用 springcloud-k8s 来实现同一 namespace 下服务之间的注册与发现,实现负载均衡。但如果不在同一 namespace 下呢?比如这里的diff-ns-service,它与另外两个服务不在同一 namespace。此时我们通过基于 Ribbon 的负载均衡策略。这是因为我们默认了 KubernetesRibbonMode 的模式:POD,就是获取服务提供者的 pod 的 ip 和 port,该 ip 是 kubernetes 集群的内部 ip,只要服务消费者是部署在同一个 kubernetes 集群内就能通过 pod 的 ip 和服务提供者暴露的端口访问。当我们使用当modeSERVICE时,就是获取服务提供者在 k8s 中的 service 的名称和端口,使用这种模式会导致 Ribbon 的负载均衡失效,转而使用 k8s 的负载均衡。

所以,如果不使用默认的 Ribbon 来实现负载均衡,可以配置:

spring:
  cloud:
    kubernetes:
      ribbon:
        mode: SERVICE

这个前提其实还是在同一 namespace 下,但如果不在同一 NS 呢?还是设置为SERVICE模式,但里面还是用 k8s 原生的调用方式:<servicename>.<namespace>.svc.cluster.local***,假设这里需要调用 diff-ns-service,则:

ResponseEntity<String> responseEntity = new RestTemplate().exchange("http://diff-ns-service-service/getservicedetail?servicename=cas-server-service",
     HttpMethod.GET, formEntity, String.class);

访问请求该服务时,发现并未请求到:

2021-11-04 09:23:16.830:147 [http-nio2-2001-exec-2] DEBUG org.springframework.web.client.RestTemplate -HTTP GET http://diff-ns-service-service/getservicedetail?servicename=cas-server-service
2021-11-04 09:23:16.834:147 [http-nio2-2001-exec-2] DEBUG org.springframework.web.client.RestTemplate -Accept=[text/plain, application/json, application/*+json, */*]
2021-11-04 09:23:16.859:255 [http-nio2-2001-exec-2] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor -Using 'text/html', given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json]
2021-11-04 09:23:16.860:91 [http-nio2-2001-exec-2] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor -Writing [""]

如果走 mode 为POD的 Ribbon 的负载均衡:

2021-11-04 09:34:32.188:147 [http-nio2-2001-exec-2] DEBUG org.springframework.web.client.RestTemplate -HTTP GET http://diff-ns-service-service/getservicedetail?servicename=cas-server-service
2021-11-04 09:34:32.193:147 [http-nio2-2001-exec-2] DEBUG org.springframework.web.client.RestTemplate -Accept=[text/plain, application/json, application/*+json, */*]
2021-11-04 09:34:32.261:115 [http-nio2-2001-exec-2] INFO  com.netflix.config.ChainedDynamicProperty -Flipping property: diff-ns-service-service.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2021-11-04 09:34:32.271:197 [http-nio2-2001-exec-2] INFO  com.netflix.loadbalancer.BaseLoadBalancer -Client: diff-ns-service-service instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=diff-ns-service-service,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2021-11-04 09:34:32.278:222 [http-nio2-2001-exec-2] INFO  com.netflix.loadbalancer.DynamicServerListLoadBalancer -Using serverListUpdater PollingServerListUpdater
2021-11-04 09:34:32.281:88 [http-nio2-2001-exec-2] WARN  org.springframework.cloud.kubernetes.ribbon.KubernetesEndpointsServerList -Did not find any endpoints in ribbon in namespace [system-server] for name [diff-ns-service-service] and portName [null]
2021-11-04 09:34:32.282:150 [http-nio2-2001-exec-2] INFO  com.netflix.loadbalancer.DynamicServerListLoadBalancer -DynamicServerListLoadBalancer for client diff-ns-service-service initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=diff-ns-service-service,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:org.springframework.cloud.kubernetes.ribbon.KubernetesEndpointsServerList@d48bf01
2021-11-04 09:34:32.308:255 [http-nio2-2001-exec-2] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor -Using 'text/html', given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json]
2021-11-04 09:34:32.309:91 [http-nio2-2001-exec-2] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor -Writing [""]
2021-11-04 09:34:32.316:1131 [http-nio2-2001-exec-2] DEBUG org.springframework.web.servlet.DispatcherServlet -Completed 200 OK

此时,给我的感觉就是:

spring:
  cloud:
    kubernetes:
      ribbon:
        #直接走k8s的LB
        mode: SERVICE #POD走ribbon的LB
      discovery:
        all-namespaces: true

此类配置是无法进行 Service 到应用服务的访问,只能访问到 Service。同时我们看到日志:

2021-11-04 09:34:32.281:88 [http-nio2-2001-exec-2] WARN  org.springframework.cloud.kubernetes.ribbon.KubernetesEndpointsServerList -Did not find any endpoints in ribbon in namespace [system-server] for name [diff-ns-service-service] and portName [null]

上面给到的是 mode 为 POD 时,走的 Ribbon 的负载均衡后,无法找到当前 pod 对应的 NS 下的 Servcie 为 diff-ns-service-service 的服务。

2021-11-04 09:20:27.109:89 [PollingServerListUpdater-1] WARN  org.springframework.cloud.kubernetes.ribbon.KubernetesServicesServerList -Did not find any service in ribbon in namespace [system-server] for name [diff-ns-service-service] and portName [null]

同样地,当 mode 为 SERVICE 时,依然无法找到当前 pod 的对应的 NS 的 Servcie 为 diff-ns-service-service 的服务。

同样会拿不到请求返回信息,这里说明:在不同NS下,Service为ClusterIP,不管如何负载均衡,都无法访问。

解决方案

一、通过 Springcloud k8s 社区来实现跨 NS 下的服务的相互访问的简单策略
二、走 K8s 的原生的负载均衡策略

从前面的分析可以看到:虽然 spring-cloud-k8s 帮我们发现了 Service,但在底层策略时,不同的NS还是做了隔离,只能通过 k8s 原生的方式来进行服务的发现:<servicename>.<namespace>.svc.cluster.local

PS:同时,我们需要注意的是,此时基于 k8s 负载均衡,我们不能再基于 Ribbon 或其他来进行负载均衡机制了,直接通过 Http 协议来请求 k8s 的 service,实现跨 NS 的 pod 之间的互通。

开源项目

  实践项目代码开源:https://gitee.com/damon_one/microservice-k8s

欢迎大家star、fork,欢迎联系我,一起学习。

云原生社区合肥站

云原生社区合肥站正式启动啦,欢迎Base合肥、关注云原生、长期从事云原生的同志们踊跃加入,云原生社区合肥站会因为你们的加入而变得更加美好~

详情参见Issue:https://github.com/cloudnativeto/community/issues/107

欢迎关注个站

c38379a67a427be11c4cde0220765338.png

往期回顾

微服务自动化部署CI/CD

如何利用k8s拉取私有仓库镜像

个站建设基础教程

ArrayList、LinkedList&nbsp;你真的了解吗?

大佬整理的mysql规范,分享给大家

如果张东升是个程序员

微服务架构设计之解耦合

浅谈负载均衡

Oauth2的认证实战-HA篇

Oauth2的授权码模式《上》

浅谈开发与研发之差异

浅谈&nbsp;Java&nbsp;集合&nbsp;|&nbsp;底层源码解析

基于 Sentinel 作熔断 | 文末赠资料

基础设施服务k8s快速部署之HA篇

今天被问微服务,这几点,让面试官刮目相看

Spring cloud 之多种方式限流(实战)

Spring cloud 之熔断机制(实战)

面试被问finally 和 return,到底谁先执行?

Springcloud Oauth2 HA篇

Spring Cloud Kubernetes之实战一配置管理

Spring Cloud Kubernetes之实战二服务注册与发现

Spring Cloud Kubernetes之实战三网关Gateway

0920bd9ef288f2e18fcaaf89991c927f.png

c70fe73edfc61b9b5cf929e3445e9071.png

cc5d4164c85d6dd512fe396655953a14.png

关注公众号,回复入群,获取更多惊喜!公众号(程序猿Damon)里回复 ES、Flink、Java、Kafka、MQ、ML、监控、大数据、k8s 等关键字可以查看更多关键字对应的文章。

60f58d59be1a6e063b3b2676295e73a9.gif

ca5d0f5ade429fddd0e4c4d95b15f63e.gif

点击 "damon8.cn" 获取更好的阅读体验!

❤️给个「在看」,是对我最大的支持❤️
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值