How to Deploy Flask App on Kubernetes?
Last Updated :
10 Oct, 2024
A lightweight Python framework providing simplicity and flexibility in development, Flask can be utilized to create web applications. In contrast, Kubernetes is an effective framework for container orchestration that facilitates the deployment, scaling, and management of containerized applications. While Kubernetes makes sure these apps function perfectly by managing resources and processing traffic efficiently, Flask allows you to construct web pages and handle requests. When taken together, they provide an effective combination that streamlines and improves reliability for developing, launching, and managing web applications at scale. By referring to this blog you will get clear knowledge about deploying the flask app in Kubernetes.
Step by step to Deploy Flask App on Kubernetes
Step 1: Create the repository and push the code into the repository.
Step 2: Here is my GitHub python-flask repository.
Step 3: Clone the repository into vm and list the files. Here are the my files in the repository.
Build a container image with Docker
Step 4: Here is the Dockerfile in the repository. You can write your own Dockerfile as per your requirements.
FROM python:3.9-slim
WORKDIR /usr/src/app
COPY . .
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 5000
ENV FLASK_APP=app.py
CMD ["flask", "run", "--host=0.0.0.0"]
FROM python:3.9-slim
: The basis image to be used for the instructions that follow is specified in this line. Here, the official Python Docker image with the 3.9-slim tag is being used. The image is smaller and just includes the minimum required to run Python applications, as indicated by the slim tag.WORKDIR /usr/src/app
: This line changes the container's working directory to /usr/src/app. Subsequent orders will be carried out here.COPY . .
: This line copies the current directory, which includes other files and the Dockerfile, into the container's working directory. The working directory that was established in the previous step is represented by the second. in the container, and the source directory on the host computer is represented by the first.RUN pip install --no-cache-dir -r requirements.txt
: Using pip, the Python dependencies listed in the requirements.txt file are installed in this line. Pip may be made to stop caching the downloaded packages by using the --no-cache-dir flag. This guarantees that no superfluous cache files are included and that the Docker image stays consistent.EXPOSE 5000
: This line tells Docker that the container is running and will listen on port 5000. It does not, however, truly publish the port. To specify which ports are meant to be exposed is more of a documentation mechanism.ENV FLASK_APP=app.py
: This line assigns the value app.py to the environment variable FLASK_APP. The Python web framework Flask uses this environment variable to identify which Python file contains the Flask application.CMD ["flask", "run", "--host=0.0.0.0"]
: The default command to be executed when the container starts is specified in this line. It starts the Flask development server (flask run), binds it to every network interface that is accessible (--host=0.0.0.0), and opens the server to outside access.
Step 5: We need to build the docker image by using the below command. For your reference refer the below image.
docker build -t <image-name>:<tag> .
Step 6: After successfully build the image list the docker images by using the below command. For your reference refer the below image. Here i have pushed the docker image into docker hub.
docker images
Create the Deployment and Service
Step 7: Create the deployment file here is the my deployment file you can can write your own deployment file as per the your requirements.
apiVersion: apps/v1
kind: Deployment
metadata:
name: flask-deployment
spec:
replicas: 1
selector:
matchLabels:
app: flask
template:
metadata:
labels:
app: flask
spec:
containers:
- name: flask-container
image: larasai/flask-pyhton
ports:
- containerPort: 5000
---
apiVersion: v1
kind: Service
metadata:
name: flask-service
spec:
selector:
app: flask
ports:
- protocol: TCP
port: 5000
targetPort: 5000
type: NodePort
- Deployment:
apiVersion
: Specifies the Kubernetes API version being used. In this case, it's apps/v1
, indicating the apps API group and version 1.kind
: Specifies the type of Kubernetes resource, which is a Deployment in this case.metadata
: Contains metadata about the deployment, such as its name.spec
: Defines the desired state for the deployment, including the number of replicas and the pod template.replicas
: Specifies the desired number of pod replicas to create. In this case, it's set to 1.selector
: Defines how Kubernetes identifies which pods are managed by this deployment.matchLabels
: Specifies that pods with labels matching the given labels should be managed by this deployment.
template
: Describes the pod template used to create new pods.metadata
: Contains labels to apply to the pods.spec
: Specifies the pod's specification.containers
: Describes the containers running in the pod.name
: Specifies the name of the container.image
: Specifies the Docker image used for the container.ports
: Specifies the ports the container exposes.containerPort
: Specifies the port on which the container listens for traffic. In this case, it's set to 5000.
- Service:
apiVersion
: Specifies the Kubernetes API version being used. In this case, it's v1
, indicating the core API group and version 1.kind
: Specifies the type of Kubernetes resource, which is a Service in this case.metadata
: Contains metadata about the service, such as its name.spec
: Defines the desired state for the service.selector
: Specifies how the service selects which pods to target.app: flask
: Indicates that pods with the label app
set to flask
should be targeted.
ports
: Specifies the ports that the service exposes.protocol
: Specifies the protocol used for the port.port
: Specifies the port number on the service.targetPort
: Specifies the port on the pods to which the service forwards traffic.
type
: Specifies the type of service. In this case, it's NodePort
, which exposes the service on each node's IP at a static port. This allows the service to be accessible from outside the Kubernetes cluster.
Step 8: Create the deployment file by using the below command the deployment and service created successfully in the Kubernetes cluster. For your reference refer the below image.
kubectl apply -f <deployment-file-name.yaml>

Step 9: List the deployments and services of the flask application. For your reference refer the below image.
kubectl get deployment
kubectl get pods
List the service of the flask deployment by using the below command.
kubectl get svc
Step 10: Access the application by using the nodeport. Refer the below image for your reference the flask application successfully deployed and accessed successfully.
Similar Reads
How to Deploy Kubernetes on AWS?
Kubernetes, the open-supply box orchestration platform, has emerged as the solution for dealing with containerized applications. When deploying Kubernetes in the cloud, Amazon Web Services (AWS) gives a robust and scalable environment. In this manual, we can walk you through the manner of deploying
7 min read
How to Deploy Kubernetes on GCP?
Kubernetes, the open-supply field orchestration platform, has emerged as the standard for coping with containerized applications. Google Cloud Platform (GCP) provides a robust and scalable surrounding for deploying Kubernetes clusters. In this comprehensive manual, we are able to walk you through th
5 min read
How To Deploy PHP Application On Kubernetes ?
Prologue to deploying a PHP applications on Kubernetes starts with understanding the containerization, where applications and their libraries are packaged into convenient, lightweight containers. Docker is generally utilized for this reason. Kubernetes then deals with these containers, abstracting a
9 min read
How To Deploy Flask APP On AWS EC2 Instance?
In this article, we will study how we can deploy our existing Flask application in AWS EC2. We will also see how to use the public IP of the EC2 instance to access the flask application. For this article, you should know about setting up EC2 in AWS. So, let's begin with the deployment. Primary Termi
5 min read
How To Deploy Python Application In Kubernetes ?
In today's IT world we are moving from the monolithic to microservice architecture to make our applications highly available and scalable to bring fault tolerance. In this transformation, containerization i.e., containerizing the application are a fundamental aspect of this micro services. In this a
6 min read
How to Deploy Angular App in Kubernetes ?
In the modern world of web development, Angular has become one of the most popular frameworks for building dynamic and responsive web applications. As the demand for scalability and reliability increases, deploying these applications in a containerized environment using Kubernetes has become a commo
5 min read
How to Deploy a Django Application in Kubernetes
In this article, we will study how we can deploy Django web applications to Kubernetes. We will also see how to dockerize and build an image of the Django application using Dockerfile. For this article, you should know about setting up a VM in Azure. We will see how to deploy applications on Linux S
5 min read
How to Deploy Node.js Application in Kubernetes?
Deploying the Node.js application on Kubernetes in a containerized manner makes it highly scalable, and fault-tolerant, and allows zero downtime. Kubernetes allows container orchestration that can be used in many ways during deployment. Load balancing is provided, which helps with automatic load tra
4 min read
How to Deploy Java Application in Kubernetes?
Kubernetes is a platform for automating deployment, scaling, and management of containerized applications. Pods are basic units that hold one or more containers. Deployments manage application deployment and updates. Services provide stable endpoints for accessing pods. Ingress manages external acce
9 min read
How to Change Port in Flask app
In this article, we will learn to change the port of a Flask application. The default port for the Flask application is 5000. So we can access our application at the below URL. http://127.0.0.1:5000/ We may want to change the port may be because the default port is already occupied. To do that we ju
1 min read