Open In App

Kubernetes – Load Balancing Service

Last Updated : 30 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Pre-requisite:- Kubernetes

Before learning Kubernetes ( or K8S in Short ) you should have some knowledge of Docker and Containers. Docker is a tool that helps the developer to create containers in which applications can run in an isolated environment. Containers are just an abstraction for the applications inside. Docker also provides some facilities for these containers to talk to each other, store data on the host machine and many more. Similarly, Kubernetes is used to control these containers and enhance their availability as well as functionality. Docker has containers, but Kubernetes has Containers, Pods, and then Nodes. Nodes contain Pods and Pods have Containers. Kubernetes ensures more isolation of the container.

Service

A service is a functionality that is by default disabled on the containers, pods, and nodes. We need to mention about specific service that we want to enable. Some of the services offered by the Kubernetes cluster are NodePort and Load Balancer services. We have discussed in detail the load balancer service below in the article.

Load Balancing Service

Suppose we run a company and we want to hire some employees. We have shared a link on which interested candidates can share their resumes and book a slot for the interview. But our website can only handle about 10 people at a time. This can lead to the loss of great talent and eventually, this is a loss to the company. To solve this problem we needed load balancers. these load balancer launches a new clone website when the number of users reaches a certain limit and redirect those extra users to the newly created clone website.

Creation of  Deployment

We could create deployment by simply executing commands on the CLI or by using YML or JSON configuration files for the deployment. Deployment communicates with the pods about the creation of containerized applications as well as about the modification that can be made to the same applications. We will be using the Nginx image for creating this deployment.

Using CLI Command

$ kubectl create deployment webserver-deployment –image=nginx:latest

$ kubectl get deployments

$ kubectl get pods

create deployment

 

It will take some time to create and start the start container, that is the reason the status of the pod is showing “ContainerCreating”.

Using Deployment Manifest File

We need to create a deployment manifest file. Below is an example of such a manifest file. This file is created in YAML format because it is easy to create and edit for humans. YAML is used in various other processes apart from configuration files. Like, in Log files, inter-process messaging, and for advance data structures. Create a yml file named “loadBalancerDeployment.yml”.

#Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-nginx
  labels:
    app: nginx-application
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-application
  template:
    metadata:
      labels:
        app: nginx-application
    spec:
      containers:
      - name: nginx-container
        image: nginx:latest
        ports:
        - containerPort: 80
                  

To create this deployment execute the below command.

$kubectl create -f  loadBalancerDeployment.yml
NGINX deployment created

 

Creation of Load Balancing Service and linking it to Deployment

To create a load balancer service we have to create a service manifest file and to link this service to the pods we can use the labels of the pods previously used in the deployment manifest file.

#Service
apiVersion: v1
kind: Service
metadata:
    name: service-nginx
    labels:
      app: nginx-application
spec:
  selector:
    app: nginx-application
  type: LoadBalancer
  ports:
  - nodePort: 31000
    port: 80
    targetPort: 80

To create this service execute the below command.

$ kubectl create -f   loadBalancerService.yml
load balancer service

 

Details of the Service

To check if the service is created or not execute the below command. it gives a brief introduction to the service.

$ kubectl get services -l app: nginx-application

To get details about the load balancer service use the “kubectl describe” command.

$ kubectl describe service service-nginx
kubectl describe service

 

We have created a deployment and linked that deployment to the load balancer service. Now load balancer will take care of all the users coming to the website.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads