Installing MetalLB Load Balancer

This tutorial covers the installation of MetalLB load balancer on k0s. k0s doesn’t come with an in-built load balancer, but it’s easy to deploy MetalLB as shown in this document.

About Load Balancers

Load balancers can be used for exposing applications to the external network. Load balancer provides a single IP address to route incoming requests to your app. In order to successfully create Kubernetes services of type LoadBalancer, you need to have the load balancer (implementation) available for Kubernetes.

Load balancer can be implemented by a cloud provider as an external service (with additional cost). This can also be implemented internally in the Kubernetes cluster (pure SW solution) with MetalLB.

MetalLB

MetalLB implements the Kubernetes service of type LoadBalancer. When a LoadBalancer service is requested, MetalLB allocates an IP address from the configured range and makes the network aware that the IP “lives” in the cluster.

k0s_metallb_loadbalancer

One of the benefits of MetalLB is that you avoid all cloud provider dependencies. That’s why MetalLB is typically used for bare-metal deployments.

See the MetalLB requirements in the MetalLB’s official documentation. By default, k0s runs with Kube-Router CNI, which is compatible with MetalLB as long as you don’t use MetalLB’s BGP mode. If you are not using Kube-Router and you are using kube-proxy in IPVS mode, you need to enable strict ARP mode in kube-proxy. See MetalLB preparations.

Port 7946 (TCP & UDP) must be allowed between the nodes. In addition, before installing MetalLB, make sure there is no other software running on port 7946 on the nodes, such as docker daemon.

Install MetalLB

  1. Install MetalLB using the official manifests by MetalLB

    1. kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.10.2/manifests/namespace.yaml
    2. kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.10.2/manifests/metallb.yaml
  2. Create ConfigMap for MetalLB

    Next you need to create ConfigMap, which includes an IP address range for the load balancer. The pool of IPs must be dedicated to MetalLB’s use. You can’t reuse for example the Kubernetes node IPs or IPs controlled by other services. You can, however, use private IP addresses, for example 192.168.1.180-192.168.1.199, but then you need to take care of the routing from the external network if you need external access. In this example, we don’t need it.

    Create a YAML file accordingly, and deploy it: kubectl apply -f metallb-configmap.yaml

    1. apiVersion: v1
    2. kind: ConfigMap
    3. metadata:
    4. namespace: metallb-system
    5. name: config
    6. data:
    7. config: |
    8. address-pools:
    9. - name: default
    10. protocol: layer2
    11. addresses:
    12. - <ip-address-range-start>-<ip-address-range-stop>
  3. Deploy an example application (web server) with a load balancer

    1. apiVersion: v1
    2. kind: Namespace
    3. metadata:
    4. name: web
    5. ---
    6. apiVersion: apps/v1
    7. kind: Deployment
    8. metadata:
    9. name: web-server
    10. namespace: web
    11. spec:
    12. selector:
    13. matchLabels:
    14. app: web
    15. template:
    16. metadata:
    17. labels:
    18. app: web
    19. spec:
    20. containers:
    21. - name: httpd
    22. image: httpd:2.4.53-alpine
    23. ports:
    24. - containerPort: 80
    25. ---
    26. apiVersion: v1
    27. kind: Service
    28. metadata:
    29. name: web-server-service
    30. namespace: web
    31. spec:
    32. selector:
    33. app: web
    34. ports:
    35. - protocol: TCP
    36. port: 80
    37. targetPort: 80
    38. type: LoadBalancer
  4. Check your LoadBalancer

    Run the following command to see your LoadBalancer with the external-ip and port.

    1. kubectl get service -n web
  5. Access your example application

    If you used private IP addresses for MetalLB in the ConfigMap (in step 2), you should run the following command from the local network. Use the IP address from the previous step.

    1. curl <EXTERNAL-IP>

    If you are successful, you should see <html><body><h1>It works!</h1></body></html>.

Additional information

For more information about MetalLB installation, take a look at the official MetalLB documentation.

Alternative examples

Get load balancer using cloud provider.