This repository contains all necessary configurations and guidelines for deploying a bookstore application built with FastAPI on AWS Elastic Kubernetes Service (EKS) using Terraform. This project demonstrates setting up a robust Kubernetes cluster and deploying microservices that manage books and client data, utilizing an API Gateway for seamless service interaction.
Before you start, make sure you have installed and configured the following tools:
- AWS CLI - Command-line tool for managing AWS resources.
- Visual Studio Code or PyCharm with the Terraform extension.
- Terraform CLI - Installed and added to your system's PATH.
- Kubectl - Kubernetes command-line tool, also added to your system's PATH.
- An AWS account with an active subscription / Free tier.
TerraformScriptsEKS
: Contains Terraform scripts for provisioning the EKS cluster.FastAPIApps
: Contains the FastAPI applications along with Dockerfiles andrequirements.txt
for each microservice.assets
: This folder contains a complete list of commands used (commands.txt
) and a PDF with step-by-step screenshots detailing the entire process.deployments
: Kubernetes YAML configurations for deploying the microservices.
-
Environment Setup:
- Install all required tools and ensure they are correctly configured in your system's PATH.
-
Terraform Initialization and Application:
- Navigate to the
TerraformScriptsEKS
directory. - Initialize Terraform:
terraform init -upgrade
- Create an execution plan:
terraform plan -out main.tfplan
- Apply the configuration to provision the EKS cluster:
terraform apply main.tfplan
- Navigate to the
-
Connect to the EKS Cluster:
- Update kubeconfig to interact with your cluster:
aws eks --region us-west-2 update-kubeconfig --name <cluster-name>
- Update kubeconfig to interact with your cluster:
-
Build and Push Docker Images:
- Navigate to each FastAPI application directory.
- Build and push the Docker images:
docker build . -t <your-dockerhub-username>/<app-name>:<tag> . docker push <your-dockerhub-username>/<app-name>:<tag>
-
Deploy Microservices:
- Apply the Kubernetes deployment and PVC configurations:
kubectl apply -f books-data-pvc.yml kubectl apply -f books-deployment.yml kubectl apply -f clients-data-pvc.yml kubectl apply -f clients-deployment.yml
- Apply the Kubernetes deployment and PVC configurations:
- Install the AWS EFS CSI Driver:
- Use Helm to install the AWS EFS CSI driver which enables Kubernetes to manage EFS file systems efficiently:
helm repo add aws-efs-csi-driver https://kubernetes-sigs.github.io/aws-efs-csi-driver/ helm repo update helm upgrade --install aws-efs-csi-driver --namespace kube-system aws-efs-csi-driver/aws-efs-csi-driver
- Use Helm to install the AWS EFS CSI driver which enables Kubernetes to manage EFS file systems efficiently:
- Setup ConfigMaps:
- Create a directory for configuration files:
New-Item -ItemType Directory -Path .\gateway-conf
- Copy the configuration file into the new directory:
Copy-Item -Path .\gateway\app\conf.d\main.yml -Destination .\gateway-conf\
- Create a ConfigMap and deploy the gateway:
kubectl create cm books-gateway-conf --from-file gateway-conf/ kubectl create -f gateway-deployment.yml
- Create a directory for configuration files:
-
Internal Exposure Using ClusterIP:
- Expose each microservice internally:
kubectl expose deploy books-api --port=8000 kubectl expose deploy clients-api --port=8000
- Expose each microservice internally:
-
External Exposure Using LoadBalancer:
- Provide external access through a LoadBalancer:
kubectl expose deploy gateway-api --port=8000 --type=LoadBalancer
- Access the services externally using the provided IP:
http://<external-ip>:8000/docs
- Provide external access through a LoadBalancer:
-
Installing NGINX Ingress Controller
- Using Helm to install:
helm install my-nginx bitnami/nginx-ingress-controller --set rbac.create=true
- Using Helm to install:
-
Configuring and Applying Ingress Rules
- Apply limits to prevent abuse:
kubectl apply -f ingress-limiter.yml
- Apply limits to prevent abuse:
-
Monitoring and Troubleshooting
- Commands to monitor and troubleshoot ingress setup:
kubectl get ingress kubectl logs -n <namespace> -l app=nginx-ingress --tail=100
- Commands to monitor and troubleshoot ingress setup:
- For a complete list of commands used and step-by-step guidance, check the
assets
folder where you can find thecommands.txt
and a detailed PDF.