Simplifying Local Network IP Assignment for Kubernetes with MetalLB

Learn to assign local network IPs to Kubernetes deployments using MetalLB. This step-by-step guide makes network load balancing easy, perfect for developers and IT pros seeking straightforward IP management without relying on cloud providers.

Simplifying Local Network IP Assignment for Kubernetes with MetalLB

In this article, I'll guide you through setting up MetalLB, a network load-balancer that allows you to assign local area network (LAN) IP addresses to your Kubernetes deployments. This setup is particularly useful for environments where a cloud provider's load balancer is unavailable or unnecessary.

Prerequisites

Please be aware that you must Disable servicelib and traefik when installing your master node.

export INSTALL_K3S_EXEC="--disable servicelb --disable traefik"

More Information at https://docs.k3s.io/installation/packaged-components

Install MetalLb

I'll be using MetalLB version 0.13.7. The command below will apply the MetalLB manifest to your K3s setup:

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

Creating an Address Pool

Next, we need to create an address pool. This pool should not conflict with any existing DHCP settings on your local network. Create a new file called metallb-layer2.yaml with the following content:

apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
  name: main-pool
  namespace: metallb-system
spec:
  addresses:
    - 192.168.2.200-192.168.2.245
  autoAssign: true
---
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
  name: main-ad
  namespace: metallb-system
spec:
  ipAddressPools:
    - main-pool

Applying the Address Pool Configuration

Apply the configuration with the following command:

kubectl apply -f metallb-layer2.yaml

Creating Load Balancer Manifests

After setting up the address pool, you can create load balancer manifests for other services that will request an IP within the specified range. For example, in my article on deploying a Ghost blog, there's a section that sets up a load balancer manifest. You can create a similar manifest for any service you want to use a local network address.

apiVersion: v1
kind: Service
metadata:
  name: ghost-app-lb
spec:
  selector:
    app: ghost-app
  ports:
    - port: 80
      targetPort: 2368
      name: http
      protocol: TCP
  type: LoadBalancer
  loadBalancerIP: 192.168.2.239

Conclusion

By following these steps, you'll have MetalLB configured to provide network load-balancing with local IP addresses in your Kubernetes environment. This setup is ideal for development and testing scenarios where using a cloud provider's load balancer might not be feasible.