This project demonstrates a FastAPI CRUD (Create, Read, Update, Delete) application deployed on a Kubernetes cluster using Azure Kubernetes Service (AKS). The application is containerized using Docker, and continuous integration and deployment (CI/CD) are managed through GitHub Actions.
The FastAPI application provides endpoints to create, read, update, and delete items. Each item has an ID, name, and price.
-
Create an Item
- URL:
/items/
- Method:
POST
- Request Body:
{ "id": 1, "name": "Item 1", "price": 10.5 }
- Response:
{ "id": 1, "name": "Item 1", "price": 10.5 }
- URL:
-
Retrieve All Items
- URL:
/items/
- Method:
GET
- Response:
[ { "id": 1, "name": "Item 1", "price": 10.5 } ]
- URL:
-
Retrieve a Specific Item
- URL:
/items/{item_id}
- Method:
GET
- Response:
{ "id": 1, "name": "Item 1", "price": 10.5 }
- URL:
-
Update an Item
- URL:
/items/{item_id}
- Method:
PUT
- Request Body:
{ "id": 1, "name": "Updated Item", "price": 20.0 }
- Response:
{ "id": 1, "name": "Updated Item", "price": 20.0 }
- URL:
-
Delete an Item
- URL:
/items/{item_id}
- Method:
DELETE
- Response:
{ "detail": "Item deleted" }
- URL:
- Docker
- Kubernetes CLI (
kubectl
) - Azure CLI (
az
) - Terraform
-
Clone the Repository
git clone <repository-url> cd <repository-name>
2. **Build and Push Docker Image**
```sh
docker buildx build --platform linux/amd64 -t <dockerhub-username>/fastapi-crud:latest --push .
-
Configure Terraform
Initialize and apply Terraform configurations:
terraform init terraform plan terraform apply -auto-approve
-
Deploy to Kubernetes
Apply Kubernetes configurations:
kubectl apply -f k8s/deployment.yaml kubectl apply -f k8s/service.yaml
Add the following secrets to your GitHub repository under Settings > Secrets and variables > Actions:
DOCKER_USERNAME
: Your Docker Hub usernameDOCKER_PASSWORD
: Your Docker Hub passwordAZURE_CREDENTIALS
: Your Azure credentials in JSON formatAZURE_RESOURCE_GROUP
: The resource group name for your AKS clusterAKS_CLUSTER_NAME
: The name of your AKS cluster
The CI/CD pipeline is defined in .github/workflows/main.yml
. It performs the following steps:
- Checks out the code
- Sets up Python and installs dependencies
- Logs in to Docker Hub
- Builds and pushes the Docker image
- Logs in to Azure
- Initializes and applies Terraform configurations
- Configures
kubectl
- Deploys the application to AKS
- Waits for the Load Balancer IP
- Tests the FastAPI endpoints
Use the following commands to test the FastAPI endpoints from the internet:
-
Create an Item
curl -X POST http://<external-ip>/items/ -H "Content-Type: application/json" -d '{"id": 1, "name": "Item 1", "price": 10.5}'
-
Retrieve All Items
curl http://<external-ip>/items/
-
Retrieve a Specific Item
curl http://<external-ip>/items/1
-
Update an Item
curl -X PUT http://<external-ip>/items/1 -H "Content-Type: application/json" -d '{"id": 1, "name": "Updated Item", "price": 20.0}'
-
Delete an Item
curl -X DELETE http://<external-ip>/items/1
To test the application locally, follow these steps:
-
Run FastAPI Locally
uvicorn app.main:app --reload
-
Test Local Endpoints
-
Create an Item:
curl -X POST http://127.0.0.1:8000/items/ -H "Content-Type: application/json" -d '{"id": 1, "name": "Item 1", "price": 10.5}'
-
Retrieve All Items:
curl http://127.0.0.1:8000/items/
-
Retrieve a Specific Item:
curl http://127.0.0.1:8000/items/1
-
Update an Item:
curl -X PUT http://127.0.0.1:8000/items/1 -H "Content-Type: application/json" -d '{"id": 1, "name": "Updated Item", "price": 20.0}'
-
Delete an Item:
curl -X DELETE http://127.0.0.1:8000/items/1
-
This setup demonstrates a complete CI/CD pipeline for deploying a FastAPI application to an AKS cluster using Docker and Terraform, with end-to-end automation managed by GitHub Actions. You can use the provided steps and commands to replicate and verify the deployment and functionality of the application.