What is Kubernetes LoadBalancer?
A LoadBalancer service is the standard way to expose a service to the outside. On cloud, this will spin up a Network Load Balancer that will give you a single IP address that will forward all traffic to your service.
If your Kubernetes cluster environment is on any cloud provider like google cloud, Azure, or AWS, then if you use the type load balancer, you will get an external IP from this provider on behalf of you. so you can access your application using the external IP provided by the provider that will forward the request to the pods. But is chargeable.
if you are using On-Premises BareMetal Kubernetes Environment then you need to use MetalLB for getting the load balancer external IP
A pure software solution: MetalLB
MetalLB provides a network load-balancer implementation for Kubernetes clusters that do not run on a supported cloud provider, effectively allowing the usage of LoadBalancer Services within any cluster.
This section demonstrates how to use the Layer 2 configuration mode of MetalLB together with the NGINX Ingress controller in a Kubernetes cluster that has publicly accessible nodes. In this mode, one node attracts all the traffic for the ingress-nginx Service IP.
Installation
Helm Add Repository and Update We will add the helm repository and then make sure that all of our repositories are up to date.
helm repo add metallb https://metallb.github.io/metallb
helm repo update
Basic configuration
$ cat values.yaml
apiVersion: v1
kind: ConfigMap
metadata:
namespace: metallb-system
name: config
data:
config: |
address-pools:
- name: default
protocol: layer2
addresses:
- 192.168.100.120-192.168.100.130
kubectl apply -f values.yaml
Load Balancer workflow diagram
First, we need to create a deployment
root@Kubernet-Master:~# cat deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels: #label name should be common in load balancer service
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
root@Kubernet-Master:~#
kubectl apply -f deployment.yaml
For checking the deployment status
kubectl get deployment
Load balancer Manifest file
root@Kubernet-Master:~# cat loadbalancer.yaml
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
status:
loadBalancer:
Apply the configuration
kubectl apply -f loadbalancer.yaml
For checking the service output
kubectl get svc
http://192.168.100.120
Hope you have got an idea about Kubernetes Load Balancer and why it is needed
Thank you !!!