NashTech Insights

How to Dockerize a FastAPI Application and Deploy to Kubernetes Cluster.

Mohd Muzakkir Saifi
Mohd Muzakkir Saifi
Table of Contents
coding script

Hello learners, I am back with another blog. In this Blog, we will learn about the dockerization of the Fastapi application and deploy it to the Kubernetes cluster [minikube]. If you like my blogs, you can read more blogs here.

Docker:-


Docker is an open-source platform that enables developers to automate the deployment with isolated containers. Containers can be defined as environments that have all the required packages and dependencies across different computing environments.

FastAPI:-


Fastapi is a high-performance web framework for building APIs with Python. It is designed to be easy to use, and efficient. FastAPI uses Python’s type annotations to enable automatic data validation and serialization. It is a high-performance asynchronous framework and utilizes the power of Python’s async and await syntax for efficient request handling.

Kubernetes-minikube:-


Kubernetes Minikube is a tool for running a local single-node Kubernetes cluster. It is very simple to set up and management of a local Kubernetes environment. With the help of Minikube, users can easily create and manage Kubernetes resources like pods, deployment, and services. Minikube allows developers to test applications locally before deploying to a production Kubernetes cluster.

Build a FastApi application:-

To start building a Fastapi application, you need to install a few packages:-

fast API and unicorn

Run the below command to install them:

pip3 install fastapi
pip3 install "uvicorn[standard]"

Now it’s time to create our application. First, you need to create a folder as fastapi-application and create a file app.py into it. Paste the below code in it:-

from typing import Union

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def read_root():
    return {"Hello": "World"}

Now you need to execute this command to run the application:

uvicorn main:app

You will see the logs in the console below images:-

You can hit this URL localhost:8000 or 127.0.0.1:8000 into the browser and verify your result,

Dockerize the application:-

First, we will add the required package in the requirement.txt file. So that anyone can install all the required packages with this file in a simple way then we will create a docker file and build the image then push it to the docker hub. We will complete this process in the steps:-

Create requirements:-

In the main folder create a file with this name requirement.txt and paste the below lines in it:-

fastapi
uvicorn

Dockerfile:-

Now will create a docker file and add all the dependencies in it:-

# Base image
FROM python:3.10

# currrent working folder
WORKDIR /app

# copy the required file in the current local folder
COPY . .

# installing the packages using this file
RUN pip install -r requirements.txt

# expose application to the 8000 port
EXPOSE 8000

# this is use to run the fastapi application
CMD ["uvicorn", "main:app", "--host", "0.0.0.0"]

Now build the docker image and push it to your docker hub account:-

docker build -t fastapi/application:1.0 .


docker run -p 8000:8000 fastapi/application:1.0

docker login

docker push fastapi/application:1.0

Deploy on Minikube:-

I am assuming that you have Minikube installed in your system and Minikube is running or you can run the below command to start the minikube:-

minikube start 

Yaml file to deploy on minikube:-

To deploy the fast API application on Kubernetes. You will need two files one for deployment and the second for the service. Basically, you need to create two files deployment.yml and service.yml.

deployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: api-blog
  template:
    metadata:
      labels:
        app: api-blog
    spec:
      containers:
      - name: app-api
        image: muzakkirsaifi/fastapi-application:1.0
        resources:
          limits:
            memory: "256Mi"
            cpu: "500m"
        ports:
        - containerPort: 8000


Run the below command to create or apply the deployment in Minikube:-

kubectl create -f deployment.yml

Now we will see the service.yml file:-

apiVersion: v1
kind: Service
metadata:
  name: api-service
spec:
  selector:
    app: api-blog
  ports:
  - port: 8000
    targetPort: 8000
  type: LoadBalancer

Run the below command to create or apply the service in Minikube:-

kubectl create -f service.yml

Check the deployment:-

kubectl get deployment

kubectl get service

minikube service fast-api-service

You will get the URL to hit on the browser as below:-

Conclusion:-

In this blog, we have learned how to Dockerize and Deploy a Fast API application to Minikube. First, we run locally then create a docker image and push then deploy on minikube. You can also read the fast api official doc and If you like my blog then you can like this or want to read more blogs then follow this link.

Mohd Muzakkir Saifi

Mohd Muzakkir Saifi

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

%d bloggers like this: