Skip to content

Setting Up MetalLB Load Balancer on Kubernetes

Updated: at 06:59 PM

Setting Up MetalLB Load Balancer on Kubernetes

When you run Kubernetes on a cloud provider, you get a load balancer for free. But what if you’re running Kubernetes on your own servers? That’s where MetalLB comes in. In this guide, you’ll learn how to set up MetalLB on your bare-metal Kubernetes cluster.

What Is MetalLB?

MetalLB is a load balancer you can run on bare-metal (or any non-cloud) Kubernetes cluster. It gives you an IP address on your cluster so that services can be reached from outside. MetalLB comes in two flavors:

BGP Mode

Layer 2 (L2) Mode

Since most home routers do not support BGP, we’ll focus on setting up MetalLB in L2 mode.

Installing MetalLB in L2 Mode on Your Kubernetes Cluster

Prerequisites

Step-by-Step Tutorial

Step 1. Install MetalLB

Apply MetalLB’s manifest using kubectl:

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

Note: Check MetalLB releases in case you need a different version.

Next, make sure MetalLB is running:

kubectl get pods -n metallb-system

You should see a few pods (controllers and speakers) running.

Step 2. Configuring MetalLB in Layer‑2 Mode

You can create the MetalLB configuration using a here-document. Adjust the IP address range to match a free block on your home network.

kubectl create -f - <<EOF
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
  name: example
  namespace: metallb-system
spec:
  ipAddressPools:
  - name: default
    protocol: layer2
    addresses:
    - 192.168.1.240-192.168.1.250
EOF

Apply the configuration:

kubectl apply -f metallb-config.yaml

Step 3.Create a Service to Test MetalLB

Now, let’s create a simple NGINX deployment and service to test MetalLB. Both will be created with a single command.

kubectl create -f - <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - port: 80
    targetPort: 80
  type: LoadBalancer
EOF

After a short wait, check the service details:

kubectl get svc nginx-service

Wrap-up

You now have MetalLB running in Layer 2 mode on your Kubernetes cluster:

This guide kept things simple. You can now use MetalLB to expose your applications outside your Kubernetes cluster while working within the limits of a home network. Enjoy your Kubernetes journey!


Previous Post
Setting up NFS Storage Class in Kubernetes with a Helm Chart
Next Post
How to setup adguard home to block ads in your home network