Create simple load balancer with kubernetes, docker, and python
Load balancer is the process of sharing the traffic load on an application on the server. With the load balancer, the incoming load will be divided based on the number of duplicated applications (pods). the purpose of the load balancer is to speed up processing time, avoid queuing requests so that the server can be optimized.
Generally, load servers are used on production servers that have large data traffic so as to prevent overloading and slow servers.
Benefits of using load balancing
- high performance guarantee
- minimize downtime
- save resources
- efficient
Kubernetes is an open-source platform that is used to manage containerized application workloads, and provides declarative configuration and automation. Kubernetes is widely used today because it has several features including:
- Container platforms
- Microservice platforms
- Easy to deploy cloud platform
I want to explain how to run a load balancer by running a simple application using the python programming language.
There are tools that you need to prepare, including:
- python3
- docker
- minikube
- (kubectl, kubeadm, kubelet)
How to install it on ubuntu is as follows
1. python
Open your terminal and type the script below. for more convenience you can see the installation instructions on the following link
$ sudo apt-get update
$ sudo apt-get install python3
$ python3 — version
ohh yaa you should not forget to install pip i.e. library management system for python
$ sudo apt-get -y install python3-pip
$ pip3 — version
2. docker
docker is an open source tool or application that functions as a container so that it can be filled with various applications. the purpose of using docker is to make it easier for developers to manage applications and can be used on various platforms. as for how to install docker, you can follow the following link
$ sudo apt-get remove docker docker-engine docker.io containerd runc
$ sudo apt-get update
$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg — dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
$ echo \
“deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable” | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
$ sudo docker run hello-world
3. minikube
minikube is an application that is used to run Kubernetes locally. minikube runs a kubernetes cluster with a single node. to install minikube you can follow this link
$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
$ sudo install minikube-linux-amd64 /usr/local/bin/minikube
$ minikube start
4. kubectl, kubeadm, kubelet
- kubectl is a tool used to give commands to kubernetes. kubectl is used to run applications, inspect clusters, and view logs.
- kubeadm is a tool used to manage kubernetes clusters.
- kubelet is an Agent that runs on each node in the cluster which is responsible for ensuring that containers are run in the Pod.
how to install it is as follows this link
$ sudo apt-get update
$ sudo apt-get install -y apt-transport-https ca-certificates curl
$ sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
$ echo “deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main” | sudo tee /etc/apt/sources.list.d/kubernetes.list
$ sudo apt-get update
$ sudo apt-get install -y kubelet kubeadm kubectl
$ sudo apt-mark hold kubelet kubeadm kubectl
OK, your equipment is ready, now it’s time to work on it.
Let’s Coding…
Prepare a folder in which there are 4 files
- app.py
create a python file to run a web application using flask you can copy the script below
you can try running this application to make sure no errors occur. for that you need to make sure you have installed flask in your environment
$ python3 app.py
2. requirements.txt
create a requirements.txt file which contains the libraries that will be used in your application
3. Dockerfile
create a file with the name Dockerfile and you can copy the script below.
after that you can build the docker file
$ docker build -t flask-kubernetes .
if there is no problem then you will see like the image below.
to make sure your image is available you can see it by
$ docker images
then you will see the name of your image as shown below
4. deployment.yaml
create a deployment.yml file, this file is a config file that will be run by kubernetes. to change the number of replicas that will run you only need to change the value on the replica according to the traffic needs of your application.
now it’s time to run the kubernetes application.
- running minikube
to run minikube you just need to write
$ minikube start
on your terminal. if there is no problem then you will see a display like the image below.
2. run kubectl
to give the command to kubernetes to run docker and replicate the service type the command as below
$ kubectl apply -f deployment.yaml
if there is no problem it will display as shown below
3. check the service has been replicated and running well
to check whether the pods have been successful to pull the docker images, run it and do the replication, it can be seen in the following way
$ kubectl get pods
if there is no problem then the service will be replicated into 5 pods each with a running status as shown below
4. To see the Kubernetes dashboard of your application you only need to type the script as below
$ minikube dashboard
if there is no problem it will look like the image below, and your browser will be directed to the dashboard to see the workload status of your application.
Congratulations, you have successfully used kubernetes to run your application.
for the complete repository you can see on my github