MetalLB

February 13, 2025    Kubernetes Load Balancer Networking Bare Metal

MetalLB: Load Balancer for Bare Metal Kubernetes

What is MetalLB?

MetalLB is a load balancer solution for Kubernetes clusters running on bare metal. It provides network load balancing functionality where cloud provider solutions like AWS ELB or GCP LB are unavailable. This document covers its key features, installation steps, and additional configurations to optimize performance in Kubernetes clusters.

Key Features of MetalLB

  • BGP and Layer 2 Modes: Supports both Border Gateway Protocol (BGP) and Layer 2 (ARP-based) load balancing.
  • Cloud-Native Integration: Works seamlessly with Kubernetes Services of type LoadBalancer.
  • Custom IP Address Pools: Allows administrators to define external IP ranges.
  • Lightweight and Simple: Easy to configure and deploy.
  • High Availability: Can be configured for redundancy in large-scale environments.
  • Supports IPv4 and IPv6: Provides flexibility for modern networking needs.

Installing MetalLB on Kubernetes

Step 1: Install MetalLB

Apply the official MetalLB manifests:

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

Step 2: Configure MetalLB

Create a ConfigMap defining the address pool:

apiVersion: v1
kind: ConfigMap
metadata:
  namespace: metallb-system
  name: config
data:
  config: |
    address-pools:
    - name: default-pool
      protocol: layer2
      addresses:
      - 192.168.1.100-192.168.1.200

Step 3: Verify Installation

kubectl get pods -n metallb-system

Exposing a Service with MetalLB

Step 1: Create a LoadBalancer Service

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

Step 2: Check the Assigned External IP

kubectl get svc my-service

The external IP should be assigned from the configured range.

Advanced Configuration Options

Enabling BGP Mode

For a more robust networking setup, BGP mode can be configured to integrate with external routers:

apiVersion: metallb.io/v1beta1
kind: BGPPeer
metadata:
  name: peer1
  namespace: metallb-system
spec:
  myASN: 64512
  peerASN: 64513
  peerAddress: 192.168.1.1

Apply the configuration:

kubectl apply -f bgp-config.yaml

Using MetalLB with Ingress Controllers

MetalLB can work alongside Kubernetes ingress controllers (such as Nginx or Traefik) to expose applications with a public-facing IP:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: my-service
            port:
              number: 80

Troubleshooting MetalLB Issues

Checking Logs

If MetalLB is not working as expected, check the logs of the MetalLB controller:

kubectl logs -n metallb-system deploy/controller

Debugging IP Allocation

If services are not getting an external IP, verify the allocated IP pool:

kubectl describe configmap config -n metallb-system

Resolving Port Conflicts

Ensure that no other services are using the same ports by running:

netstat -tulnp | grep 80

Conclusion

MetalLB enables Kubernetes clusters running on bare metal to leverage external load balancing without cloud provider dependencies. By following these steps, you can set up MetalLB to handle Kubernetes LoadBalancer services efficiently, integrate it with ingress controllers, and configure BGP for advanced networking needs.