Deploying FastAPI and PostgreSQL Microservices to EKS
Objective
This lab shows you how to deploy the microservices of the python-fastapi-demo-docker project onto your Amazon EKS cluster—either your AWS Fargate or managed node groups-based cluster. To gain a deeper understanding of the Kubernetes resources in these manifests, refer to Deploying FastAPI and PostgreSQL Kubernetes resources to Amazon EKS.
Prerequisites
- Securing FastAPI Microservices with Kubernetes Secrets in Amazon EKS
- Building and Running the Docker Containers
- Uploading Container Images to Amazon ECR
Initial Setup
Navigate to the root directory of the python-fastapi-demo-docker
project where your environment variables are sourced:
cd ~/environment/python-fastapi-demo-docker
1. Creating db-init-script Configmap
Run the following command from the python-fastapi-demo-docker
project directory to create the ConfigMap:
kubectl create configmap db-init-script --from-file=init.sh=server/db/init.sh -n my-cool-app
The expected output should look like this:
configmap/db-init-script created
To confirm that your Kubernetes Configmap has been successfully created, you can use the kubectl get configmap
command. This command lists all ConfigMaps that exist in the specified namespace:
kubectl get configmap -n my-cool-app
The expected output should look like this:
NAME DATA AGE
db-init-script 1 4m47s
kube-root-ca.crt 1 5m36s
2. Deploying the PostgreSQL StatefulSet, Service, and PersistentVolumeClaim
The eks/deploy-db-python.yaml file is used for the deployment of the PostgreSQL database and consists of four primary resources: a StorageClass, Service, StatefulSet, and PersistentVolumeClaim.
- Fargate
- Managed Node Groups
From the python-fastapi-demo-docker
project directory, apply the database manifest:
kubectl apply -f eks/deploy-db-python-fargate.yaml
It will take a few minutes for Fargate to provision pods. In order to verify that the database pod is running please run the below command:
kubectl get pod fastapi-postgres-0 -n my-cool-app
The expected output should look like this:
kubectl get pod fastapi-postgres-0 -n my-cool-app
NAME READY STATUS RESTARTS AGE
fastapi-postgres-0 1/1 Running 0 4m32s
From the python-fastapi-demo-docker
project directory, apply the database manifest:
kubectl apply -f eks/deploy-db-python.yaml
In order to verify that the database pod is running please run the below command:
kubectl get pod fastapi-postgres-0 -n my-cool-app
The expected output should look like this:
kubectl get pod fastapi-postgres-0 -n my-cool-app
NAME READY STATUS RESTARTS AGE
fastapi-postgres-0 1/1 Running 0 4m32s
3. Deploying the FastAPI Deployment, Service, and Ingress
The deploy-app-python.yaml manifest file is used for the deployment of the FastAPI application and consists of three primary resources: a Service, Deployment, and Ingress.
First, retrieve your Amazon ECR repository URI using the following command:
echo ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/fastapi-microservices:${IMAGE_VERSION}
The expected output should look like this:
012345678901.dkr.ecr.us-west-1.amazonaws.com/fastapi-microservices:1.0
Next, open eks/deploy-app-python.yaml and replace the sample value with your ECR repository URI image and tag (e.g., 1.0
).
From the python-fastapi-demo-docker
project directory, apply the application manifest:
kubectl apply -f eks/deploy-app-python.yaml
4. Verifying the Deployment
After applying the manifest, verify that the deployment is running correctly.
Check the services:
kubectl get services -n my-cool-app
The expected output should look like this:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
db ClusterIP None <none> 5432/TCP 2m48s
fastapi-service NodePort 10.100.18.255 <none> 80:30952/TCP 21s
Check the ingress:
kubectl get ingress -n my-cool-app
The expected output should look like this:
NAME CLASS HOSTS ADDRESS PORTS AGE
fastapi-ingress <none> * k8s-mycoolap-fastapii-8114c40e9c-860636650.us-west-2.elb.amazonaws.com 80 3m17s
Check the deployments:
kubectl get deployments -n my-cool-app
The expected output should look like this:
NAME READY UP-TO-DATE AVAILABLE AGE
fastapi-deployment 1/1 1 1 67s
Check the StatefulSet:
kubectl get statefulsets -n my-cool-app
The expected output should look like this:
NAME READY AGE
fastapi-postgres 1/1 3m59s
Check the PersistentVolumeClaims:
kubectl get pvc -n my-cool-app
The expected output should look like this:
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
postgres-data-fastapi-postgres-0 Bound pvc-84d12ce1-916c-4044-8056-94eb97e25ccd 1Gi RWO ebs-sc 4m12s
Check the pods:
kubectl get pods -n my-cool-app
The expected output should look like this:
NAME READY STATUS RESTARTS AGE
fastapi-deployment-6b587dfb54-j26pc 1/1 Running 0 2m19s
fastapi-postgres-0 1/1 Running 0 4m46s