Prompt
How do I set up load balancing for services in Kubernetes?
Latest observation
In Kubernetes, “load balancing” usually means one of these:
- Inside the cluster: distribute traffic across pods for a service
- Outside the cluster: expose a service to clients on the internet or private network
- Advanced HTTP routing: path/host-based routing with an Ingress or Gateway
1) Basic service load balancing with a Service
Kubernetes Services automatically load balance traffic across healthy pods that match a selector.
Example Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: app
image: nginx
ports:
- containerPort: 80
Example ClusterIP Service
apiVersion: v1
kind: Service
metadata:
name: my-app-svc
spec:
selector:
app: my-app
ports:
- port: 80
targetPort: 80
type: ClusterIP
Apply:
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
This gives you an internal virtual IP. Requests to the Service are balanced across the 3 pods.
2) Expose the service externally
Option A: LoadBalancer Service
If you’re on a cloud provider, use type: LoadBalancer:
apiVersion: v1
kind: Service
metadata:
name: my-app-lb
spec:
selector:
app: my-app
ports:
- port: 80
targetPort: 80
type: LoadBalancer
Kubernetes asks the cloud provider to provision an external load balancer and route traffic to your service.
Check the external IP:
kubectl get svc my-app-lb
Option B: NodePort
apiVersion: v1
kind: Service
metadata:
name: my-app-nodeport
spec:
selector:
app: my-app
ports:
- port: 80
targetPort: 80
nodePort: 30080
type: NodePort
Then access it via http://<node-ip>:30080.
Use this when a cloud load balancer isn’t available, though it’s less ideal for production.
3) HTTP/HTTPS routing with Ingress
If you want to route multiple services by hostname or path, use an Ingress plus an Ingress Controller.
Example Ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-app-ingress
spec:
rules:
- host: app.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app-svc
port:
number: 80
You must install an Ingress Controller first, such as:
- NGINX Ingress Controller
- Traefik
- HAProxy
- cloud-specific ingress controller
Ingress is the preferred way for web apps that need host/path routing and TLS termination.
4) Internal-only load balancing
If you want an internal load balancer only, many clouds support annotations on a LoadBalancer Service.
Example for AWS:
apiVersion: v1
kind: Service
metadata:
name: internal-lb
annotations:
service.beta.kubernetes.io/aws-load-balancer-internal: "true"
spec:
selector:
app: my-app
ports:
- port: 80
targetPort: 80
type: LoadBalancer
The exact annotation depends on your cloud provider.
5) Things to verify
- Your pods are running and Ready
- The Service selector matches pod labels
- The target port matches the container port
- If using Ingress, the controller is installed
- Cloud firewall / security groups allow traffic
Useful commands:
kubectl get pods
kubectl get svc
kubectl get endpoints
kubectl describe svc my-app-svc
kubectl describe ingress my-app-ingress
6) Choosing the right approach
- Only inside cluster →
ClusterIP - Simple external access →
LoadBalancer - Bare metal / no cloud LB →
NodePortor MetalLB - Multiple web apps / routing / TLS →
Ingress - More advanced traffic management → Service Mesh or Gateway API
If you want, I can give you:
- a full working YAML example
- setup steps for AWS, GKE, AKS, or bare metal
- help debugging why your Service isn’t balancing traffic