This project demonstrates how to deploy a FastAPI application on Kubernetes with integrated monitoring using Prometheus and Grafana. It includes a FastAPI app, Dockerfile for containerization, Kubernetes configuration files, and setup scripts for easy deployment and monitoring.
- Project Structure
- FastAPI Application
- Docker Configuration
- Kubernetes Deployment
- Monitoring Setup
- Getting Started
- Manual Setup
- Dependencies
- Cleanup
.
├── app/
│ ├── main.py
│ ├── requirements.txt
│ ├── Dockerfile
│ ├── setup.sh
│ ├── locustfile.py
│ └── grafana-dashboard.json
├── deployment.yaml
├── service.yaml
├── service-monitor.yaml
└── README.md
The FastAPI application provides several endpoints:
- Root endpoint (
/
): Returns a greeting message - Items endpoint (
/items/{item_id}
): Returns item details - Generate endpoint (
/generate
): Generates text using a language model - Generate Quantized endpoint (
/generate_quantized
): Generates text using a quantized language model - Health endpoint (
/health
): Returns the health status of the application - Metrics endpoint (
/metrics
): Exposes application metrics for Prometheus
For more details, see app/main.py
.
The application is containerized using Docker. The Dockerfile specifies:
- Python 3.12 slim image as the base
- Installation of dependencies from
requirements.txt
- Copying of application files
- Command to run the FastAPI app using Gunicorn and Uvicorn workers
For more details, see app/Dockerfile
.
The application is deployed on Kubernetes using the following configurations:
- Creates replicas of the FastAPI application
- Uses the
fastapi-app:latest
image - Sets resource requests and limits
- Configures liveness and readiness probes
- Exposes the application using a NodePort service
- Maps port 8000 of the container to NodePort 30001
- Configures Prometheus to scrape metrics from the FastAPI application
The project uses Prometheus for metrics collection and Grafana for visualization.
- Prometheus: Collects and stores metrics from the application and cluster.
- Grafana: Visualizes the metrics collected by Prometheus.
A custom Grafana dashboard is provided in app/grafana-dashboard.json
.
For a quick start, run the following command:
cd app
./setup.sh
This script will:
- Start or configure Minikube
- Build the Docker image
- Deploy the application to Kubernetes
- Install Prometheus and Grafana using Helm
- Apply the custom Grafana dashboard
After the setup is complete:
- Access the FastAPI application via Minikube service or NodePort
- Access Grafana at http://localhost:3000 (username: admin, password will be displayed)
To start the FastAPI service:
minikube service fastapi-service
To run load tests with Locust:
locust -f app/locustfile.py --host=<URL-FROM-MINIKUBE-COMMAND-ABOVE>
If you prefer to set up the project manually:
-
Install required tools: Docker, Kubernetes (Minikube), Helm
-
Build the Docker image:
docker build -t fastapi-app:latest ./app
-
Apply Kubernetes configurations:
kubectl apply -f deployment.yaml kubectl apply -f service.yaml kubectl apply -f service-monitor.yaml
-
Install Prometheus and Grafana:
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts helm repo update helm install monitoring prometheus-community/kube-prometheus-stack
-
Apply the Grafana dashboard:
kubectl create configmap grafana-dashboard-config --from-file=app/grafana-dashboard.json -n monitoring kubectl label configmap grafana-dashboard-config grafana_dashboard=1 -n monitoring
-
Access the application and monitoring tools as described in the "Getting Started" section.
- Python 3.12
- FastAPI
- Uvicorn
- Gunicorn
- Prometheus client
- Transformers (for text generation)
- Locust (for load testing)
For a complete list of Python dependencies, see app/requirements.txt
.
To remove all resources created by this project:
# Delete application resources
kubectl delete -f deployment.yaml
kubectl delete -f service.yaml
kubectl delete -f service-monitor.yaml
# Delete Prometheus and Grafana
helm uninstall monitoring
# If using Minikube, stop the cluster
minikube stop
This project provides a comprehensive example of deploying a FastAPI application on Kubernetes with integrated monitoring. It's designed to be easily deployable and extensible for your own projects.