Deploying a FastAPI Application on Kubernetes: A Step-by-Step Guide for Production

Sumanta Mukhopadhyay
4 min readFeb 25, 2023

--

If you’re developing a FastAPI application and are planning to deploy it to production, you’ll want to ensure that it can be scaled to handle increased traffic, and can be deployed and managed easily. Kubernetes is a popular container orchestration tool that can help you achieve these goals.

In this tutorial, we’ll show you how to take a FastAPI application to production using Kubernetes. We’ll containerize the application, set up a Kubernetes cluster, create a deployment and service, and access the application using the service IP address.

Prerequisites

Before we start, you’ll need the following:

  • A working FastAPI application
  • Docker installed on your machine
  • A Kubernetes cluster set up on your local machine using Minikube
  • kubectl installed on your machine

Step 1: Containerize the FastAPI Application

The first step in deploying a FastAPI application using Kubernetes is to containerize the application. To do this, you’ll need to create a Dockerfile in the root directory of your application. Here’s an example Dockerfile:

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9

COPY ./app /app

EXPOSE 80

This Dockerfile is based on the tiangolo/uvicorn-gunicorn-fastapi image, which includes the Uvicorn server, the Gunicorn server, and the FastAPI framework.

The COPY command copies the contents of the app directory in your local machine to the /app directory in the Docker container.

The EXPOSE command exposes port 80 in the container.

To build the Docker image, navigate to the root directory of your application in a terminal and run the following command:

docker build -t <dockerhub_username>/<image_name>:<tag> .

Replace <dockerhub_username>/<image_name>:<tag> with a name and tag for your Docker image. This command builds the Docker image based on the Dockerfile in the current directory.

Step 2: Set up a Kubernetes Cluster

The next step is to set up a Kubernetes cluster on your local machine. In this tutorial, we’ll use Minikube to set up a single-node Kubernetes cluster.

To install and start Minikube, follow the instructions in the official documentation: https://minikube.sigs.k8s.io/docs/start/

Once Minikube is installed and started, you can check that it is running by running the following command:

minikube status

This command should show the status of the Minikube cluster.

Step 3: Create a Kubernetes Deployment

A deployment is a Kubernetes object that manages a set of identical pods. To create a deployment for the FastAPI application, you’ll need to create a YAML file that specifies the Docker image and container port. Here is an example YAML file:

apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: <dockerhub_username>/<image_name>:<tag>
ports:
- containerPort: 80

Replace <dockerhub_username>/<image_name>:<tag> with the Docker image name and tag that you created earlier. Save this file as deployment.yaml.

To create the deployment, run the following command:

kubectl apply -f deployment.yaml

This command creates a deployment object in the Kubernetes cluster based on the YAML file.

Step 4: Create a Kubernetes Service

A Kubernetes service is an object that provides a stable IP address and DNS name for accessing a set of pods. To create a service for the FastAPI application, you’ll need to create another YAML file. Here’s an example YAML file:

apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
selector:
app: myapp
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer

Save this file as service.yaml.

This YAML file creates a service object in the Kubernetes cluster that selects pods with the label app: myapp. It exposes port 80 of the pods and maps it to port 80 of the service. The type: LoadBalancer line creates a load balancer that exposes the service outside of the cluster.

To create the service, run the following command:

kubectl apply -f service.yaml

This command creates a service object in the Kubernetes cluster based on the YAML file.

Step 5: Access the FastAPI Application

Now that the deployment and service are created, you can access the FastAPI application using the service IP address.

To get the IP address of the service, run the following command:

minikube service myapp-service --url

This command prints the URL to access the service. Copy this URL and paste it into a web browser to access the FastAPI application.

Conclusion

In this tutorial, we showed you how to take a FastAPI application to production using Kubernetes. We containerized the application, set up a Kubernetes cluster, created a deployment and service, and accessed the application using the service IP address.

By using Kubernetes, you can easily scale and manage your FastAPI application in a production environment. You can also use Kubernetes to deploy your application to a cloud provider such as Amazon Web Services, Google Cloud Platform, or Microsoft Azure.

We hope this tutorial was helpful. If you have any questions or comments, please leave them below.

--

--

Sumanta Mukhopadhyay
Sumanta Mukhopadhyay

No responses yet