Skip to main content

Lab 4.4 - LoadBalancer and Cloud Integration

Lab Objectives

By the end of this lab, you will be able to:

  • Understand the LoadBalancer Service type.
  • Configure a LoadBalancer Service.
  • Understand integration with cloud load balancers.
  • Test external access via LoadBalancer.

Estimated Duration

30-45 minutes

Prerequisites

  • kubectl installed and configured.
  • Functional local Kubernetes cluster (minikube or kind).
  • Knowledge of Services (Chapters 4.1 and 4.2).

Part 1: Understanding LoadBalancer

A LoadBalancer Service type exposes the Service externally using a load balancer provided by the cloud provider (AWS ELB, Azure Load Balancer, GCP Load Balancer).

On a local cluster (minikube/kind), the LoadBalancer behaves like a NodePort, but in cloud production, it automatically provisions an external load balancer.


Part 2: Creating a LoadBalancer Service

Step 2.1: Deploy an Application

Create a simple Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
spec:
replicas: 3
selector:
matchLabels:
app: web-app
template:
metadata:
labels:
app: web-app
spec:
containers:
- name: nginx
image: nginx:1.20
ports:
- containerPort: 80

Apply:

kubectl apply -f deployment.yaml

Step 2.2: Create a LoadBalancer Service

Create service-loadbalancer.yaml:

apiVersion: v1
kind: Service
metadata:
name: web-app-lb
spec:
type: LoadBalancer
selector:
app: web-app
ports:
- port: 80
targetPort: 80
protocol: TCP

Apply:

kubectl apply -f service-loadbalancer.yaml

Part 3: Verification

Step 3.1: Verify the Service

kubectl get svc web-app-lb

On minikube, you will see <pending> for the EXTERNAL-IP. Enable the tunnel:

minikube tunnel

In another terminal, check again:

kubectl get svc web-app-lb

You should see an EXTERNAL-IP.

Step 3.2: Test Access

Test the access:

curl http://<EXTERNAL-IP>

Part 4: Cloud Integration (Conceptual)

On a cloud cluster (AWS, Azure, GCP), the LoadBalancer automatically provisions:

  • AWS: Elastic Load Balancer (ELB)
  • Azure: Azure Load Balancer
  • GCP: Google Cloud Load Balancer

The cloud provider automatically handles the creation and configuration of the load balancer.


Part 5: Cleanup

kubectl delete svc web-app-lb
kubectl delete deployment web-app

Lab Summary

In this lab, you created a LoadBalancer Service and understood how it works. On a cloud cluster, this Service type automatically provisions an external load balancer to expose your application.


Next Steps

This module on Services and Networking is now complete. You can move on to Module 5 on Configuration and Secrets.

Module 5: Configuration and Secrets


Lab created: December 2024