Kubernetes Deployment of 2-Tier Flask Application for DevOps Engineers:

Ghazanfar Ali
6 min readOct 10, 2023

--

Introduction:

In this project we will see how we can deploy two tier flask application on kubernetes, this flask app will be running on docker container.

To run a two tier application on kubernetes we should know these 5 things:

1- POD: It is smallest unit in kubernetes it contains containers, also supporting containers, environment variables etc. Think of a pod like a single server or computer. It’s a small, self-contained unit that can run your application. However, it might not be very reliable on its own.

2- Deployment: A deployment is like having multiple identical servers. If one server goes down (like a pod crashes), another one automatically replaces it. So, your application stays up and running. In deployment we can define replica sets like how many replicas we want of our pods.

3- Service: Imagine a service as a receptionist at a company. When you want to talk to a server (a pod), you don’t talk directly to it. Instead, you talk to the receptionist (the service), and they connect you to the right server. It makes sure your request goes to the right place. Or we can say we need service if we want to access the application from outside world mean from outside of cluster.

4- Persistent Volume: It is like a hard drive that you can plug into a server. It’s where you can store important data that you want to keep even if a server (or pod) gets replaced. It’s like having a safe place to store your important files.

5- Persistent Volume Claim (PVC): Think of a persistent volume claim as a request for a safe deposit box. When your application needs to store important data, it asks for a safe place (a PVC) to put it. Kubernetes makes sure you get the right safe deposit box (PV) to store your data.

Lets start the hands on!

First clone the github repo to your local system for manifests file:

https://github.com/devops-CloudComputing/TwoTier-Flask-App-Deployment

We will use minikube cluster for our deployment:

First of all we will create the pod for our app:

Pod is running:

Now we will see the deployment file, actually deployment file tells the desired state of our pod, if we want scaling mean multiple pods for our application we can define it in replica set of deployment file, we use labels in metadata section of deployment its mean anyone has this label this deployment is for them, in template section of deployment we define the label of app pod so it matches with the label of deployment file:

Selector: A selector is a way to filter and select resources based on their labels. It’s like a query language for labels. You can use selectors to specify the labels you want to match.

Matching Labels in a Selector: When you want to select resources that have specific labels, you define a selector with label expressions. Here are some common ways to match labels in a selector.

Now run the deployment file so it creates 3 pods for us:

If we kill any pod it will auto heal and recreate the pod again, and if we want only two pods instead of three we can do it by scale command:

We can also do scale up with same command.

Now we will see the service to expose our application to outside cluster, before writing the service file remember that few ports:

1- Target Port: It is the port of your pod on which your app is running.

2- Port: It is the internal port of your node which has many pods inside it.

3- NodePort: On which people from outside world can access this application. It is the external port of the node.

Now run the service file through kubectl:

Now check the status of our application:

It is reachable but with database error, It is because we have run the frontend or 1st tier of application but not the backend or two tier.

So, we will deploy the backend tier now, for this first we need persistent volume before creating the database , but make sure the directory that is mentioned in pv.yaml file has present in your system in my case it is:

Then run the pv yaml file:

We have created persistent volume also claim it, now go our backend deployment:

Now we will run the service for backend so that it communicate with the flask app:

Now define the service ip of mysql in deployment file of flask application:

Now re deploy the deployment file:

Now our application giving the table error:

To resolve this issue, go to container of mysql and create the table there. But first we need to find the name of the container running inside the pod:

This describe command help to get all the data, metadata of pod heer we can find the container name also:

In our case it is mysql because we used the mysql image, so access the container:

And add the mysql table:

Finally we have successfully deployed the two tier flask app on kubernetes:

--

--

Ghazanfar Ali

I write technical blogs on DevOps and AWS, Azure and GCP