LoadBalancer
LoadBalancer is a Service type that automatically provisions an external load balancer in cloud environments. It’s the simplest way to expose Services externally in cloud Kubernetes platforms like AWS, GCP, Azure, and others. Think of LoadBalancer as a concierge service—you request external access, and the cloud provider automatically sets up everything needed to route internet traffic to your Service.
What is LoadBalancer?
A LoadBalancer Service extends NodePort and ClusterIP by automatically creating an external load balancer provided by your cloud platform. The cloud provider assigns a stable external IP address and handles all the complexity of routing traffic from the internet to your Service.
How LoadBalancer Works
When you create a LoadBalancer Service, Kubernetes:
- Creates a NodePort Service (LoadBalancer includes NodePort)
- Creates a ClusterIP Service (LoadBalancer includes ClusterIP)
- Sends request to cloud provider via the cloud controller manager
- Cloud provider provisions load balancer (e.g., AWS ELB, GCP Load Balancer, Azure LB)
- Assigns external IP address and configures routing
- Updates Service status with the external IP
LoadBalancer Service Example
Here’s a basic LoadBalancer Service:
apiVersion: v1
kind: Service
metadata:
name: web-loadbalancer
spec:
type: LoadBalancer
selector:
app: web
ports:
- port: 80
targetPort: 8080
protocol: TCP
After creation, Kubernetes will provision a cloud load balancer and assign an external IP. You can check the status:
kubectl get service web-loadbalancer
Output:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S)
web-loadbalancer LoadBalancer 10.96.0.1 203.0.113.1 80:30080/TCP
The EXTERNAL-IP shows the public IP address assigned by the cloud provider.
Cloud Provider Integration
LoadBalancer Services work differently depending on your cloud provider:
AWS (EKS)
Creates an Elastic Load Balancer (Classic, Network, or Application LB):
apiVersion: v1
kind: Service
metadata:
name: web-lb
annotations:
service.beta.kubernetes.io/aws-load-balancer-type: "nlb" # Network Load Balancer
spec:
type: LoadBalancer
selector:
app: web
ports:
- port: 80
targetPort: 8080
AWS-specific annotations:
service.beta.kubernetes.io/aws-load-balancer-type: “nlb”, “elb”, or “alb”service.beta.kubernetes.io/aws-load-balancer-ssl-cert: ARN of SSL certificateservice.beta.kubernetes.io/aws-load-balancer-backend-protocol: “http” or “https”
Google Cloud (GKE)
Creates a Google Cloud Load Balancer:
apiVersion: v1
kind: Service
metadata:
name: web-lb
annotations:
cloud.google.com/load-balancer-type: "Internal" # For internal LB
spec:
type: LoadBalancer
selector:
app: web
ports:
- port: 80
targetPort: 8080
Azure (AKS)
Creates an Azure Load Balancer:
apiVersion: v1
kind: Service
metadata:
name: web-lb
annotations:
service.beta.kubernetes.io/azure-load-balancer-internal: "true" # Internal LB
spec:
type: LoadBalancer
selector:
app: web
ports:
- port: 80
targetPort: 8080
Service Hierarchy
LoadBalancer includes all functionality of NodePort and ClusterIP:
Complete Example
Here’s a complete example with a Deployment and LoadBalancer Service:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.21
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: web-loadbalancer
spec:
type: LoadBalancer
selector:
app: web
ports:
- port: 80
targetPort: 80
protocol: TCP
After creation, access the application via the external IP:
curl http://203.0.113.1
Multiple Ports
LoadBalancer Services can expose multiple ports:
apiVersion: v1
kind: Service
metadata:
name: multi-port-lb
spec:
type: LoadBalancer
selector:
app: my-app
ports:
- name: http
port: 80
targetPort: 8080
- name: https
port: 443
targetPort: 8443
The cloud provider will configure the load balancer to handle both ports.
External IP Assignment
The external IP is assigned by the cloud provider and appears in the Service status:
kubectl get service web-loadbalancer -o yaml
status:
loadBalancer:
ingress:
- ip: 203.0.113.1 # External IP assigned by cloud provider
Note: The external IP may take a few minutes to be assigned, especially on first creation.
LoadBalancer Status
You can monitor LoadBalancer provisioning:
# Check Service status
kubectl get service web-loadbalancer
# Watch for external IP assignment
kubectl get service web-loadbalancer -w
# Describe Service for detailed status
kubectl describe service web-loadbalancer
Common status messages:
<pending>- Load balancer is being provisioned- IP address - Load balancer is ready
- Error messages - Provisioning failed (check cloud provider logs)
When to Use LoadBalancer
Use LoadBalancer when:
✅ Cloud environments - Running on AWS, GCP, Azure, or other supported clouds
✅ Simple external access - Need straightforward internet access to Services
✅ Stable external IP - Require a stable, public IP address
✅ TCP/UDP services - Exposing non-HTTP services (databases, custom protocols)
✅ Production workloads - Production services that need reliable external access
✅ Quick setup - Want automatic load balancer provisioning
Don’t use LoadBalancer when:
- Running on bare metal or unsupported platforms
- You need HTTP/HTTPS routing features (use Ingress instead)
- Cost is a concern (each LoadBalancer has cloud costs)
- You need path-based or host-based routing (use Ingress)
- You want SSL termination with automatic certificates (use Ingress)
LoadBalancer vs Ingress
Use LoadBalancer for:
- Non-HTTP services (databases, custom TCP/UDP)
- Simple HTTP services that don’t need routing
- Services that need dedicated external IPs
Use Ingress for:
- HTTP/HTTPS web applications
- Multiple services sharing one IP
- Path-based or host-based routing
- SSL/TLS termination with automatic certificates
Cost Considerations
LoadBalancer Services create cloud resources that incur costs:
- AWS: ELB charges per hour + data transfer
- GCP: Load balancer charges per hour + forwarding rules
- Azure: Load balancer charges + data processing
Cost optimization:
- Use Ingress for HTTP/HTTPS (one load balancer for many services)
- Use ClusterIP for internal-only services
- Consider internal LoadBalancers for private access
- Monitor and delete unused LoadBalancers
Internal Load Balancers
Some cloud providers support internal (private) load balancers:
AWS (Internal NLB)
apiVersion: v1
kind: Service
metadata:
name: internal-lb
annotations:
service.beta.kubernetes.io/aws-load-balancer-type: "nlb"
service.beta.kubernetes.io/aws-load-balancer-internal: "true"
spec:
type: LoadBalancer
selector:
app: web
ports:
- port: 80
targetPort: 8080
GCP (Internal LB)
apiVersion: v1
kind: Service
metadata:
name: internal-lb
annotations:
cloud.google.com/load-balancer-type: "Internal"
spec:
type: LoadBalancer
selector:
app: web
ports:
- port: 80
targetPort: 8080
Internal load balancers provide:
- Private IP addresses (not accessible from internet)
- Lower cost than external load balancers
- Better security for internal services
- VPC-only access
Best Practices
- Use for production - LoadBalancer is ideal for production external access
- Monitor costs - Track LoadBalancer usage and costs
- Use Ingress for HTTP - Prefer Ingress for HTTP/HTTPS web traffic
- Specify annotations - Use cloud-specific annotations for configuration
- Clean up unused Services - Delete LoadBalancer Services when not needed
- Use internal LBs - Use internal load balancers for private access
- Document external IPs - Keep track of assigned external IPs
- Set up monitoring - Monitor load balancer health and performance
- Consider DNS - Point DNS records to LoadBalancer external IPs
- Review security - Ensure proper security groups/firewall rules
Troubleshooting
External IP Stuck in Pending
- Check cloud controller manager:
kubectl get pods -n kube-system | grep cloud-controller - Verify cloud provider integration: Ensure cloud controller manager is running
- Check Service events:
kubectl describe service <service-name> - Verify IAM permissions: Cloud controller needs permissions to create load balancers
- Check cloud provider quotas: Ensure you haven’t exceeded load balancer limits
- Review cloud provider logs: Check cloud controller manager logs
Cannot Access External IP
- Verify IP assigned:
kubectl get service <service-name> - Check security groups: Ensure firewall rules allow traffic
- Test from inside cluster: Verify Service works via ClusterIP
- Check load balancer status: Review cloud provider console
- Verify Endpoints:
kubectl get endpoints <service-name> - Test DNS resolution: Ensure DNS points to correct IP
Load Balancer Not Provisioning
- Cloud provider support: Verify your cloud provider supports LoadBalancer
- Controller logs:
kubectl logs -n kube-system <cloud-controller-pod> - API permissions: Check cloud provider API permissions
- Resource quotas: Verify cloud provider resource limits
- Network configuration: Check VPC/subnet configuration
High Costs
- Audit LoadBalancers: List all LoadBalancer Services
- Use Ingress: Consolidate HTTP services behind Ingress
- Delete unused Services: Remove Services that are no longer needed
- Use internal LBs: Use internal load balancers when possible
- Monitor usage: Set up cost alerts for load balancer usage
See Also
- Services Overview - Introduction to Kubernetes Services
- ClusterIP - Internal-only Services
- NodePort - Exposing Services via node IPs
- Ingress - HTTP/HTTPS routing (often better for web apps)
- Gateway API - Modern alternative to Ingress