Part of series: Kubernetes
5. Load Balancer (MetalLB)
MetalLB allows the use of LoadBalancer type services in a bare-metal environment like our K3s cluster.
5.1 Install MetalLB
# Create namespace
sudo kubectl create namespace metallb-system --kubeconfig /etc/rancher/k3s/k3s.yaml
# Install MetalLB using Helm
sudo helm repo add metallb https://metallb.github.io/metallb --kubeconfig /etc/rancher/k3s/k3s.yaml
sudo helm repo update --kubeconfig /etc/rancher/k3s/k3s.yaml
sudo helm install metallb metallb/metallb --namespace metallb-system --kubeconfig /etc/rancher/k3s/k3s.yaml
5.2 Configure the IP address pool
Create an ipaddresspool.yaml file with the following content (adjust the IP range according to your network):
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
name: first-pool
namespace: metallb-system
spec:
addresses:
- 192.168.1.200-192.168.1.220
Apply the configuration:
sudo kubectl apply -f ipaddresspool.yaml --kubeconfig /etc/rancher/k3s/k3s.yaml
5.3 Configure the advertisement mode
Create an l2advertisement.yaml file with the following content:
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
name: l2-advert
namespace: metallb-system
spec:
ipAddressPools:
- first-pool
Apply the configuration:
sudo kubectl apply -f l2advertisement.yaml --kubeconfig /etc/rancher/k3s/k3s.yaml
5.4 Test MetalLB
Create a test application:
# Create an NGINX deployment
sudo kubectl create deployment nginx --image=nginx --kubeconfig /etc/rancher/k3s/k3s.yaml
# Expose the deployment as a LoadBalancer type service
sudo kubectl expose deployment nginx --port=80 --type=LoadBalancer --kubeconfig /etc/rancher/k3s/k3s.yaml
# Verify that an external IP has been assigned
sudo kubectl get svc nginx --kubeconfig /etc/rancher/k3s/k3s.yaml
You should see an external IP address assigned from the pool you configured (192.168.1.200-192.168.1.220). You can access NGINX using this IP from any machine on your network.
You can also see which IP it is running on from OpenLens:
