This project demonstrates a Flask API with two microservices deployed on a Kubernetes cluster. The microservices include:
/reverse
: Reverses a given message./api
: Accepts a message and delegates the reversal task to/reverse
while also adding a random number.
Ensure you have Minikube or a Kubernetes cluster running with DNS and an Ingress controller.
sudo su
minikube start --vm-driver=none
minikube addons enable ingress
minikube addons enable kube-dns
git clone https://github.com/nitesh8860/flask-minikube.git
docker run -d -p 5001:5000 --restart=always --name registry -v /var/registry:/var/lib/registry registry:2
./start
./stop
Get the Cluster IP of the /api
service:
kubectl get svc | grep flask-api
Use curl
to test the API:
curl -XPOST http://<CLUSTER-IP>:5000/api -d '{"message": "abcdef"}' -H "Content-Type: application/json"
Check the logs of the deployments:
kubectl logs deployment/flask-api
kubectl logs deployment/flask-reverse
gcloud builds --project projectName submit --tag gcr.io/projectName/flask-reverse:v1 ./reverse/
gcloud builds --project projectName submit --tag gcr.io/projectName/flask-api:v1 ./api/
Deploy the services:
kubectl apply -f deploy-gcloud-reverse.yaml
kubectl apply -f deploy-gcloud-api.yaml
Get service URLs:
kubectl get services --namespace=flask
Apply Ingress after changing the hostname in ingress.yml
:
kubectl apply -f ingress.yml
In this pipeline, the /reverse
service is the backend, and the /api
service is the frontend.
- Avoid downtimes using Canary deployments.
- Create separate namespaces for development, testing, and production environments.
Enjoy exploring this Flask API with microservices on a Kubernetes cluster!