LoadBalancer

This guide covers how to get service of type LoadBalancer working in a kind cluster using Metallb.

This guide complements metallb installation docs, and sets up metallb using layer2 protocol. For other protocols check metallb configuration docs.

With Docker on Linux, you can send traffic directly to the loadbalancer’s external IP if the IP space is within the docker IP space.

On macOS and Windows, docker does not expose the docker network to the host. Because of this limitation, containers (including kind nodes) are only reachable from the host via port-forwards, however other containers/pods can reach other things running in docker including loadbalancers. You may want to check out the Ingress Guide as a cross-platform workaround. You can also expose pods and services using extra port mappings as shown in the extra port mappings section of the Configuration Guide.

Installing metallb using default manifests

Create the metallb namespace

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

Apply metallb manifest

  1. kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.12.1/manifests/metallb.yaml

Wait for metallb pods to have a status of Running

  1. kubectl get pods -n metallb-system --watch

Setup address pool used by loadbalancers

To complete layer2 configuration, we need to provide metallb a range of IP addresses it controls. We want this range to be on the docker kind network.

  1. docker network inspect -f '{{.IPAM.Config}}' kind

The output will contain a cidr such as 172.19.0.0/16. We want our loadbalancer IP range to come from this subclass. We can configure metallb, for instance, to use 172.19.255.200 to 172.19.255.250 by creating the configmap.

  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. - 172.19.255.200-172.19.255.250

Apply the contents

  1. kubectl apply -f https://kind.sigs.k8s.io/examples/loadbalancer/metallb-configmap.yaml

Using LoadBalancer

The following example creates a loadbalancer service that routes to two http-echo pods, one that outputs foo and the other outputs bar.

  1. kind: Pod
  2. apiVersion: v1
  3. metadata:
  4. name: foo-app
  5. labels:
  6. app: http-echo
  7. spec:
  8. containers:
  9. - name: foo-app
  10. image: hashicorp/http-echo:0.2.3
  11. args:
  12. - "-text=foo"
  13. ---
  14. kind: Pod
  15. apiVersion: v1
  16. metadata:
  17. name: bar-app
  18. labels:
  19. app: http-echo
  20. spec:
  21. containers:
  22. - name: bar-app
  23. image: hashicorp/http-echo:0.2.3
  24. args:
  25. - "-text=bar"
  26. ---
  27. kind: Service
  28. apiVersion: v1
  29. metadata:
  30. name: foo-service
  31. spec:
  32. type: LoadBalancer
  33. selector:
  34. app: http-echo
  35. ports:
  36. # Default port used by the image
  37. - port: 5678

Apply the contents

  1. kubectl apply -f https://kind.sigs.k8s.io/examples/loadbalancer/usage.yaml

Now verify that the loadbalancer works by sending traffic to it’s external IP and port.

  1. LB_IP=$(kubectl get svc/foo-service -o=jsonpath='{.status.loadBalancer.ingress[0].ip}')
  1. # should output foo and bar on separate lines
  2. for _ in {1..10}; do
  3. curl ${LB_IP}:5678
  4. done