Deploying Flask applications on Kubernetes. ๐Ÿš€

ยท

5 min read

Introduction

We have a straightforward Flask application at hand that communicates with a MySQL database. The functionality of the app is such that it enables users to post messages. These messages are subsequently stored in the database and exhibited on the frontend. Today, our focus will be on deploying this very application using Kubernetes.

Steps:

Login to AWS console > EC2 > Select region Asia Pacific (Mumbai)ap-south-1 > Launch Ubuntu > t2.medium > number of instances=2.

Make sure instances are running

Connect via ssh

Copy and use both commands

  1. chmod 400 ****key.pem

  2. Example: ssh ******

Both Master & Worker Node

Take access to both instances and use the below commands:

sudo apt update
sudo apt-get install -y apt-transport-https ca-certificates curl
sudo apt install docker.io -y

sudo systemctl enable --now docker # enable and start in single command.

curl -fsSL "https://packages.cloud.google.com/apt/doc/apt-key.gpg" | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/kubernetes-archive-keyring.gpg

echo 'deb https://packages.cloud.google.com/apt kubernetes-xenial main' | sudo tee /etc/apt/sources.list.d/kubernetes.list

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

On master node

On Worker node

Use the below commands only on the Master Node

  1. Initialize the Kubernetes master node.

     sudo kubeadm init
    

Output> Your Kubernetes control plane has initialized successfully!

  1. Set up local kubeconfig (both for root user and normal user):
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

sudo cat /etc/kubernetes/admin.conf

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

  1. Generate a token for worker nodes to join:

     sudo kubeadm token create --print-join-command
    

  1. Expose port 6443 in the Security group for the Worker to connect to Master Node

Use the below commands only on the Worker Node

  1. Run the following commands on the worker node.

     sudo kubeadm reset pre-flight checks
    

  2. Paste the join command you got from the master node and append --v=5 at the end. Make sure either you are working as sudo user or use sudo before the command:

    sudo kubeadm join ******* (Copy command from your master node) --v=5

After succesful join->

Verify Cluster Connection

On Master Node:

kubectl get nodes

Optional: Labeling Nodes

If you want to label worker nodes, you can use the following command:

kubectl label node <node-name> node-role.kubernetes.io/worker=worker

Optional: Test a demo Pod

If you want to test a demo pod, you can use the following command:

kubectl run hello-world-pod --image=busybox --restart=Never --command -- sh -c "echo 'Hello, World' && sleep 600"

alias k=kubectl

sudo echo "alias k=kubectl" ~/.bashrc

On worker node

chown $USER /var/run/docker.sock

docker ps

Check everything is running fine.

How to set-up two-tier application deployment on Kubernetes cluster

Setup

  • First, clone the code to your master node
git clone https://github.com/yesicanvinayak/two-tier-app.git
  • Move to k8s directory
cd two-tier-flask-app/k8s

  • Now, execute the below commands one by one

  • Apply mysql-svc.yml file using the below command

k apply -f mysql-svc.yml

Explanation: This command creates or updates a service defined in the mysql-svc.yml file. The service exposes the MySQL database to network traffic within the Kubernetes cluster.

Command: kubectl apply -f two-tier-app-svc.yml

  • Explanation: This command creates or updates services defined in the two-tier-app-svc.yml file. These services expose the two-tier application to network traffic, either within the cluster or externally.

Command:

mkdir -p /home/ubuntu/two-tier-flask-app/mysqldata

k apply -f mysql-pv.yml

k get pv

k apply -f mysql-pvc.yml

k get pvc

  • Command: k apply -f mysql-pv.yml

    • Explanation: This command creates or updates a Persistent Volume (PV) defined in the mysql-pv.yml file. A PV is a piece of storage that has been provisioned for use with Kubernetes pods. A PV can be used to store data that needs to persist beyond the lifecycle of individual pods.
  • Command: k apply -f mysql-pvc.yml

    • Explanation: This command creates or updates a Persistent Volume Claim (PVC) defined in the mysql-pvc.yml file. A PVC is a request for storage by a user that can be fulfilled by a PV.
  • Command: k apply -f mysql-deployment.yml

    • Explanation: This command is used to create or update a MySQL database deployment defined in the mysql-deployment.yml file. The deployment specifies how to run MySQL instances in a Kubernetes cluster.

Command:

k apply -f two-tier-app-deployment.yml

k get pods

k get deploy

k scale deployment two-tier-app --replicas=4

Command:

  • k apply -f two-tier-app-deployment.yml

    • Explanation: This command is used to create or update resources defined in the two-tier-app-deployment.yml file. The file typically contains definitions for a two-tier application deployment in a Kubernetes cluster.

Create table in database

show databases;

mysql> use mydb;

Output > Database changed

mysql> CREATE TABLE messages ( -> id INT AUTO_INCREMENT PRIMARY KEY, -> message TEXT -> );

Output > Query OK, 0 rows affected (0.02 sec)

mysql> select * from messages;

Data will be blank.

Check Application

Open the browser and check with the worker-node public IP and app-service's port number.

Add data.

Check the data Shown in the database.

Cmmand: select * from messages;

Scale your deployment.

k scale deployment two-tier-app --replicas=4

I hope you enjoy the blog post!

If you find the blog helpful, please do like it and share it with your friends. If you have any doubts or questions related to the topics covered in the blog, feel free to ask them in the comments section. Iโ€™ll be more than happy to help!

ย