Deploy Python-Flask Application to Kubernetes

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.


  1. Docker Desktop

step 1- Create a Flask Application

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

from flask import Flask, jsonify
app = Flask(__name__)
def hello():
return jsonify({"Message": "Hello World"})
if __name__ == "__main__":'', debug=True)

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


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
ADD . /app/
RUN pip install -r requirements.txt
CMD ["python", "/app/"]

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


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
name: flask-test-service
app: flask-test-app
- protocol: "TCP"
port: 6000
targetPort: 5000
type: LoadBalancer
---apiVersion: apps/v1
kind: Deployment
name: flask-test-app
app: flask-test-app
replicas: 5
app: flask-test-app
- name: flask-test-app
imagePullPolicy: IfNotPresent
- 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


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

you can check pods by this command:

kubectl get po


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


NAME                      TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
flask-test-service LoadBalancer <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:


So, In this way of deployment your application is scalable and always available to the user.
you can access the above code from

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store