Install MetalLB LoadBalancer

MetalLB is a baremetal loadbalancer for kubernetes clusters. It will also assign an IP Address from an IP Address pool to the service that is type LoadBalancer.

  1. Download manifests

Let’s start off by downloading the manifests from the official MetalLB github repository.

$ mkdir -p ~/k3s/metallb/
$ curl https://raw.githubusercontent.com/metallb/metallb/v0.12.1/manifests/namespace.yaml -o ~/k3s/metallb/namespace.yaml
$ curl https://raw.githubusercontent.com/metallb/metallb/v0.12.1/manifests/metallb.yaml -o ~/k3s/metallb/metallb.yaml
  1. Set IP Address pool

    1. let’s start off by creating the metallb-configmap.yaml file and then set the IP Address pool

      touch ~/k3s/metallb/metallb-configmap.yaml
      
    2. Once the file has been created, let’s edit the yaml file and input the following:

      apiVersion: v1
      kind: ConfigMap
      metadata:
          namespace: metallb-system
          name: config
      data:
          config: |
              address-pools:
              - name: default
                protocol: layer2
                addresses:
                - 192.168.122.200-192.168.122.225
      
  2. Deploy MetallB

    Alright! with all of that out of the way, let’s go and deploy MetallB using the .yaml files

    1. Let’s start off by creating the namespace for MetallB

      $ kubectl apply -f namespace.yaml
      
      namespace/metallb-system created
      
    2. Once the namespace has been applied, we can now apply the Config Map, which will set the pool of addresses to assign to services

      $ kubectl apply -f metallb-configmap.yaml
      
      onfigmap/config created
      
    3. With namespace and configmap set, let’s deploy the MetallB

      $ kubectl apply -f metallb.yaml
      
      Warning: policy/v1beta1 PodSecurityPolicy is deprecated in v1.21+, unavailable in v1.25+
      podsecuritypolicy.policy/controller configured
      podsecuritypolicy.policy/speaker configured
      serviceaccount/controller created
      serviceaccount/speaker created
      clusterrole.rbac.authorization.k8s.io/metallb-system:controller unchanged
      clusterrole.rbac.authorization.k8s.io/metallb-system:speaker unchanged
      role.rbac.authorization.k8s.io/config-watcher created
      role.rbac.authorization.k8s.io/pod-lister created
      role.rbac.authorization.k8s.io/controller created
      clusterrolebinding.rbac.authorization.k8s.io/metallb-system:controller unchanged
      clusterrolebinding.rbac.authorization.k8s.io/metallb-system:speaker unchanged
      rolebinding.rbac.authorization.k8s.io/config-watcher created
      rolebinding.rbac.authorization.k8s.io/pod-lister created
      rolebinding.rbac.authorization.k8s.io/controller created
      daemonset.apps/speaker created
      deployment.apps/controller created
      
  3. Confirm MetallB installation

    Let’s confirm MetallB has installed correctly. Everything should be running and ready like the output below:

     $ kubectl get all -n metallb-system
    
     NAME                              READY   STATUS    RESTARTS   AGE
     pod/controller-66445f859d-ltktd   1/1     Running   0          40s
     pod/speaker-stc7z                 1/1     Running   0          40s
     pod/speaker-tt9tm                 1/1     Running   0          40s
     pod/speaker-7nfth                 1/1     Running   0          40s
    
     NAME                     DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR            AGE
     daemonset.apps/speaker   3         3         3       3            3           kubernetes.io/os=linux   41s
    
     NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
     deployment.apps/controller   1/1     1            1           40s
    
     NAME                                    DESIRED   CURRENT   READY   AGE
     replicaset.apps/controller-66445f859d   1         1         1       40s
    

Congrats! you’ve deployed your first application as well as a laodbalancer!