We are going to implement a Microservice app is deployed on Kubernetes using Flask app and Mongodb.
Deploying a microservice application on Kubernetes is a common practice in modern software development and container orchestration. Kubernetes provides a robust and scalable platform for managing containerized applications. Below, I'll outline the steps to deploy a microservice application on Kubernetes
Here I am using Killercoda tool for the kubernetes. Killercoda is an interactive learning platform which allows everyone to access Linux Kubernetes based environments just in their browser.
First of all clone the code from Github
git clone https://github.com/rushikesh-rawool10/microservices-k8s.git
Now go inside flask-api folder
The Docker file for flask app
To create a Dockerfile for a Flask web application, you'll need to follow some common steps to containerize your application. Here's a basic example of a Dockerfile for a Flask app
Now we need to build the docker image and pushed to Dockerhub repo.
docker build . -t mongo
docker run -d -p 5000:5000 mongo:latest
docker tag mongo rushidevops10/mongo:latest
docker push rushidevops10/mongo:latest
Image built successfully.
Docker Image successfully pushed to Docker Hub.
Now go to k8s Directory.
To deploy MongoDB using Kubernetes, you need to create a Kubernetes Deployment file (usually in YAML format) that defines the MongoDB container and its configuration. Here's an example of a basic MongoDB Deployment file:
We will now deploy Mongo DB service by running the following commands:
We need a deployment file 'mongo.yaml'
a ' mongo-pv.yml' file for persistent volume to store the pod's data.
To claim the volume we need file called 'mongo-pvc.yml'
We need a service file 'mongo-svc.yml' to expose the app to the external world. Services enable network communication to your Pods, allowing you to expose your application to the network and to other services within the Kubernetes cluster.
kubectl apply -f mongo.yaml
kubectl apply -f mongo-pv.yml
kubectl apply -f mongo-pvc.yml
kubectl apply -f mongo-svc.yml
Now the deployment file, service file, persistentVolume and persistentvolumeclaim will be created one by one.
Enter into the running pod by following command
kubectl exec -it <running-pod-name> bash
Now run mongosh
command to connect Mongo DB
To access the deployment from outside the cluster create a service file
Create a taskmaster deployment and service file by running the following commands
Check the running pods by command
kubectl get pods
To access this deployment using below command:
curl https://<ip>:<node-port>
To insert data in the mongodb, follow the command:
curl -d '{"task":"Successfully completed the assignment on Microservices on Kubernetes"}' -H "Content-Type: application/json" -X POST http://127.0.0.1:30007/task
curl -d '{"task":"Thanks a lot Shubham Sir for your valuable Guidance!"}' -H "Content-Type: application/json" -X POST http://127.0.0.1:30007/task
To see the data, run the below command:
curl http://127.0.0.1:30007/tasks