Kubernetes Flask App Deploy With Microservice

·

3 min read

In this blog, we will learn how to deploy a Flask app that communicates with a MongoDB database, containerize the app using Docker, and manage it through a Kubernetes cluster

Steps:

Kubernetes installation

sudo apt-get update

sudo apt install docker.io

systemctl start docker

systemctl enable docker

Install the necessary packages for Kubernetes:

curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg

Add the Kubernetes signing key:

echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list

Update the system and install Kubernetes:

sudo apt update -y sudo apt install kubeadm=1.20.0-00 kubectl=1.20.0-00 kubelet=1.20.0-00 -y

Initialize the cluster (Master):

sudo su

kubeadm init

Run this command on the Master Node:

mkdir -p $HOME/.kube

sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

sudo chown $(id -u):$(id -g) $HOME/.kube/config

kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml

Generate the Token for the configuration of the Worker Node:

kubeadm token create --print-join-command

Worker Node

Paste the above output in the Worker Node:

sudo su ---Paste the Join command on the worker node with --v=5

Now execute this command in the Master Node :

kubectl get nodes

Master Node by executing the following command: git clone Project Code

https://github.com/Ibrahimsi/microservices-k8s.git

You have successfully cloned the repository to your system. Follow the steps below to navigate to the files:

Deploying the Taskmaster Micro-service

kubectl apply -f taskmaster.yml

  • Execute the following command to scale up the Taskmaster:

kubectl scale --replicas=3 deployment/taskmaster

  • To provide users with access to our taskmaster, we need to deploy a taskmaster service.

kubectl apply -f taskmaster-svc.yml

Expose the Service to Postman

To ensure our taskmaster service can be accessed, we need to whitelist port 30007 in the Security Group of our Worker Node (Azure VM), since that's the port it's running on.

Next, open the Postman Dashboard and navigate to My Workspace. Select the GET method and enter the Public IP of your Worker Node in the address field, followed by port 30007. Finally, click on the Send button to initiate the request.

Our deployment of the taskmaster service was successful.

Integrate Persistent Volume and PV Claim

kubectl apply -f mongo-pv.yml

Create a persistent volume and claim it by executing the following command:

kubectl apply -f mongo-pvc.yml

Deploy Mongo DB

kubectl apply -f mongo.yml

MongoDB has been deployed successfully.

To check the status of all pods, execute the following command

kubectl get pods

View the running containers by using <docker ps> commands on the worker node.

Our MongoDB container is up and running

Troubleshoot

MongoDB services create a need to connect outside the world.

kubectl apply -f mongo-svc.yml

Check now if working or not

Check browser

THANKS, AND HAPPY LEARNING...!!!