top of page

Configure Load Balancer using MetalLB in Bare Metal Kubernetes Cluster

Aug 6

3 min read

0

45

Maybe you've just set up a Kubernetes cluster, either on bare metal or in your home lab. Now, you're preparing to launch an app by creating a deployment and exposing it via a service. However, when you try to expose the service, you encounter an issue where the external IP remains in a pending state. Consequently, you cannot access your app from outside the cluster.

Configure Load Balancer using MetalLB in Bare Metal Kubernetes Cluster
Kubernetes Service External IP in Pending State

This is frustrating. In a public cloud, once the service is exposed, the platform automatically manages the creation and assignment of an external IP address via its load balancer. However, in a bare metal setup or home lab, there isn't a load balancer available. As a result, you must implement a load balancer in your Kubernetes setup using MetalLB. In this article, we will explain step by step how to configure MetalLB in a bare metal Kubernetes cluster to make your service accessible externally.


Step 1: Configure Kubernetes Cluster


Enable strict ARP mode by setting it to True. ARP is short for Address Resolution Protocol. When set to True, one of the speaker pods is designated as the leader pod. This pod receives traffic directed to the load balancer and manages traffic redirection to your app using iptables.

kubectl edit configmap -n kube-system kube-proxy

apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: "ipvs"
ipvs:
  strictARP: true

Configure Load Balancer using MetalLB in Bare Metal Kubernetes Cluster
Kubernetes KubeProxy Configuration strictARP: true

Step 2: Install MetalLB

Once youve setup your KubeProxyConfiguration. Let us install MetalLB using Manifest. As of this writing, the latest version of MetalLB is v0.15.2. You may change the version with your preferred release.


Run this command to install MetalLB

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

To check that your installation was successful. Ensure that the following resources are created. A namespace "metallb-system", and pods "controller, and speaker".


Run this command to check for the namespace

kubectl get namespaces

Configure Load Balancer using MetalLB in Bare Metal Kubernetes Cluster
kubectl get namespaces

Run this command to check for the pods

kubectl get pods --namespace=metallb-system

Configure Load Balancer using MetalLB in Bare Metal Kubernetes Cluster


It is safe to say that your installation was successful when you see the controller and speaker Pods in Running STATUS.


Step 3: Configure MetalLB


To set up MetalLB, we must configure an Address Pool and Layer 2 Advertisement. The Address Pool determines the range of IP addresses available for the LoadBalancer. Layer 2 Advertisement enables MetalLB to broadcast the LoadBalancer IPs from the selected pools using L2. You can refer to this post on GitHub.

.


Setup Address Pool

Using your editor of choice, create a file named address. Put the following configuration. Note: The addresses are your available IP range in your network. Since I am running a home lab setup. This is my available IP range in my router.


Create the addresspool.yaml file

vi addresspool.yaml
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
  name: default-pool
  namespace: metallb-system
spec:
   addresses:
   - 192.168.100.128/27

Generate the Address Pool using Kubectl

kubectl apply -f addresspool.yaml

You will see something like this if you have successfully created your address pool.

Configure Load Balancer using MetalLB in Bare Metal Kubernetes Cluster
Generate the Address Pool using Kubectl


Setup Layer 2 Advertisement


Create the adverisement.yaml file

vi advertisement.yaml
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
  name: default-pool-advertisement
  namespace: metallb-system
spec:
  ipAddressPools:
  - default-poo

Generate the Layer 2 Advertisement Pool using Kubectl

kubectl apply -f advertisement.yaml
Configure Load Balancer using MetalLB in Bare Metal Kubernetes Cluster
Generate the Layer 2 Advertisement Pool using Kubectl

Step 4: Test MetalLB by creating Service of type LoadBalancer


Create a sample Deployment

kubectl create deployment webserver --image=httpd --replicas=2 --port=80
Configure Load Balancer using MetalLB in Bare Metal Kubernetes Cluster
kubectl create deployment

Expose the Deployment using Service

kubectl expose deployment webserver --port=8000 --target-port=80 --type=LoadBalancer
Configure Load Balancer using MetalLB in Bare Metal Kubernetes Cluster
Expose the Deployment using Service

Check that the Service is running and with External IP Address. As you can see, an external IP address of 192.168.100.128 has been assigned to our Service named web server. If the status of external IP Address is Pending. There is something wrong with the setup. Check and revisit your configuration.

Configure Load Balancer using MetalLB in Bare Metal Kubernetes Cluster


Configure Load Balancer using MetalLB in Bare Metal Kubernetes Cluster
Test MetalLB LoadBalancer in Browser


If you need detailed tutorial in creating Deployments and Services in Kubernetes. I have a separate blog that can be found in this page for further reading.


Teodoro Rico III(Perry):

Teodoro Rico III (Perry) is a technology executive from the Philippines, currently working as a First Vice-President in the financial sector. He is a Cloud Architect, Cloud Infrastructure, and DevSecOps expert, proficient in both strategy, business case, and hands-on experience. 

bottom of page