[Updated] How to install MetalLB in your Self hosted K8s cluster.

Abhishek SinghAbhishek Singh
3 min read

What is MetalLB?

MetalLB is a load-balancer that can be implemented in your self-hosted(bare metal) Kubernetes cluster two support service type Loadbalancer.

Kubernetes does not offer an implementation of network load balancers (Services of type LoadBalancer) for bare-metal clusters. The implementations of network load balancers that Kubernetes does ship with are all glue code that calls out to various IaaS platforms (GCP, AWS, Azure…). If you’re not running on a supported IaaS platform (GCP, AWS, Azure…), LoadBalancers will remain in the “pending” state indefinitely when created. Credit: Metallb documentation

Requirement:

  • Working Kubernetes cluster

  • Cluster Network configuration that will be compatible with Metallb (Calico, Flannel, etc)

  • IPv4 addresses to assign in pools.

Usage:

MetalLB connects with our Kubernetes cluster and provides a Network load-balancer implementation, that allows us to create service type LoadBalancer. That is not possible in bare metal clusters without using MetalLB.

It has two features that work together to provide this service: address allocation, and external announcement.

Address Allocation:

When you use Kubernetes on a cloud service and ask for a load balancer, the cloud service gives you an IP address. But if you're using your own hardware (called a "bare-metal cluster"), MetalLB gives you that IP address.

MetalLB can't just make up IP addresses. You have to give it a list of IPs it can use. MetalLB will then choose from this list to give out IPs as needed. But it only uses the IPs you've given it.

External Announcement:

Once MetalLB gives an External IP to a service, it has to let the wider network know that this IP address is being used inside the cluster. To do this MetalLB uses common network methods. Depending on the chosen setting, it can use ARP, NDP and BGP.

Installation:

You can install MetalLB in your cluster using the following command -

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

This will deploy the MetalLB in your cluster under metallb-system namespace. After installation, you will able to see two types of resources -

  1. MetalLB controller

  2. MetalLB speaker

These resources will be created along with their service accounts and RBAC permissions.

Now installation is done let's create the IP address pool using the following manifest-

apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
  name: first-pool
  namespace: metallb-system
spec:
  addresses:
  - 192.168.1.21/32
  - 192.168.1.22/32

Apply the above manifest using the following command-

kubectl apply -f ippool.yaml -n metallb-system

Now create a dummy service with the type of LoadBalancer and see if external IP is getting assigned.

apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: LoadBalancer

apply the above manifest and check if the External IP is assigned -

kuberctl get service my-app-service

Output -

NAME            TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)        AGE
my-app-service  LoadBalancer   10.100.200.123  192.168.1.21   80:32000/TCP   3s

If External-IP status is pending then please recheck your Metallb pods if they running properly or not.

By default, it uses Layer2 mode for communication, but in case it is not communicating with your service, please consider applying the following manifest-

apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
  name: l2-firstpool
  namespace: metallb-system
spec:
  ipAddressPools:
  - first-pool

Your basic MetalLB installation and configuration done. Now you can create a network load-balancer in your cluster.

Soon I will be uploading a few more advanced article about MetalLB and cluster networking. Till then if you have any questions feel free to drop a comment or send me directly over my discord server.

Thanks

20
Subscribe to my newsletter

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

Written by

Abhishek Singh
Abhishek Singh

Hi This is Abhishek Singh, Devops Engineer by profession and Gamer by passion. I'm a devops engineer with more than 3 years of experience in Linux, Monitoring and Devops. I'm huge Open source supporter and Linux lover. In free time I love to play Minecraft and Valorant.