2

Does Spring Cloud Kubernetes support Spring Cloud Loadbalancer?

I am to deploy Spring boot application on Kubernetes and also perform client side load balancing with ribbon client by following link https://cloud.spring.io/spring-cloud-static/spring-cloud-kubernetes/2.0.0.M1/reference/html/#ribbon-discovery-in-kubernetes

I want to use new Spring cloud loadbalancer instead of Ribbon client.

James Westgate
  • 11,306
  • 8
  • 61
  • 68
anjani
  • 21
  • 2

2 Answers2

1

Yes, it does. A full example can be found here - https://github.com/dhananjay12/spring-microservices-using-spring-kubernetes

Basically, following dependency would do it

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-kubernetes</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  <exclusions>
    <exclusion>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-archaius</artifactId>
    </exclusion>
    <exclusion>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    </exclusion>
    <exclusion>
      <groupId>com.netflix.ribbon</groupId>
      <artifactId>ribbon-eureka</artifactId>
    </exclusion>
  </exclusions>
</dependency>
Dhananjay
  • 1,140
  • 1
  • 12
  • 28
0

I'm late to the party but would like to share that the support for Spring Cloud LoadBalancer was added to Spring Cloud Kubernetes last year and it works as a charm. As of Spring Cloud 2020.0. (aka Ilford), it is the default client-side load balancer therefore you don't need to exclude ribbon anymore.

I'd suggest including the org.springframework.cloud:spring-cloud-starter-kubernetes-client-all dependency in your project to leverage all the niceties enabled by Spring Cloud Kubernetes.

The most typical way to use Spring Cloud LoadBalancer on Kubernetes is with service discovery. If you have any DiscoveryClient on your classpath, the default Spring Cloud LoadBalancer configuration uses it to check for service instances. As a result, it only chooses from instances that are up and running. All that is needed is to annotate your Spring Boot application with @EnableDiscoveryClientto enable K8s-native Service Discovery.

You can read more about it on the Spring Cloud Kubernetes documentation.

dbaltor
  • 2,737
  • 3
  • 24
  • 36