Deploying Cloud Native Monitoring Application on kubernetes

Throughout this project, we will be creating a monitoring application in Python using Flask. We will start building the application and then containerize it using Docker, we will create a docker file after that we will build the image and run the container locally. Once we have containerized the app locally then we will create an AWS ECR using Python boto3 module. In ECR we will push the Docker image to store and retrieve the docker image in a secure and efficient manner. Next, we will move to the deployment phase we will create an elastic Kubernetes cluster with nodes and deploy the application on Kubernetes. we will create

Prerequisites

  1. AWS Account

  2. Programmatic access and AWS-configured CLI

3. Python3 installed

  1. Docker and Kubernetes installed

  2. Code Editor(VS code)

After validating all the prerequisites we are going to create a cloud_native_monitoring_app folder under VS code. then we will create an app.py file which will be having the Python code.

app.py

import psutil

from flask import Flask

app = Flask(name)

@app.route("/")

def index():

cpu_percent=psutil.cpu_percent()

mem_percent=psutil.virtual_memory().percent

Message = None

if cpu_percent > 80 or mem_percent > 80 ;

Message = "High CPU or Memory Utilization detected. Please Scale up"

return "cpu_utiliztion: {cpu_percent} and memory utilization: {mem_percent}"

if __name_ =='__main__'

app.run=(debug=True,host=0.0.0.0)

then we need to make sure that psutil library is present, hence we will be installing pip.but we need all the dependencies under requirements.txt so we will be keeping all the dependencies under requirements.txt

requirements.txt

Flask==2.2.3

Markupsafe==2.1.2

werkzung==2.2.3

itsdangerous==2.1.2

psutil==5.8.0

plotly==5.5.0

tenacity==8.0.1

boto3==1.9.148

kubernetes==10.0.1

pip3 install psutil

pip3 install -r requirements.txt

python3 app.py

Now we need to containerize this application using Docker.

Dockerfile:-

From python:3.9-slim-buster

WORKDIR /app

COPY requirments.txt .

RUN pip3 install --no-cache-dir -r requirments.txt

COPY . .

ENV FLASK_RUN_HOST= 0.0.0.0

EXPOSE 5000

CMD ["flask", "run"]

after creating the docker file we need to run the docker file to create an image out of it.

Docker build -t my-flask-app.

now we can check if the image has been built with the help of

Docker images

it will the list the image which we have just created.Once the images has been created we can run that image to make the container as a running application

docker run -p 5000:5000 my-flask-app