# FastAPI Kubernetes Deployment
This repository contains all the necessary files to deploy a FastAPI application with MySQL using Kubernetes. The solution includes:
- A FastAPI application.
- A MySQL database configured using StatefulSet.
- Kubernetes manifests to manage deployments and services.
---
## Prerequisites
1. **Kubernetes Cluster**: Ensure you have a running Kubernetes cluster (e.g., Minikube, K3s, or cloud-managed).
2. **kubectl**: Kubernetes command-line tool installed and configured to access your cluster.
3. **Docker**: Installed and configured to build and push images.
4. **Docker Hub Account**: For storing the Docker image.
---
## Deployment Steps
### 1. Clone the Repository
```bash
git clone https://github.com/<your-username>/fastapi-kubernetes.git
cd fastapi-kubernetes
-
Navigate to the FastAPI application directory:
cd test/k8s-eval-fastapi
-
Build the Docker image:
docker build -t <your-dockerhub-username>/fastapi:latest .
-
Push the Docker image to Docker Hub:
docker push <your-dockerhub-username>/fastapi:latest
-
Create the required Kubernetes secrets for MySQL:
kubectl create secret generic mysql-user -n eval \ --from-literal=user=<your-mysql-username> \ --from-literal=password=<your-mysql-password> \ --from-literal=database=<your-database-name>
-
Apply the MySQL manifests:
kubectl apply -f mysql/mysql-local-data-folder-pv.yaml kubectl apply -f mysql/statefulset.yaml kubectl apply -f mysql/mysql-service.yaml
-
Verify the MySQL pod is running:
kubectl get pods -n eval
-
Apply the FastAPI Deployment and Service:
kubectl apply -f test/k8s-eval-fastapi/fastapi-deployment.yaml kubectl apply -f test/k8s-eval-fastapi/fastapi-service.yaml
-
Verify the FastAPI pods are running:
kubectl get pods -n eval
-
Check the exposed service:
kubectl get svc -n eval
-
Get the IP address of your Kubernetes node:
kubectl get nodes -o wide
-
Use the NodePort to access the application:
curl http://<Node-IP>:30000
-
View logs of the FastAPI pod:
kubectl logs <fastapi-pod-name> -n eval
-
Check logs for debugging:
kubectl logs <mysql-pod-name> -n eval
To delete the resources:
kubectl delete -f mysql/
kubectl delete -f test/k8s-eval-fastapi/
fastapi-kubernetes/
├── mysql/
│ ├── mysql-local-data-folder-pv.yaml # Persistent Volume for MySQL
│ ├── mysql-service.yaml # MySQL Service
│ ├── statefulset.yaml # MySQL StatefulSet
├── test/k8s-eval-fastapi/
│ ├── app/ # FastAPI source code
│ │ └── main.py # FastAPI application entry point
│ ├── Dockerfile # Dockerfile to build the FastAPI image
│ ├── fastapi-deployment.yaml # Deployment for FastAPI
│ ├── fastapi-service.yaml # Service for FastAPI
│ ├── requirements.txt # Python dependencies
└── README.md # Project documentation
- Yassine: GitHub Profile