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
- What It Does: Acts like a router that speaks the Border Gateway Protocol (BGP).
- Best For: Data centers or setups where routers, switches, or your network support BGP.
- Drawback: Home routers rarely support BGP. This makes BGP mode hard to use if you’re not running in a professional network.
Layer 2 (L2) Mode
- What It Does: Uses the local network protocols. It works like a simple ARP (Address Resolution Protocol) responder.
- Best For: Home networks and small clusters where you do not have fancy routers or switches.
- Advantage: It is easy to set up and works with your normal home router.
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
- k8s cluster
- kubectl
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
- ipAddressPools: This part tells MetalLB what IP addresses it can use to assign to your services.
- Protocol: Set to layer2 for home routers.
- Addresses: Adjust this range to a set of free IP addresses on your home network. Make sure these IP addresses aren’t in use by your router or any other device.
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:
- BGP mode is not used because most home routers do not support it.
- L2 mode works well with your simple network setup and is easy to install.
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!