Thakur Amit
Thakur Amit

Reputation: 437

What are my options for adding load balancing for spring boot docker container

I have a project in spring boot and angular. The project is also dockerzied.

Now I want to add load balancing for my back-end, so what are my options. Should I use Zuul Proxy and Eureka, docker swarm or k8s or something else?

From what I understood about docker swarm and k8s is that they can run as many containers you want, but there isn't something like API-gateway (entry point) which routes the incoming requests and distributes the load between different containers of a particular micro-service. Maybe I am wrong about this.

Thanks in advance.

Upvotes: 1

Views: 2105

Answers (2)

johnwock
johnwock

Reputation: 11

you can use docker swarm , where master node in cluster can act as loadbalancer

Upvotes: 1

cmlonder
cmlonder

Reputation: 2540

Short Answer

  • Use Zuul as your API gateway
  • Use Eureka as service discovery and enable it in Zuul. So now Zuul can get other service information which is registered to Eureka
  • Enable Ribbon load balancing in Zuul. Normally Ribbon makes client-side load balancing but since client/server terminology is interchangeable, Zuul in your case is your server when you make your requests from your Angular to the backend, so Zuul will be your reverse proxy.

Using k8s or docker swarm (or even none) depends on how your applications run in production and how you maintain them. Also, some of the features you have in the Spring Cloud application stack can already be provided by these orchestrations. For instance; if you have k8s environment set up, you will have some configuration like below;

kind: Service
apiVersion: v1
metadata:
  name: foo
spec:
  selector:
    app: spring-foo-service
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 9102

And your service calls /foo where Kubernetes match it with its selector and redirects to request pods labeled with spring-foo-service. So for your service to service communication load balancer is done by Kubernetes already. You can still have client-side load balancing too, Ribbonships with Kubernetes. There are much more with Ingress, nginx configurations, external/internal load balancing but this topic is huge for your question, you can search more based on your infrastructure.

Long Answer

There are lots of concepts intertwined in your question that may lead to confusion. We should first divide them into topics by different microservice-based architectures; before start-up, spring cloud is not direct competitor to Kubernetes or Docker Swarm, still good to see them separately some solutions provided by each of them overlap.

Spring Cloud

  • API Gateway - Zuul. It can enable also load balancing feature but behind the scenes it uses ribbon.
  • Service Discovery - Eureka. Your services can register to Eureka and also can get other service informations from eureka (depend on how you configure your service with Eureka)
  • Client Side Load Balancer - Ribbon

There are also other elements in this structure like; configuration management, authentication, logging, monitoring which I will not go in detail since your question is about Load balancing. But don't forget that these are provided as a separate java application in your microservice stack.

But besides the above services, there are still missing elements in our structure that are not provided by Spring Cloud. Let's ask some questions;

  • How to scale my services under high traffic?
  • How to provide self-recovery of my services in case of failure?
  • How to provide high availability? And so on. As you realize our questions mainly about orchestration.

The services we described so far in Spring Cloud gives us to develop microservices. But since we are using microservices, we will probably have high demand which means we need operational orchestration/automation too. But to make it clear let's also divide this to different topics by combining it with spring cloud solutions above; manual maintenance & automation

Run Your Services - Manual

Imagine we develop our structure with Zuul, Eureka, Ribbon, and so on. Now we need to deploy our services, you can just start up your services with docker or you can also combine multiple of them with docker-compose (but not scaling up automatically, this is not available in this step). So everything is ready, we have some services;

  • registers to eureka
  • gets other service information from eureka
  • makes client-side load balancing (because each client nows other service locations from eureka, which is efficient way to server-side/central load balancer)
  • and so on.

But under high traffic, we need to start up another. This is not available in our application stack so far in Spring Cloud but you can also achieve it differently in this step. You can even scale up with docker-compose commands. Now let's quick overview to Orchestration solutions; Kubernetes, docker swarm etc.

Run Your Services - Container Orchestration

Both Kubernetes and Docker swarm provides orchestrations for your container. Also, they provide abstraction on the top of programming languages for microservice requirements.

Kubernetes

As already mentioned in the short answer, Kubernetes already covers some Spring Cloud Application features out of the box. For instance; you don't need service registry as an application in your structure anymore. In addition to this services provided in place of your application stack, Kubernetes provide scaling, deployment, resource management etc.

Summary

One is not an alternative to each other, you should be aware of some features overlap. So you should design your infrastructure based on this fact.

Upvotes: 4

Related Questions