LoadBalancer in Kind using MetalLB

Hardik PatelHardik Patel
2 min read

Kind (Kubernetes IN Docker) does not support LoadBalancer services by default because it runs inside Docker, and there is no cloud provider to provision external load balancers. However, you can simulate a LoadBalancer using MetalLB. Here’s how you can deploy an example application with a LoadBalancer service in a Kind cluster.


Step 1: Create a Kind Cluster

Create a Kind cluster with an extra port mapping to expose the load balancer to your host.

# kind-cluster.yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
  - role: control-plane
    extraPortMappings:
      - containerPort: 80
        hostPort: 8080
      - containerPort: 443
        hostPort: 8443

Create the cluster:

kind create cluster --config kind-cluster.yaml

Step 2: Install MetalLB

MetalLB allows us to assign external IPs to services of type LoadBalancer.

kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/main/config/manifests/metallb-native.yaml

Wait for MetalLB pods to be ready:

kubectl wait --namespace metallb-system --for=condition=available deployment --all --timeout=120s

Step 3: Configure MetalLB IP Address Pool

Find your Docker network:

docker network inspect -f '{{.IPAM.Config}}' kind

It should return something like [{192.168.49.0/24}], meaning available IPs are in 192.168.49.x.

(Note: You can ask ChatGPT which range to choose based on your output cause first 256 IP addresses are used by kind)

Create a MetalLB IP pool (metallb-config.yaml):

apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
  name: kind-ip-pool
  namespace: metallb-system
spec:
  addresses:
    - 192.168.49.100-192.168.49.200  # Adjust this range based on your kind network
---
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
  name: kind-advertisement
  namespace: metallb-system

Apply the configuration:

kubectl apply -f metallb-config.yaml

Step 4: Deploy Example Application

Create a simple Nginx deployment (app.yaml):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx
          ports:
            - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer

Apply the deployment and service:

kubectl apply -f app.yaml

Step 5: Get LoadBalancer IP

kubectl get svc nginx-service

Example output:

NAME            TYPE           CLUSTER-IP      EXTERNAL-IP      PORT(S)        AGE
nginx-service  LoadBalancer   10.96.211.153   192.168.49.100   80:30918/TCP   5m

Now, you can access your application using the EXTERNAL-IP:

curl http://192.168.49.100

Or open it in a browser.


Summary

  • Created a Kind cluster with port mappings.

  • Installed MetalLB to enable LoadBalancer services.

  • Configured an IP address pool in MetalLB.

  • Deployed a simple Nginx application.

  • Exposed the application via a LoadBalancer service.

This setup makes it easy to simulate cloud-like LoadBalancer behavior in Kind! 🚀

0
Subscribe to my newsletter

Read articles from Hardik Patel directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Hardik Patel
Hardik Patel

I am Tech enthusiastic from India. Learning and sharing about DevOps journey.