CodeBlanks Logo Icon CodeBlanks

LoadBalancer

Learn the LoadBalancer service type.

LoadBalancer

The LoadBalancer type requests a real external load balancer from the cloud provider (AWS, GCP, Azure) and gives it a public IP. It's built on top of NodePort: the load balancer spreads traffic to the node ports.

$ kubectl get svc web
NAME   TYPE           CLUSTER-IP    EXTERNAL-IP     PORT(S)        AGE
web    LoadBalancer   10.96.45.12   34.118.22.104   80:31512/TCP   2m

Each LoadBalancer service means a separate load balancer (and cost); for several HTTP services, Ingress, a single entry point, is preferred.

LoadBalancer locally

In Minikube, while minikube tunnel runs in a separate terminal, LoadBalancer services get a local IP. On bare-metal servers, tools like MetalLB take on this job:

# terminal 1
minikube tunnel
# terminal 2
$ kubectl get svc web
NAME   TYPE           EXTERNAL-IP     PORT(S)
web    LoadBalancer   10.96.45.12     80:31512/TCP
$ curl http://10.96.45.12

Practice A LoadBalancer service

Write a Service YAML: name web, type: LoadBalancer; selector app: web; port port: 80, targetPort: 8080.

Starter code

terminal
# buraya yaz

Sample solution

terminal
apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  type: LoadBalancer
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 8080

Expected output

service/web created
NAME   TYPE           CLUSTER-IP    EXTERNAL-IP     PORT(S)        AGE
web    LoadBalancer   10.96.45.12   34.118.22.104   80:31512/TCP   40s

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Why does a LoadBalancer service's EXTERNAL-IP stay <pending> in Minikube?

  • The pod isn't ready
  • There's a port conflict
  • There's no cloud provider to provide the load balancer Correct answer
  • The YAML is wrong

Explanation: The minikube tunnel command assigns a local external IP.

2 Make the service type a cloud load balancer.

Question
spec:
  type: ____
Answer
spec:
  type: LoadBalancer

Explanation: LoadBalancer requests an external IP.

3 Match.

Term Meaning
LoadBalancer Cloud load balancer
EXTERNAL-IP Public address
minikube tunnel Local external IP
MetalLB Bare-metal solution

All lessons in Intermediate