Science and technology

Install a Kubernetes load balancer in your Raspberry Pi homelab with MetalLB

Kubernetes is designed to combine with main cloud suppliers’ load balancers to supply public IP addresses and direct site visitors right into a cluster. Some skilled community gear producers additionally supply controllers to combine their bodily load-balancing merchandise into Kubernetes installations in personal knowledge facilities. For an fanatic working a Kubernetes cluster at dwelling, nonetheless, neither of those options could be very useful.

Kubernetes doesn’t have a built-in community load-balancer implementation. A bare-metal cluster, akin to a Kubernetes cluster installed on Raspberry Pis for a private-cloud homelab, or actually any cluster deployed exterior a public cloud and missing costly skilled hardware, wants one other answer. MetalLB fulfills this area of interest, each for fans and large-scale deployments.

MetalLB is a community load balancer and may expose cluster companies on a devoted IP tackle on the community, permitting exterior purchasers to connect with companies contained in the Kubernetes cluster. It does this through both layer 2 (data link) utilizing Address Resolution Protocol (ARP) or layer 4 (transport) utilizing Border Gateway Protocol (BGP).

While Kubernetes does have one thing known as Ingress, which permits HTTP and HTTPS site visitors to be uncovered exterior the cluster, it helps solely HTTP or HTTPS site visitors, whereas MetalLB can help any community site visitors. It is extra of an apples-to-oranges comparability, nonetheless, as a result of MetalLB offers decision of an unassigned IP tackle to a specific cluster node and assigns that IP to a Service, whereas Ingress makes use of a selected IP tackle and internally routes HTTP or HTTPS site visitors to a Service or Services primarily based on routing guidelines.

MetalLB might be arrange in just some steps, works particularly effectively in personal homelab clusters, and inside Kubernetes clusters, it behaves the identical as public cloud load-balancer integrations. This is nice for training functions (i.e., studying how the expertise works) and makes it simpler to “lift-and-shift” workloads between on-premises and cloud environments.


As talked about, MetalLB works through both ARP or BGP to resolve IP addresses to particular hosts. In simplified phrases, this implies when a shopper makes an attempt to connect with a selected IP, it should ask “which host has this IP?” and the response will level it to the proper host (i.e., the host’s MAC tackle).

With ARP, the request is broadcast to your complete community, and a bunch that is aware of which MAC tackle has that IP tackle responds to the request; on this case, MetalLB’s reply directs the shopper to the proper node.

With BGP, every “peer” maintains a desk of routing info directing purchasers to the host dealing with a specific IP for IPs and the hosts the peer is aware of about, and it advertises this info to its friends. When configured for BGP, MetalLB friends every of the nodes within the cluster with the community’s router, permitting the router to direct purchasers to the proper host.

In each situations, as soon as the site visitors has arrived at a bunch, Kubernetes takes over directing the site visitors to the proper pods.

For the next train, you may use ARP. Consumer-grade routers do not (a minimum of simply) help BGP, and even higher-end shopper or skilled routers that do help BGP might be tough to arrange. ARP, particularly in a small dwelling community, might be simply as helpful and requires no configuration on the community to work. It is significantly simpler to implement.

Installing MetalLB is simple. Download or copy two manifests from MetalLB’s GitHub repository and apply them to Kubernetes. These two manifests create the namespace MetalLB’s elements can be deployed to and the elements themselves: the MetalLB controller, a “speaker” daemonset, and repair accounts.

Install the elements

Once you create the elements, a random secret is generated to permit encrypted communication between the audio system (i.e., the elements that “speak” the protocol to make companies reachable).

(Note: These steps are additionally out there on MetalLB’s web site.)

The two manifests with the required MetalLB elements are:

They might be downloaded and utilized to the Kubernetes cluster utilizing the kubectl apply command, both regionally or straight from the online:

# Verify the contents of the information, then obtain and pipe then to kubectl with curl
# (output omitted)
$ kubectl apply -f
$ kubectl apply -f

After making use of the manifests, create a random Kubernetes secret for the audio system to make use of for encrypted communications:

# Create a secret for encrypted speaker communications
$ kubectl create secret generic -n metallb-system memberlist --from-literal=secretkey="$(openssl rand -base64 128)"

Completing the steps above will create and begin all of the MetalLB elements, however they won’t do something till they’re configured. To configure MetalLB, create a configMap that describes the pool of IP addresses the load balancer will use.

Configure the tackle swimming pools

MetalLB wants one final little bit of setup: a configMap with particulars of the addresses it may possibly assign to the Kubernetes Service LoadBalancers. However, there’s a small consideration. The addresses in use don’t should be certain to particular hosts within the community, however they have to be free for MetalLB to make use of and never be assigned to different hosts.

In my dwelling community, IP addresses are assigned by the DHCP server my router is working. This DHCP server mustn’t try to assign the addresses that MetalLB will use. Most shopper routers permit you to resolve how massive your subnet can be and might be configured to assign solely a subset of IPs in that subnet to hosts through DHCP.

In my community, I’m utilizing the subnet, and I made a decision to offer half the IPs to MetalLB. The first half of the subnet consists of IP addresses from to This vary might be represented by a /25 subnet: The second half of the subnet can equally be represented by a /25 subnet: Each half comprises 126 IPs—greater than sufficient for the hosts and Kubernetes companies. Make positive to resolve on subnets acceptable to your personal community and configure your router and MetalLB appropriately.

After configuring the router to disregard addresses within the subnet (or no matter subnet you might be utilizing), create a configMap to inform MetalLB to make use of that pool of addresses:

# Create the config map
$ cat <<EOF | kubectl create -f -
apiVersion: v1
variety: ConfigMap
  namespace: metallb-system
  title: config
  config: |
    - title: address-pool-1
      protocol: layer2

The instance configMap above makes use of CIDR notation, however the record of addresses may also be specified as a variety:


Once the configMap is created, MetalLB can be lively. Time to strive it out!

You can take a look at the brand new MetalLB configuration by creating an instance net service, and you need to use one from a previous article on this collection: Kube Verify. Use the identical picture to check that MetalLB is working as anticipated: This picture comprises an Nginx server listening for requests on port 8080. You can view the Containerfile used to create the picture. If you need, you’ll be able to as a substitute construct your personal container picture from the Containerfile and use that for testing.

If you beforehand created a Kubernetes cluster on Raspberry Pis, you could have already got a Kube Verify service working and may skip to the section on making a LoadBalancer-type of service.

If you should create a kube-verify namespace

If you don’t have already got a kube-verify namespace, create one with the kubectl command:

# Create a brand new namespace
$ kubectl create namespace kube-verify
# List the namespaces
$ kubectl get namespaces
NAME              STATUS   AGE
default           Active   63m
kube-node-lease   Active   63m
kube-public       Active   63m
kube-system       Active   63m
metallb-system    Active   21m
kube-verify       Active   19s

With the namespace created, create a deployment in that namespace:

# Create a brand new deployment
$ cat <<EOF | kubectl create -f -
apiVersion: apps/v1
variety: Deployment
  title: kube-verify
  namespace: kube-verify
    app: kube-verify
  replicas: three
      app: kube-verify
        app: kube-verify
      - title: nginx
        - containerPort: 8080
deployment.apps/kube-verify created

Create a LoadBalancer-type Kubernetes service

Now expose the deployment by making a LoadBalancer-type Kubernetes service. If you have already got a service named kube-verify, this may substitute that one:

# Create a LoadBalancer service for the kube-verify deployment
cat <<EOF | kubectl apply -f -
apiVersion: v1
variety: Service
  title: kube-verify
  namespace: kube-verify
    app: kube-verify
    - protocol: TCP
      port: 80
      goalPort: 8080
  sort: LoadBalancer

You might accomplish the identical factor with the kubectl expose command:

kubectl expose deployment kube-verify -n kube-verify --type=LoadBalancer --target-port=8080 --port=80

MetalLB is listening for companies of sort LoadBalancer and instantly assigns an exterior IP (an IP chosen from the vary you chose once you arrange MetalLB). View the brand new service and the exterior IP tackle MetalLB assigned to it with the kubectl get service command:

# View the brand new kube-verify service
$ kubectl get service kube-verify -n kube-verify
NAME          TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)        AGE
kube-verify   LoadBalancer   80:31491/TCP   4m14s

# Look on the particulars of the kube-verify service
$ kubectl describe service kube-verify -n kube-verify
Name:                     kube-verify
Namespace:                kube-verify
Labels:                   app=kube-verify
Annotations:              <none>
Selector:                 app=kube-verify
Type:                     LoadBalancer
LoadBalancer Ingress:
Port:                     <unset>  80/TCP
GoalPort:               8080/TCP
NodePort:                 <unset>  31491/TCP
Endpoints:      ,,
Session Affinity:         None
External Traffic Policy:  Cluster
  Type    Reason        Age    From                Message
  ----    ------        ----   ----                -------
  Normal  IPAllocated   5m55s  metallb-controller  Assigned IP ""
  Normal  nodeAssigned  5m55s  metallb-speaker     saying from node "gooseberry"

In the output from the kubectl describe command, be aware the occasions on the backside, the place MetalLB has assigned an IP tackle (yours will differ) and is “announcing” the task from one of many nodes in your cluster (once more, yours will differ). It additionally describes the port, the exterior port you’ll be able to entry the service from (80), the goal port contained in the container (port 8080), and a node port via which the site visitors will route (31491). The finish result’s that the Nginx server working within the pods of the kube-verify service is accessible from the load-balanced IP, on port 80, from anyplace on your property community.

For instance, on my community, the service was uncovered on, and I can curl that IP from my laptop computer on the identical community:

# Verify that you simply obtain a response from Nginx on the load-balanced IP
$ curl
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

<html xmlns="" xml:lang="en" lang="en">
  <title>Test Page for the HTTP Server on Fedora</title>
(additional output omitted)

MetalLB is a good load balancer for a house Kubernetes cluster. It lets you assign actual IPs from your property community to companies working in your cluster and entry them from different hosts on your property community. These companies may even be uncovered exterior the community by port-forwarding site visitors via your property router (however please watch out with this!). MetalLB simply replicates cloud-provider-like conduct at dwelling on bare-metal computer systems, Raspberry Pi-based clusters, and even digital machines, making it straightforward to “lift-and-shift” workloads to the cloud or simply familiarize your self with how they work. Best of all, MetalLB is straightforward and handy and makes accessing the companies working in your cluster a breeze.

Have you used MetalLB, or do you utilize one other load-balancer answer? Are you primarily utilizing Nginx or HAProxy Ingress? Let me know within the feedback!

Most Popular

To Top