Deploy Python-Flask Application to Kubernetes

Pratik Kumar
4 min readNov 4, 2020

This article will explain how can we deploy our Python-Flask Application to Kubernetes. We will use Kubernetes to deploy our Flask Application.

We will build a Flask Application and create a Docker Image of flask application and deploy in a Kubernetes cluster.

Why we are using Kubernetes?

Suppose we hosted an application on internet using any hosting services. At some point of time number of user on your application increases so your application crashes because of very high number of requests. So in that case there is no backup or no another application which will serve in the meantime we fix the application problem.

Kubernetes comes with solution for this problem. So what Kubernetes does is it create multiple instances of your application. It has also the load balancer over it so that the users which are accessing your application will not directly access the url of your application. It will call the load balancer and load balancer will the distribute the number of request over multiple instances of the application even if one of the instances of the application is crashed Kubernetes automatically create another instance of the application.

Prerequisites

  1. Docker Desktop
  2. Kubernetes CLI
  3. Minikube

step 1- Create a Flask Application

let’s create a basic Flask application. create an app.py file and add this code into it.

from flask import Flask, jsonify
app = Flask(__name__)
@app.route("/")
def hello():
return jsonify({"Message": "Hello World"})
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True)

Create an requirement.txt file to add flask as our dependency.

flask

step 2- Create a Docker image

To containerize the application we will create a Dockefile from which we will create an Docker image corresponding to the Application.

create a file name Dockefile and add this into it.

FROM python:3.7
RUN mkdir /app
WORKDIR /app/
ADD . /app/
RUN pip install -r requirements.txt
CMD ["python", "/app/app.py"]

then we will generate docker image for the application using this Dockerfile.
Run below command to create an image

docker build -t flask-kubernetes-deploy .

it will generate a docker image of this application. we can also check image via this command

docker images

output

REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
flask-kubernetes-deploy latest 544c8498b243 26 hours ago 885MB

tag the image using this command

docker tag flask-kubernetes-deploy <username_of_docker hub>/flask-kubernetes-app

push the image to docker hub

docker push <username_of_docker hub>/flask-kubernetes-app

step 3- Deployment

create a deployment.yaml to deploy an application on to the Kubernetes engine.

apiVersion: v1
kind: Service
metadata:
name: flask-test-service
spec:
selector:
app: flask-test-app
ports:
- protocol: "TCP"
port: 6000
targetPort: 5000
type: LoadBalancer
---apiVersion: apps/v1
kind: Deployment
metadata:
name: flask-test-app
spec:
selector:
matchLabels:
app: flask-test-app
replicas: 5
template:
metadata:
labels:
app: flask-test-app
spec:
containers:
- name: flask-test-app
image: docker.io/<username>/flask-kubernetes-app
imagePullPolicy: IfNotPresent
ports:
- containerPort: 5000

in this yaml file there is two parts one is service and another is deployment. means we are creating a service which will work as a load balancer and deployment which will serve as application.

we need to execute below command to create service and deployment

kubectl apply -f deployment.yaml

output:

service/flask-test-service created
deployment.apps/flask-test-app creted

you can check pods by this command:

kubectl get po

output:

NAME                                       READY   STATUS    RESTARTS   AGE
flask-test-app-74bcc4d458-87xj2 1/1 Running 0 10h
flask-test-app-74bcc4d458-9vvdr 1/1 Running 0 10h
flask-test-app-74bcc4d458-n82s8 1/1 Running 0 10h
flask-test-app-74bcc4d458-vcp6m 1/1 Running 0 10h
flask-test-app-74bcc4d458-vr6zk 1/1 Running 0 10h

as an output we will get 5 replicas of application.

we can check the service by this command:

kubectl get svc

output:

NAME                      TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
flask-test-service LoadBalancer 10.101.220.162 <pending> 6000:32609/TCP 10h

Run minikube service flask-test-service to access the application and minikube dashboard to access the Kubernetes application.

you can see the deployment and pods on minikube dashboard like that:

Conclusion:

So, In this way of deployment your application is scalable and always available to the user.
you can access the above code from
https://github.com/pratik-iiitkalyani/Deploy-Flask-Application-To-Kubernetes-using-Minikube

--

--