MetalLB (Network LoadBalancer ) & Minikube.

ShoaibMasood

--

In this article I will start with a brief introduction of Services and which types of services does Kubernetes offers. After that I will be focusing on metalLB, a third party network LoadBalancer, for bare metal cluster. And how we could install and configured it by using minikube(a single node bare metal cluster). So lets starts !!

Services In Kubernetes:

“An abstract way to expose an application running on a set of Pods as a network service”.

In Kubernetes, Service is a resource which is created to make a single and fixed point of entry to a group of pods providing the same Service. Each Service will have a Cluster-IP , name , port and also the list of back-end linked with this Service. This Service also acts as an internal loadbalancer, when the service has more than one endpoints. There are three ways to define a service. Cluster-IP , Node-Port, LoadBalancer are basically types of Service.

Service Type LoadBalancer:

Exposes the service externally using a cloud provider loadbalancer. Node-port and Cluster-IP services to which the external loadbalancer routes, are automatically created. There is a big problem occurs as the type LoadBalancer is only available for use if your K8s cluster is setup in any of the public cloud providers, GCE AWS , etc which comes with $$ :).

So first I am going to show you without load-balancing and see! whats happen, I am going to create a nginx deployment by using Kubectl run command.

Now the deployment is created, in order to access this nginx application we need to expose this as Service. So lets try create a service of type LoadBalancer by using kubectl expose command.

To view all resources in Kubernetes you can use a kubectl get all command.

As we can see a deployment has been created with a name nginx. So we haven’t got a load balancing solution in our cluster this is a bare-metal cluster so see what happens, the service nginx got created with type LoadBalancer, Cluster-IP which is accessible only within the cluster not outside the cluster and the External-IP which is the IP address of this service but it will be in the pending state forever if you wait for it to come up then you will have to wait forever because it won’t work . So we need to deploy a load balancing solution for our cluster.

MetalLB:

As a solution to above problem, MetalLB aims to redress this imbalance by offering Network LB implementation that integrates with standard network equipment , so that external service on bare metal cluster also just work as much as possible.

So let me delete that service by using kubectl delete service command. (You can also use svc for service.)

Now lets install MetalLB on our cluster…

Install MetalLB on Minikube:

To install MetalLB, as there are two parts to it one is deploying this resource in our cluster and then we have to do a configuration without which it won’t work so first simply apply this manifest. Kubectl apply -f https://raw.githubusercontent.com/google/metallb/v0.8.1/manifests/metallb.yaml

This will deploy MetalLB to your cluster, under the metallb-system name-space. To get all name space in cluster we use kubectl get ns command:

lets see what’s inside the name-space by using kubectl get all -n metallb-system command.

There are two major components.

  • The metallb-system/controller deployment. This is the cluster -wide controller that handles IP address assignments.
  • The metallb-system/speaker daemon-set. This is the component that speaks the protocols of your choice to make the services reachable.

As we can see that controller component is deployed as a deployment and speaker component as daemon set and its running on all the worker nodes because it’s a daemon set and the controller is a deployment with one replicas. So we have created the MetalLB resource, Now we need to do the configuration part.

MetalLB Layer 2 Configuration:

MetalLB remains idle until configured. This accomplished by creating and deploying a configmap into the same name space (metallb-system) as the deployment. And there are various options, the easiest solution is to use the layer 2 configuration which I’m going to be use in this article .

MetalLB contains two pieces of information, a protocol and range of IP addresses. In this configuration MetalLB is instructed to handout addresses from the 192.168.99.95/105, its our predefined range with respect to node IP. In our case to get IP of our minikube we use minikube ip command and set range accordingly in config file.

So lets create configmap file.yaml and lets deploy this in our cluster by using kubectl create -f command.

So our configmap has been created and its created in metallb-system namespace. let’s take a look at that by using kubectl describe cm config -n metallb-system command.

So now we have got our config map here .so lets try to create service by using expose command which we already used above.

As we can see the service nginx has been created with the type LoadBalancer and you got the external IP which is 192.168.99.95 that’s the first IP address of the range that we set in the config map. So basically you need to reserve a portion of you network for load balancing so it has picked up the first IP of the range that we defined. So we should be able to access the nginx application using the load-balancer. It can directly access this IP or you can add an entry to your DNS if you have DNS server in your environments. Lets see if we can access it!!

Summary:

So that’s how you use the MetalLB LoadBalancer in your cluster. Hope this article can gives you a good understanding of how Services work and how MetalLB LoadBalancer can be configured by using Minikube.

--

--

Responses (4)