0
0
Kubernetesdevops~5 mins

LoadBalancer service type in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want your app to be reachable from outside your cluster. The LoadBalancer service type creates a network load balancer that routes external traffic to your app automatically.
When you want to expose a web app running in Kubernetes to the internet with a stable IP address.
When you need to distribute incoming traffic evenly across multiple app instances.
When you want cloud provider integration to automatically create a load balancer for your service.
When you want to avoid manually configuring external routers or firewalls.
When you want a simple way to get external access without complex networking setup.
Config File - loadbalancer-service.yaml
loadbalancer-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: example-loadbalancer
spec:
  type: LoadBalancer
  selector:
    app: example-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080

This file defines a Service named example-loadbalancer that exposes pods with label app: example-app.

The type: LoadBalancer tells Kubernetes to create an external load balancer.

The service listens on port 80 and forwards traffic to port 8080 on the pods.

Commands
This command creates the LoadBalancer service in your Kubernetes cluster using the configuration file.
Terminal
kubectl apply -f loadbalancer-service.yaml
Expected OutputExpected
service/example-loadbalancer created
This command checks the status of the LoadBalancer service and shows its external IP once assigned.
Terminal
kubectl get services example-loadbalancer
Expected OutputExpected
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE example-loadbalancer LoadBalancer 10.96.123.45 34.123.45.67 80:31234/TCP 10s
This command shows detailed information about the LoadBalancer service, including endpoints and events.
Terminal
kubectl describe service example-loadbalancer
Expected OutputExpected
Name: example-loadbalancer Namespace: default Labels: <none> Annotations: <none> Selector: app=example-app Type: LoadBalancer IP: 10.96.123.45 LoadBalancer Ingress: 34.123.45.67 Port: <unset> 80/TCP TargetPort: 8080/TCP Endpoints: 192.168.1.10:8080,192.168.1.11:8080 Session Affinity: None Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal EnsuringLoadBalancer 10s service-controller Ensuring load balancer Normal EnsuredLoadBalancer 5s service-controller Ensured load balancer
Key Concept

If you remember nothing else from this pattern, remember: the LoadBalancer service type automatically creates an external IP to route traffic to your app pods.

Common Mistakes
Using LoadBalancer service type on a local Kubernetes cluster without cloud provider support.
Local clusters like minikube or kind do not support automatic external load balancers, so the EXTERNAL-IP stays pending.
Use NodePort or port forwarding for local clusters, or use a cloud Kubernetes service for LoadBalancer support.
Not labeling pods with the selector labels defined in the service.
The service cannot find any pods to route traffic to, so no endpoints are available.
Ensure pods have the correct labels matching the service selector.
Summary
Create a LoadBalancer service YAML file to expose your app externally.
Apply the service with kubectl apply to create it in the cluster.
Check the service status and external IP with kubectl get services.
Use kubectl describe to see detailed info and troubleshoot.