A Ku­ber­netes load balancer au­to­mat­i­cal­ly dis­trib­utes network traffic across multiple pods to ensure even load dis­tri­b­u­tion and high avail­abil­i­ty. It is typically im­ple­ment­ed through a “Load­Bal­ancer” type service that forwards external requests to internal services. This way, ap­pli­ca­tions can be made reliably and scalably ac­ces­si­ble.

IONOS Cloud Managed Ku­ber­netes
Container workloads in expert hands

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

What is a load balancer in Ku­ber­netes?

Load balancers dis­trib­ute the workload across servers or virtual machines as ef­fi­cient­ly as possible, helping to boost the overall per­for­mance of the system. Po­si­tioned in front of the servers, a load balancer prevents in­di­vid­ual 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­gent­ly redi­rect­ing requests.

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

  • Internal Ku­ber­netes load balancers
  • External Ku­ber­netes load balancers

Internal Ku­ber­netes load balancers

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

External Ku­ber­netes load balancers

External load balancers assign a specific IP address or a DNS name to a service node of a Ku­ber­netes cluster, allowing it to receive external HTTP requests. The “Load­Bal­ancer” is a special Ku­ber­netes service type designed to forward external traffic to in­di­vid­ual Ku­ber­netes pods within the cluster, ensuring an optimal dis­tri­b­u­tion of incoming requests.

Note

There are several options or al­go­rithms to configure load balancing in Ku­ber­netes. The one you choose depends entirely on your in­di­vid­ual needs. The different al­go­rithms primarily determine the principle by which the load balancer processes incoming traffic.

How does a load balancer work?

In Ku­ber­netes, a load balancer takes on the central task of ef­fi­cient­ly dis­trib­ut­ing network traffic across multiple instances of a service, namely the pods. The goal is to ensure balanced uti­liza­tion, increase avail­abil­i­ty, and com­pen­sate for the failure of in­di­vid­ual com­po­nents.

Tech­ni­cal­ly, the load balancer receives incoming requests and checks which pods are currently available and efficient. Ku­ber­netes uses con­tin­u­ous internal mon­i­tor­ing for this purpose: Pods that are faulty or over­loaded are au­to­mat­i­cal­ly excluded from routing. The load balancer then dy­nam­i­cal­ly decides which pod to forward each request to.

This dis­tri­b­u­tion is based on various criteria. Users are unaware of this process. The ap­pli­ca­tion remains ac­ces­si­ble and high per­form­ing, even when in­di­vid­ual pods start, are re­de­ployed, 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 Ku­ber­netes cluster, shown in yellow.

What is a Ku­ber­netes load balancer for?

A Ku­ber­netes load balancer defines a service running within the cluster that is ac­ces­si­ble over the public internet. To un­der­stand this, it’s helpful to look at the Ku­ber­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 Ku­ber­netes service is typically required. Besides “Load­Bal­ancer,” there are other service types suitable for various scenarios. All service types share the char­ac­ter­is­tic of grouping a set of pods into a logical unit and de­scrib­ing how they can be accessed.

Optimal dis­tri­b­u­tion of external traffic

A Ku­ber­netes load balancer is designed to ensure optimal dis­tri­b­u­tion of external traffic to the pods in your Ku­ber­netes cluster. This makes these services suitable for virtually any use case. Since Ku­ber­netes load balancers can direct traffic specif­i­cal­ly to in­di­vid­ual pods, high avail­abil­i­ty 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­ti­miz­ing scal­a­bil­i­ty

Scal­a­bil­i­ty is also pos­i­tive­ly impacted by the use of load balancing. Ku­ber­netes can au­to­mat­i­cal­ly create or delete pods as needed. Thus, if it is de­ter­mined that incoming traffic requires more or fewer resources than currently available, Ku­ber­netes can au­to­mat­i­cal­ly respond to this situation.

How to create a load balancer for Ku­ber­netes

To create a Ku­ber­netes load balancer, your cluster must run in a cloud or an en­vi­ron­ment that supports the con­fig­u­ra­tion of external load balancers.

At IONOS, a static IP is assigned to a node in the cluster when a Ku­ber­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­gent­ly dis­trib­utes incoming traffic to the in­di­vid­ual pods.

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

type: LoadBalancer

For example, the con­fig­u­ra­tion of a Ku­ber­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­vid­ual 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 Ku­ber­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