Deploying a Flask API and MongoDB on Kubernetes

Deploying a Flask API and MongoDB on Kubernetes

An End to End project

Prerequisites

Before we get started, make sure you have the following tools and services installed and configured:

🚀 Two AWS EC2 instances with Ubuntu 20.04 LTS installed of instance type t2.medium

🚀 Setting up Kubernetes Cluster

1️⃣ Deploying the Flask API

Create Deployment

Master node command below

Worker command below to check whether the container is working or not

2️⃣ Scaling the Pod

Added an API service on NodePort (expose to an outside pod)

kubectl apply -f taskmaster-svc.yml

3️⃣ Adding Persistent Volume

4️⃣ Deploying MongoDB

Applying deployment for creating MongoDB below

Go to the worker node and type docker ps to get the Mongo DB status

Mongo DB running now

5️⃣ Now connecting Mongo DB to the outer side world

This will create a new Service that maps port 27017 to the MongoDB container.

6️⃣ Test the Flask API

With the MongoDB database and Flask API deployed, we can now test the API to ensure that everything is working as expected. To do this, I'll use the curl command to make HTTP requests to the API.

Create a task

To create a task in the API, run the following command:

curl -H "Content-Type: application/json" -X POST -d '{"title": "Do laundry", "description": "Wash and fold clothes"}' http://<server-ip>:30007/tasks

Replace <server-ip> with the IP address of the node that the Flask API Service is running on. If the task is created successfully, you should see a JSON response containing the task details:

{"_id": {"$oid": "60924af78432c1d826ef23ec"}, "title": "Do laundry", "description": "Wash and fold clothes"}

Get all tasks

To get all tasks from the API, run the following command:

curl http://<server-ip>:30007/tasks

Replace <server-ip> with the IP address of the node that the Flask API Service is running on. If everything is working correctly, you should see a JSON response containing the task you just created:

[{"_id": {"$oid": "60924af78432c1d826ef23ec"}, "title": "Do laundry", "description": "Wash and fold clothes"}]

Get a specific task

To get a specific task by ID, run the following command:

curl http://<server-ip>:30007/tasks/<task-id>

Replace <server-ip> with the IP address of the node that the Flask API Service is running on, and <task-id> with the ID of the task you want to retrieve. If everything is working correctly, you should see a JSON response containing the details of the specified task:

{"_id": {"$oid": "60924af78432c1d826ef23ec"}, "title": "Do laundry", "description": "Wash and fold clothes"}

Update a task

To update a task by ID, run the following command:

curl -H "Content-Type: application/json" -X PUT -d '{"title": "Do laundry", "description": "Wash and fold clothes and towels"}' http://<server-ip>:30007/tasks/<task-id>

Replace <server-ip> with the IP address of the node that the Flask API Service is running on, and <task-id> with the ID of the task you want to update. If the task is updated successfully, you should see a JSON response containing the updated task details:

{"_id": {"$oid": "60924af78432c1d826ef23ec"}, "title": "Do laundry", "description": "Wash and fold clothes and towels"}

Delete a task

To delete a task by ID, run the following command:

curl -X DELETE http://<server-ip>:30007/tasks/<task-id>

Replace <server-ip> with the IP address of the node that the Flask API Service is running on, and <task-id> with the ID of the task, you want to delete. If the task is deleted successfully, you should see a JSON response containing the ID of the deleted task:

{"_id": {"$oid": "60924af78432c1d826ef23ec"}}

Troubleshooting

During the deployment process, you may encounter some errors. Here are a few common errors and how to troubleshoot them:

Error: "The connection to the server localhost:8080 was refused"

Thank you!!

~Ritul Gupta