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.
Apply the official MetalLB manifests:
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/main/config/manifests/metallb-native.yaml
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
kubectl get pods -n metallb-system
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
kubectl get svc my-service
The external IP should be assigned from the configured range.
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
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
If MetalLB is not working as expected, check the logs of the MetalLB controller:
kubectl logs -n metallb-system deploy/controller
If services are not getting an external IP, verify the allocated IP pool:
kubectl describe configmap config -n metallb-system
Ensure that no other services are using the same ports by running:
netstat -tulnp | grep 80
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.