Learnitweb

LoadBalancer Service in Kubernetes

Introduction

In a Kubernetes cluster, when you deploy an application, you often need it to be accessible from outside the cluster. Kubernetes provides several types of Services to expose your applications to external traffic, one of which is the LoadBalancer Service. The LoadBalancer Service is commonly used when you want to expose your application to the internet and distribute the incoming traffic across multiple pods.

What is a LoadBalancer Service?

A LoadBalancer Service in Kubernetes provides a stable external IP address and routes the incoming traffic to the backend Pods. This type of service is most commonly used in cloud environments such as AWS, Azure, and GCP where external load balancers can be provisioned.

How LoadBalancer Service Works

When you create a LoadBalancer Service in Kubernetes:

  • It automatically provisions an external Load Balancer in the cloud provider (if applicable).
  • Assigns a public IP address to the Service.
  • Forwards incoming traffic to the appropriate Pods based on the Service’s selector.
  • Ensures high availability and scalability by distributing traffic to healthy Pods.

Use Case of LoadBalancer Service

  • Exposing a web application to the internet.
  • Distributing traffic to multiple pods for high availability.
  • Providing a stable endpoint (IP address) for your application.

Creating a LoadBalancer Service

Let’s create a LoadBalancer Service for a sample application.

Step 1: Deploy an Application

First, deploy an application using a Deployment.

deployment.yaml:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world-deployment
spec:
replicas: 3
selector:
matchLabels:
app: hello-world
template:
metadata:
labels:
app: hello-world
spec:
containers:
- name: hello-world
image: hashicorp/http-echo:0.2.3
args:
- "-text=Hello World"
ports:
- containerPort: 5678
apiVersion: apps/v1 kind: Deployment metadata: name: hello-world-deployment spec: replicas: 3 selector: matchLabels: app: hello-world template: metadata: labels: app: hello-world spec: containers: - name: hello-world image: hashicorp/http-echo:0.2.3 args: - "-text=Hello World" ports: - containerPort: 5678
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: hello-world
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
      - name: hello-world
        image: hashicorp/http-echo:0.2.3
        args:
        - "-text=Hello World"
        ports:
        - containerPort: 5678

Apply the Deployment using:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
kubectl apply -f deployment.yaml
kubectl apply -f deployment.yaml
kubectl apply -f deployment.yaml

Step 2: Create a LoadBalancer Service

Now create a LoadBalancer Service to expose the application.

service.yaml:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
apiVersion: v1
kind: Service
metadata:
name: hello-world-service
spec:
type: LoadBalancer
selector:
app: hello-world
ports:
- port: 80
targetPort: 5678
apiVersion: v1 kind: Service metadata: name: hello-world-service spec: type: LoadBalancer selector: app: hello-world ports: - port: 80 targetPort: 5678
apiVersion: v1
kind: Service
metadata:
  name: hello-world-service
spec:
  type: LoadBalancer
  selector:
    app: hello-world
  ports:
  - port: 80
    targetPort: 5678

Apply the Service using:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
kubectl apply -f service.yaml
kubectl apply -f service.yaml
kubectl apply -f service.yaml

Check the status of the Service:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
kubectl get svc
kubectl get svc
kubectl get svc

Example output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-world-service LoadBalancer 10.0.1.1 34.89.120.22 80:32435/TCP 5m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE hello-world-service LoadBalancer 10.0.1.1 34.89.120.22 80:32435/TCP 5m
NAME                   TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
hello-world-service    LoadBalancer   10.0.1.1       34.89.120.22  80:32435/TCP   5m

The EXTERNAL-IP is the IP address that can be used to access your application from the internet.

Step 4: Test the Application

Access the application using the external IP:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
curl http://34.89.120.22
curl http://34.89.120.22
curl http://34.89.120.22

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Hello World
Hello World
Hello World

Step 5: Scaling the Application

If you scale the deployment to 10 replicas, the LoadBalancer will automatically distribute traffic to all the pods.

Scale the deployment:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
kubectl scale deployment hello-world-deployment --replicas=10
kubectl scale deployment hello-world-deployment --replicas=10
kubectl scale deployment hello-world-deployment --replicas=10

Verify the pods:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
kubectl get pods
kubectl get pods
kubectl get pods

You will notice that traffic is now balanced across all 10 pods.

Advantages of LoadBalancer Service

  1. Automatic Load Balancing: Distributes traffic to healthy pods.
  2. External IP Address: Provides a stable IP for external access.
  3. Failover: Automatically redirects traffic if a pod fails.
  4. Ease of Use: Simple to set up in cloud environments.

Disadvantages of LoadBalancer Service

  1. Cloud Dependency: Heavily dependent on cloud providers for external IP allocation.
  2. Cost: Incurs additional cost in cloud environments.
  3. Limited Control: Limited control over the Load Balancer’s configuration.