ππ«ππππππππ π πͺππππ π΅πππππ π΄πππππππππ π¨ππππππππππ ππ π²ππππππππππ
Introduction:
Starting from scratch, we will craft a Python monitoring app with Flask and run it locally on port 5000. Then came the fun part: containerization with Docker, building images, and launching Docker containers easily! π³
But that's not all! we embrace the power of AWS, creating an ECR repository to push my Docker images effortlessly. Then it was time to dive into EKS, where we forged clusters, deployed services, and used Python scripts to orchestrate the magic of Kubernetes.
With tools like AWS, Docker, Kubernetes, and more, this project has been an exhilarating journey filled with learning and growth. Join me as I share the details, challenges, and triumphs of deploying this cloud-native monitoring application.
A step-by-step procedure for deploying a Cloud Native Monitoring Application on Kubernetes.
Prerequisites:
1) AWS Account:
Follow below official site below to create an AWS account https://docs.aws.amazon.com/accounts/latest/reference/manage-acct-creating.html
2) GitHub Account
To create a GitHub account:
πΈLogin to https://github.com/
πΈProvide username, email, and password
πΈVerify account
πΈClick on Join a free plan
πΈYou will get some questions, so fill it
πΈClick on complete setup
πΈGo to Gmail and verify your account
**
3) Python3 Installed**
Follow python official website to install (https://www.python.org/downloads/)
4)Docker Installed
Follow docker official website to install (https://docs.docker.com/get-docker/)
Installation steps:
πΉsudo apt-get update βy
πΉsudo apt-get install docker.io βy
πΉsudo usermod -aG docker $USER # Replace with your system's username, e.g., 'ubuntu'
πΉnewgrp docker
πΉsudo chmod 777 /var/run/docker.sock
πΉsudo systemctl restart docker
πΉsudo systemctl status docker
5)Kubectl Installed
https://kubernetes.io/docs/tasks/tools/
Installation steps:
βΌοΈcurl -LO [dl.k8s.io/release/$(curl](https://dl.k8s.io.. -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl/bin/linux/amd64/kubectl)
βΌοΈsudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
βΌοΈchmod +x ./kubectl
βΌοΈsudo mv ./kubectl /usr/local/bin/kubectl
βΌοΈkubectl version βclient
6)AWS configured with CLI
Navigate to the IAM dashboard on AWS, then select "Users." Enter the username and proceed to the next step
Assign permissions by attaching policies directly, opting for "Administrator access," and then create the user.
Within the user settings, locate "Create access key," and choose the command line interface (CLI) option to generate an access key.
Upon creation, you can view or download the access key and secret access key either from the console or via CSV download.
Now go to your terminal and follow the below steps:
π sudo apt install unzip
π curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" unzip awscliv2.zip
π sudo ./aws/install aws configure (input created access key id and secret access key)
π cat ~/.aws/config
π cat ~/.aws/credentials
π aws iam list-users (to list all IAM users in an AWS account)
7) Code editor (Vscode)**
Utilize Visual Studio Code or any other platform that supports Python application execution according to your preference (https://code.visualstudio.com/)
Let's start the project:
Step1: Make sure to have all above prerequisites installed
Step2: Checkout the code present on below GitHub repo by nasiullah choudhari (cloud champ β creator of this project)
https://github.com/N4si/cloud-native-monitoring-app
Now go to your terminal and clone the code in to your local machine
The app directory contain below files which can be used as follows:
β‘app.py: This Python script utilizes the psutil library to monitor CPU and memory usage. It creates a Flask web app that displays these metrics. If either metric exceeds 80%, it prompts to scale up resources.
β‘requirements.txt: contains all necessary dependencies for this project
β‘Dockerfile: This Dockerfile sets up a Python environment based on Python 3.9 on Debian Buster. It installs the required dependencies specified in requirements.txt using pip3. Then, it exposes port 5000 and configures Flask to run on all network interfaces. Finally, it starts the Flask application using flask run.
β‘ecr.py: This Python script utilizes the Boto3 library to interact with the Amazon Elastic Container Registry (ECR). It creates a new repository named "my_monitoring_app_image" using the create_repository method of the ECR client. It then retrieves the URI of the newly created repository and prints it to the console. Essentially, this code automates the process of creating an ECR repository for storing container images.
β‘eks.py: This script automates Flask application deployment to Kubernetes by creating a Deployment and Service. It defines a Deployment for the Flask app and a Service to expose it.
Step3: To install required dependencies using pip:
pip3 install psutil (a python library which retrieves info about system utilization)
pip3 install -r requirements.txt
step4: Run python3 app.py and check if the app is accessible in UI.
This is how we can run this monitoring application in local host
Lets containerize this application using docker:
Step5: write Dockerfile (present in git repo) and create an image from it
command: docker build βt my-flask-app . (-t = tag image with specific name, . = current directory)
To check the images created use : docker images
Step6:
Now to create a container, run below
command: command: docker run βp 5000:5000 d39bbeb4f1e8 -> starts a Docker container from the image with ID d39bbeb4f1e8 and maps port 5000 on the host machine to port 5000 inside the container.
You can access in browser via localhost:5000, which means your application running fine on docker container.
Step7: To push the image to ecr registry
To create a repo using python script (here refer to ecr.py script in git repo)
command: python3 ecr.py
This will create an ecr repo in aws using python
Step8: To push the image in ecr in the created repository . click on view push commands and execute all commands one by one to push the image in to ecr.
Now you can view the image has been pushed in to the created repo.
Step9: To deploy into Kubernetes cluster,
Navigate to EKS dashboard -> create a cluster
Offer the name, default Kubernetes version, cluster service role, default VPC, preferred subnets, a valid security group, and an IPV4 cluster IP, which can be set to either public or private based on your needs. Supplying these particulars is adequate for cluster creation.
Once the cluster is active, we need to create node groups on this cluster as follows:
Choosing these options will create 2 nodes inside your cluster
If you encounter permission issues when pulling an image from ECR, review the permissions assigned in IAM. Ensure that the necessary policies are attached to grant the appropriate access to ECR resources
Step10: To deploy into the Kubernetes cluster, we use 2 concepts
1) Kubernetes deployment: Kubernetes Deployment is a way to manage and control how your application runs on Kubernetes. It specifies what container images to use, how many instances of those containers should run, and how to update them over time
2) Kubernetes service: Kubernetes Service is a way to make your application accessible to other parts of your Kubernetes cluster or the outside world
We can also use minikube in case of deploying a single-node kubernetes cluster as follows: