A Kuber­netes load balancer auto­mat­ic­ally dis­trib­utes network traffic across multiple pods to ensure even load dis­tri­bu­tion and high avail­ab­il­ity. It is typically im­ple­men­ted through a ‘Load­Bal­an­cer’ type service that forwards external requests to internal services. This way, ap­plic­a­tions can be made reliably and scalably ac­cess­ible.

IONOS Cloud Managed Kuber­netes
Container workloads in expert hands

The ideal platform for demanding, highly scalable container ap­plic­a­tions. Managed Kuber­netes works with many cloud-native solutions and includes 24/7 expert support.

What is a load balancer in Kuber­netes?

Load balancers dis­trib­ute the workload across servers or virtual machines as ef­fi­ciently as possible, helping to boost the overall per­form­ance of the system. Po­si­tioned in front of the servers, a load balancer prevents in­di­vidu­al machines from becoming over­loaded and ensures optimal use of available resources. Even if a server fails, load balancing keeps the system running smoothly by in­tel­li­gently re­dir­ect­ing requests.

Kuber­netes load balancers operate a bit dif­fer­ently—but with the same un­der­ly­ing concept. In Kuber­netes, however, dis­tinc­tion must be made between two different types of load balancers:

  • Internal Kuber­netes load balancers
  • External Kuber­netes load balancers

Internal Kuber­netes load balancers

Internal Kuber­netes load balancers take a different approach than classic load balancers and are mentioned here for com­plete­ness. They ensure that only ap­plic­a­tions running within the same virtual network as their Kuber­netes cluster can access this network.

External Kuber­netes load balancers

External load balancers assign a specific IP address or a DNS name to a service node of a Kuber­netes cluster, allowing it to receive external HTTP requests. The ‘Load­Bal­an­cer’ is a special Kuber­netes service type designed to forward external traffic to in­di­vidu­al Kuber­netes pods within the cluster, ensuring an optimal dis­tri­bu­tion of incoming requests.

Note

There are several options or al­gorithms to configure load balancing in Kuber­netes. The one you choose depends entirely on your in­di­vidu­al needs. The different al­gorithms primarily determine the principle by which the load balancer processes incoming traffic.

How does a load balancer work?

In Kuber­netes, a load balancer takes on the central task of ef­fi­ciently dis­trib­ut­ing network traffic across multiple instances of a service, namely the pods. The goal is to ensure balanced util­isa­tion, increase avail­ab­il­ity, and com­pensate for the failure of in­di­vidu­al com­pon­ents.

Tech­nic­ally, the load balancer receives incoming requests and checks which pods are currently available and efficient. Kuber­netes uses con­tinu­ous internal mon­it­or­ing for this purpose: Pods that are faulty or over­loaded are auto­mat­ic­ally excluded from routing. The load balancer then dy­nam­ic­ally decides which pod to forward each request to.

This dis­tri­bu­tion is based on various criteria. Users are unaware of this process. The ap­plic­a­tion remains ac­cess­ible and high per­form­ing, even when in­di­vidu­al pods start, are re­deployed, or fail in the back­ground.

Image: Overview of how Kubernetes load balancer works
The load balancer dis­trib­utes external traffic to the pods of a service in the Kuber­netes cluster, shown in yellow.

What is a Kuber­netes load balancer for?

A Kuber­netes load balancer defines a service running within the cluster that is ac­cess­ible over the public internet. To un­der­stand this, it’s helpful to look at the Kuber­netes ar­chi­tec­ture. A cluster includes multiple nodes, each con­tain­ing several pods. Each pod in the cluster is assigned an internal IP, which cannot be accessed from outside the cluster.

Making software available under a fixed IP

To make the software running in pods usable under a dedicated IP address, a Kuber­netes service is typically required. Besides ‘Load­Bal­an­cer’, there are other service types suitable for various scenarios. All service types share the char­ac­ter­ist­ic of grouping a set of pods into a logical unit and de­scrib­ing how they can be accessed.

Optimal dis­tri­bu­tion of external traffic

A Kuber­netes load balancer is designed to ensure optimal dis­tri­bu­tion of external traffic to the pods in your Kuber­netes cluster. This makes these services suitable for virtually any use case. Since Kuber­netes load balancers can direct traffic spe­cific­ally to in­di­vidu­al pods, high avail­ab­il­ity of your cluster is guar­an­teed: If a pod becomes non-func­tion­al or exhibits errors, the load balancer ensures that tasks are dis­trib­uted to the other pods.

Op­tim­ising scalab­il­ity

Scalab­il­ity is also pos­it­ively impacted by the use of load balancing. Kuber­netes can auto­mat­ic­ally create or delete pods as needed. Thus, if it is de­term­ined that incoming traffic requires more or fewer resources than currently available, Kuber­netes can auto­mat­ic­ally respond to this situation.

How to create a load balancer for Kuber­netes

To create a Kuber­netes load balancer, your cluster must run in a cloud or an en­vir­on­ment that supports the con­fig­ur­a­tion of external load balancers.

At IONOS, a static IP is assigned to a node in the cluster when a Kuber­netes load balancer is created. This IP allows the service to be accessed from outside the cluster. The Kube-Proxy running on the node in­tel­li­gently dis­trib­utes incoming traffic to the in­di­vidu­al pods.

First, create a service and then set the service type to Load­Bal­an­cer by adding the following line to the service manifest:

type: LoadBalancer

For example, the con­fig­ur­a­tion of a Kuber­netes load balancer might look like this: The service groups pods under the ‘web-app’ selector. Incoming traffic on port 8080 under the load balancer IP is dis­trib­uted to the in­di­vidu­al pods, ad­dress­ing the service running on each pod at port 80:

apiVersion: v1
kind: Service
metadata:
    name: web-app-service
spec:
    selector:
        app: web-app
    type: LoadBalancer
    loadBalancerIP: 203.0.113.0
    ports:
        - name: http
            port: 8080
            targetPort: 80
            protocol: TCP
yaml

Another way to create a Kuber­netes load balancer is through the kubectl command line.

With the command

kubectl expose deployment test --target-port=9376 \
    --name=test-service --type=LoadBalancer
bash

create and deploy a new service named ‘test-service’ that functions as a load balancer.

If you want to find out the IP address of your newly created service, the following command will help:

kubectl describe services test-service
bash
Go to Main Menu