Hello, readers! This article talks about Creating a Kubernetes External Load Balancer with demonstrative examples.
So, let us begin!! 🙂
Purpose of a Load Balancer in Kubernetes
On a generic scale, a load balancer is used to distribute the load across various entities. Yes, it enables us to distribute and circulate the network load across various servers.
By this, it behaves as a reverse proxy and also enables multiple users to stay connected to the end platform, and increases the reliability of the end application as a whole. They tend to reduce the burden on the servers that manage the application in terms of the traffic and routing of the users.
Usually, the Layer 07 Load balancers enable us to have the traffic management and routing through the network based on the HTTP layer protocols present in the application data.
In this post, we will be having the External Load balancer set up in the Kubernetes cluster. By this, the applications become available to the users over the IP address of the load balancer within the Kubernetes cluster.
Prerequisites – Checklist
For us to deploy an External Load balancer within the Kubernetes infrastructure, we need to make sure that we have the underlying Kubernetes infrastructure.
Also, make sure that the kubectl command-line is set up for us to have the cluster details locally through Command Prompt.
We also need to be mindful of the number of worker nodes within the cluster. Need to have at least 2 worker nodes present for the cluster.
In order to have the Load balancer created, we can choose any of the below methods-
- Using the manifest YAML files
- Using the kubectl command
Let us have a look at it one by one.
1. Creating a Service for the Loadbalancer using manifest
By manifest file, we want to use a declarative approach to create a Service of type Loadbalancer.
Have a look at the below YAML file-
apiVersion: v1
kind: Service
metadata:
name: LB-service
spec:
selector:
app: LB
ports:
- port: 8700
targetPort: 8080
type: LoadBalancer
In the above YAML, we have defined a Kubernetes Service of type Loadbalancer. It attempts to route the traffic approaching port 8080 through the pods that match the label app=LB.
2. Creating a Service for the Loadbalancer using kubectl
Instead of a manifest developed, we can use the kubectl expose command to create a Kubernetes External Load Balancer as shown below:
kubectl expose deployment example --port=8765 --target-port=9376 \
--name=example-service --type=LoadBalancer
The expose command enables us to create a service of a specific type for example Loadbalancer and have it routed through ports and target-ports having a relevant name as well.
Inspecting the Load balancer
Having created a Kubernetes Load balancer service, let us now try to introspect it to view the IP address assigned to it for the external users to access the application over and to communicate the routing.
Have a look at the below command-
kubectl describe svc service-name
Output:
Name: LB-service
Namespace: default
Labels: app=LB
Annotations: <none>
Selector: app=LB
Type: LoadBalancer
IP Families: <none>
IP: 10.3.22.96
IPs: 10.3.22.96
LoadBalancer Ingress: 192.0.2.89
Port: <unset> 8700/TCP
TargetPort: 8080/TCP
NodePort: <unset> 30594/TCP
Endpoints: 172.17.0.3:8080
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
The Loadbalancer Ingress is the IP over which the app traffic will be routed to the backend service.
Conclusion
By this, we have approached the end of this topic. Feel free to comment below in case of any questions.
For more such posts related to Kubernetes, Stay tuned with us.
Till then, Happy Learning! 🙂