How to Configure Load Balancing in Kubernetes

Updated: January 31, 2024 By: Guest Contributor Post a comment

Introduction

Load balancing is a critical aspect of deploying scalable, high-availability applications. When working with Kubernetes, configuring load balancing efficiently is essential to ensure that your applications can handle the incoming traffic evenly and that no single pod or node becomes overwhelmed. In this tutorial, we will explore how to set up load balancing in Kubernetes, leveraging its built-in tools and resources.

Understanding Kubernetes Load Balancing

Before diving into the configuration process, it’s essential to comprehend how Kubernetes handles load balancing. Kubernetes provides a built-in system to distribute network traffic to your pods using services. A Service in Kubernetes is an abstraction which defines a logical set of pods and a policy by which to access them. This abstraction enables load balancing and will often serve as the entry point for external traffic.

Kubernetes Services support different types ClusterIP, NodePort, and LoadBalancer. For external load balancing, the LoadBalancer service type integrates with various cloud providers’ load balancers such as AWS ELB, Azure Load Balancer, or Google Cloud Load Balancer.

Prerequisites

  • A running Kubernetes cluster
  • kubectl command-line tool, configured to communicate with your cluster
  • A basic understanding of core Kubernetes concepts, particularly pods and services

Example Application Deployment

Firstly, let’s deploy a sample application which we will then configure to balance the load across its replicas. We will deploy a basic HTTP server using the following deployment manifest:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: example-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: example-app
  template:
    metadata:
      labels:
        app: example-app
    spec:
      containers:
      - name: http-server
        image: hashicorp/http-echo
        args:
          - "-text=hello world"
          - "-listen=:8080"
        ports:
        - containerPort: 8080

Apply this deployment with kubectl apply -f deployment.yaml. Now you have three replicas of your application running in your Kubernetes cluster.

Setting Up a ClusterIP Service

By default, a Service in Kubernetes is type ClusterIP which is only accessible within the cluster. To create a ClusterIP service for the example application, use the following manifest:

apiVersion: v1
kind: Service
metadata:
  name: example-service
spec:
  selector:
    app: example-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: ClusterIP

After applying this service manifest with kubectl apply -f service.yaml, the ClusterIP service will load balance internal traffic across the three replicas of your application.

Creating a NodePort Service

A NodePort service exposes your application on a port across each of your nodes. This allows external traffic but it does not provide actual load balancing. Here is an example NodePort service:

apiVersion: v1
kind: Service
metadata:
  name: example-nodeport-service
spec:
  selector:
    app: example-app
  type: NodePort
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8080
    nodePort: 30007

This service will be accessible externally using any node’s IP address and the nodePort specified in the manifest, which in this case is 30007.

LoadBalancer Service

Configuring the LoadBalancer Service is the most straightforward way to achieve load balancing with external traffic when using cloud providers. Here’s how you can set it up for our example application:

apiVersion: v1
kind: Service
metadata:
  name: example-loadbalancer-service
spec:
  selector:
    app: example-app
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8080
  type: LoadBalancer

When you apply this manifest using kubectl apply -f loadbalancer-service.yaml, your cloud provider will provision a load balancer that routes external traffic to your nodes, then to the correct pod.

Ingress Controllers and Resources

For more advanced load balancing, you might want to use an Ingress Controller and Ingress Resources. An Ingress is an API object that manages external access to the services in a cluster, typically HTTP. Ingress can provide load balancing, SSL termination, and name-based virtual hosting.

You first need to deploy an Ingress Controller, like nginx or traefik. You can find respective installation guides in official documentation or third-party tutorials.

Here is an example of an Ingress resource that would work with the nginx Ingress Controller:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: example-service
            port:
              number: 80

This ingress will route traffic from example.com to the appropriate service and pods.

Conclusion

In this tutorial, you have learned about the different methods of load balancing in Kubernetes, ranging from basic ClusterIP services to advanced Ingress resources. By configuring these features in your Kubernetes cluster, you’ll be able to deploy resilient and high-availability applications that can handle significant traffic loads and provide a seamless user experience.

Please note that cloud-provider specific steps such as the LoadBalancer setup can vary, and you should refer to the documentation provided by your cloud provider for specific instructions.