Skip to content

Install MetalLB

Kubernetes LoadBalancer service exposes multiple pods running a specific application on an external IP address. While Kubernetes doesn't come with its own LoadBalancer service capability, it has "plumbing" that cloud providers, such as AWS and GCP or k8s frameworks can leverage to provide proper software load balancing services.

MetalLB is one such framework. We will be using it to give our cluster the ability to provision load balanced services on an external IP of the cluster without the need to rely on commercial cloud providers.

Note

When we talk about external IPs in this context, we refer to your main home network subnet, such as 10.0.0.0/24 in our setup. If you want to expose the LoadBalancer service to the Internet, in addition to following the steps in this section, you'll need to configure port forwarding on your Internet router/modem to forward incoming traffic to the LoadBalanced IP address.

Configure MetalLB

Install MetalLB manifest

kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.11.0/manifests/namespace.yaml
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.11.0/manifests/metallb.yaml

Create MetalLB yaml config file

Network

The following metallb-config.yaml file contains information about the dynamic IP range that MetalLB will be using to assign IP addresses to the LoadBalancer services. If necessary, update the file with IP address range specific to your environment.

metallb-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  namespace: metallb-system
  name: config
data:
  config: |
    address-pools:
    - name: metallb-ip-space
      protocol: layer2
      addresses:
      - 10.0.0.180-10.0.0.199

Apply the MetalLB config

kubectl create -f metallb-config.yaml

Test MetalLB install

Create test deployment and service yaml config files

lb-test-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: lb-test
spec:
  replicas: 3
  selector:
    matchLabels:
      app: metallb-test
  template:
    metadata:
      labels:
        app: metallb-test
    spec:
      containers:
      - name: nginx
        image: nginx
lb-test-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: metallb-test
spec:
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: metallb-test
  type: LoadBalancer
  externalTrafficPolicy: Local

Important

externalTrafficPolicy: Local is required in order for LoadBalancer services to be accessible externally.

Apply the test configs

kubectl create namespace metallb-test
kubectl create -f lb-test-deployment.yaml --namespace=metallb-test
kubectl create -f lb-test-service.yaml --namespace=metallb-test

Check the service

Check the IP address that MetalLB assigned to the service

kubectl describe service metallb-test --namespace=metallb-test | grep "Assigned IP"

Access the URL of the service with your browser, i.e. http://10.0.0.180. You should see the nginx start page:

nginx