https://github.com/daoud-hussain/cloud-native-monitoring
Deploying Cloud Native app on K8S to monitor system resources using python.
https://github.com/daoud-hussain/cloud-native-monitoring
Last synced: 3 months ago
JSON representation
Deploying Cloud Native app on K8S to monitor system resources using python.
- Host: GitHub
- URL: https://github.com/daoud-hussain/cloud-native-monitoring
- Owner: Daoud-Hussain
- License: mit
- Created: 2024-08-15T05:40:37.000Z (10 months ago)
- Default Branch: master
- Last Pushed: 2024-08-16T07:05:36.000Z (10 months ago)
- Last Synced: 2025-01-08T07:16:28.923Z (5 months ago)
- Language: HTML
- Homepage:
- Size: 2.11 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# **Cloud Native Resource Monitoring Python App on K8s!**
## Things you will Learn π€―
1. Python and How to create Monitoring Application in Python using **Flask** and **psutil**.
2. How to run a Python App locally.
3. Learn Docker and How to containerize a Python application.
- Creating Dockerfile.
- Building DockerImage.
- Running Docker Container.
- Docker Commands.
4. Create ECR repository using Python 'Boto3' and pushing Docker Image to ECR.
5. Learn Kubernetes and Create EKS cluster and Nodegroups.
6. Create Kubernetes Deployments and Services using Python.## Architecture Diagram π
## **Prerequisites** π
(Things to have before starting the projects)
- [x] AWS Account.
- [x] Programmatic access and AWS configured with CLI.
- [x] Python3 Installed.
- [x] Docker and Kubectl installed.
- [x] Code editor (Vscode)# β¨Letβs Start the Project β¨
## **Part 1: Deploying the Flask application locally**
### **Step 1: Clone the code**
Clone the code from the repository:
```
git clone https://github.com/Daoud-Hussain/Cloud-Native-Monitoring.git
```### **Step 2: Install dependencies**
The application uses the **`psutil`** and **`Flask`, Plotly, boto3** libraries. Install them using pip:
```
pip3 install -r requirements.txt
```### **Step 3: Run the application**
To run the application, navigate to the root directory of the project and execute the following command:
```
python3 app.py
```This will start the Flask server on **`localhost:5000`**. Navigate to [http://localhost:5000/](http://localhost:5000/) on your browser to access the application.
## **Part 2: Dockerizing the Flask application**
### **Step 1: Create a Dockerfile**
Create a **`Dockerfile`** in the root directory of the project with the following contents:
```
# Use the official Python image as the base image
FROM python:3.9-slim-buster# Set the working directory in the container
WORKDIR /app# Copy the requirements file to the working directory
COPY requirements.txt .RUN pip3 install --no-cache-dir -r requirements.txt
# Copy the application code to the working directory
COPY . .# Set the environment variables for the Flask app
ENV FLASK_RUN_HOST=0.0.0.0# Expose the port on which the Flask app will run
EXPOSE 5000# Start the Flask app when the container is run
CMD ["flask", "run"]
```### **Step 2: Build the Docker image**
To build the Docker image, execute the following command:
```
docker build -t .
```### **Step 3: Run the Docker container**
To run the Docker container, execute the following command:
```
docker run -p 5000:5000
```This will start the Flask server in a Docker container on **`localhost:5000`**. Navigate to [http://localhost:5000/](http://localhost:5000/) on your browser to access the application.
## **Part 3: Pushing the Docker image to ECR**
### **Step 1: Create an ECR repository**
You can create an ECR repository programmatically using Python `Boto3`. Boto3 is the name of the Python SDK for AWS. It allows you to directly create, update, and delete AWS resources from your Python scripts. You can do it manually using Amazon ECR service.
```
import boto3# Create an ECR client
ecr_client = boto3.client('ecr')# Create a new ECR repository
repository_name = 'my-ecr-repo'
response = ecr_client.create_repository(repositoryName=repository_name)# Print the repository URI
repository_uri = response['repository']['repositoryUri']
print(repository_uri)
```### **Step 2: Push the Docker image to ECR**
Push the Docker image to ECR using the push commands on the console:
```
docker push :
```
## **Part 4: Creating an EKS cluster and deploying the app using Python**
### **Step 1: Create an EKS cluster**
Create an EKS cluster and add node group
![]()
![]()
![]()
![]()
![]()
![]()
![]()
### **Step 2: Create a node group**
Create a node group in the EKS cluster.
![]()
![]()
![]()
![]()
![]()
### **Step 3: Create deployment and service**
In Kubernetes, a **Deployment** manages the creation and scaling of pods, ensuring the desired number are running, while a **Service** exposes the deployed pods to external or internal network traffic, enabling communication between components.```jsx
from kubernetes import client, config# Load Kubernetes configuration
config.load_kube_config()# Create a Kubernetes API client
api_client = client.ApiClient()# Define the deployment
deployment = client.V1Deployment(
metadata=client.V1ObjectMeta(name="my-flask-app"),
spec=client.V1DeploymentSpec(
replicas=1,
selector=client.V1LabelSelector(
match_labels={"app": "my-flask-app"}
),
template=client.V1PodTemplateSpec(
metadata=client.V1ObjectMeta(
labels={"app": "my-flask-app"}
),
spec=client.V1PodSpec(
containers=[
client.V1Container(
name="my-flask-container",
image="public.ecr.aws/r3n6z2g2/my_monitoring_app_image:latest",
ports=[client.V1ContainerPort(container_port=5000)]
)
]
)
)
)
)# Create the deployment
api_instance = client.AppsV1Api(api_client)
api_instance.create_namespaced_deployment(
namespace="default",
body=deployment
)# Define the service
service = client.V1Service(
metadata=client.V1ObjectMeta(name="my-flask-service"),
spec=client.V1ServiceSpec(
selector={"app": "my-flask-app"},
ports=[client.V1ServicePort(port=5000)]
)
)# Create the service
api_instance = client.CoreV1Api(api_client)
api_instance.create_namespaced_service(
namespace="default",
body=service
)
```Make sure to edit the name of the image on line 26 with your image Uri.
- Once you run this file by running βpython3 eks.pyβ deployment and service will be created.
- Check by running following commands:```jsx
kubectl get deployment -n default (check deployments)
kubectl get service -n default (check service)
kubectl get pods -n default (to check the pods)
```Once your pod is up and running, run the port-forward to expose the service
```bash
kubectl port-forward service/ 5000:5000
```## Output π
![]()
## Contributing ποΈ
Pull requests are welcome for any changes.
## Author πββ
- [Daoud Hussain](https://www.linkedin.com/in/daoud-hussain/) on LinkedIn.
- You Can also check out my [Medium](https://medium.com/@dev.daoudhussain) for articles on DevOps Tools and Technologies.οΈ