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:
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:
kubectl apply -f deployment.yaml
Step 2: Create a LoadBalancer Service
Now create a LoadBalancer Service to expose the application.
service.yaml:
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:
kubectl apply -f service.yaml
Check the status of the Service:
kubectl get svc
Example output:
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:
curl http://34.89.120.22
Output:
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:
kubectl scale deployment hello-world-deployment --replicas=10
Verify the pods:
kubectl get pods
You will notice that traffic is now balanced across all 10 pods.
Advantages of LoadBalancer Service
- Automatic Load Balancing: Distributes traffic to healthy pods.
- External IP Address: Provides a stable IP for external access.
- Failover: Automatically redirects traffic if a pod fails.
- Ease of Use: Simple to set up in cloud environments.
Disadvantages of LoadBalancer Service
- Cloud Dependency: Heavily dependent on cloud providers for external IP allocation.
- Cost: Incurs additional cost in cloud environments.
- Limited Control: Limited control over the Load Balancer’s configuration.