Load Balancing an application in Kubernetes

3/21/2019

Lets say that I have two deployments which contain two instances of a backend application. (Instead of having one deployment with multiple replicas, as they need to be configured differently).

How would you guys go about load balancing between the two? The classic approach would be to set up HAProxy with the two backends. Does this sound right in the context of Kubernetes? Is there a better way to expose two deployments on a single Ingress Controller resource?

-- Neekoy
kubernetes
load-balancing

2 Answers

3/21/2019

You can define a Service that will be determined by labels selectors. The requests to the service will be spread across the deployments (as the same with ingress)

Example:

apiVersion: v1
kind: Service
metadata:
  labels:
    app: my-deployments
spec:
  ports:
  - port: 80
  selector:
    app: my-deployments
-- Amityo
Source: StackOverflow

3/21/2019

Ideally you should be running one deployment with multiple replicas. Define the service object selecting the backend pods. The service object automatically load balances the backend pods in round Robin fashion.

If you want to load balance multiple deployment objects then define one service each for deployment, ServiceA and serviceB. You should be running ha-proxy load balancing the traffic between ServiceA and serviceB.

We recommend you opt for first approach unless you have a valid reason to consider second approach

-- P Ekambaram
Source: StackOverflow