This is a Python application for monitoring resource usage in a cloud-native environment, such as Kubernetes. The app is built using Flask and psutil, and it can be run locally or containerized with Docker.
- Monitor CPU, memory, disk, and network usage of your Kubernetes nodes
- Visualize resource usage using interactive charts and graphs
- Easily deploy the application using Kubernetes Deployments and Services
- Containerized using Docker and pushed to AWS ECR for easy deployment on Kubernetes
- Python3
- Flask
- psutil
- AWS
- Docker
- Kubernetes
Step 1: Clone the code
Clone the code from the repository:
$ git clone https://github.com/ashwaq06/Cloud-native-monitoring-app
Step 2: Install dependencies
The application uses the psutil, Flask, Plotly, and 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/ on your browser to access the 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 .
# Install dependencies
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 <image_name> .
Step 3: Run the Docker container
To run the Docker container, execute the following command:
$ docker run -p 5000:5000 <image_name>
This will start the Flask server in a Docker container on localhost:5000. Navigate to http://localhost:5000/ on your browser to access the application.
Step 1: Create an ECR repository
Create an ECR repository using Python:
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 command on the console:
$ docker push <ecr_repo_uri>:
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
#deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-flask-app
spec:
replicas: 1
selector:
matchLabels:
app: my-flask-app
template:
metadata:
labels:
app: my-flask-app
spec:
containers:
- name: my-flask-container
image: 568373317874.dkr.ecr.us-east-1.amazonaws.com/my-cloud-native-repo:latest
ports:
- containerPort: 5000
#service
apiVersion: v1
kind: Service
metadata:
name: my-flask-service
spec:
selector:
app: my-flask-app
ports:
- port: 5000
- Make sure to modify the image name with the URI of your image.
- Before applying the configuration of the file, Ensure that your
kubectl
is configured to interact with your EKS cluster by running the following command:
aws eks --region <your-region> update-kubeconfig --name <your-cluster-name>
- Save the above YAML configurations into a file naming it as
eks.yaml
or any other name as per your wish. You can then apply these configurations to your Kubernetes cluster using thekubectl
apply command:
kubectl apply -f eks.yaml
To check the deployments
kubectl get deployment -n default
To check the service
kubectl get service -n default
To check the pods
kubectl get pods -n default
- Once the pod is up and running, you can expose the service using port-forwarding. To do this, run the following command:
kubectl port-forward service/<service_name> 5000:5000
This will allow you to access the service on your local machine at http://localhost:5000.