├── README.md ├── requirements.txt ├── Dockerfile ├── ecr.py ├── app.py ├── eks.py └── templates └── index.html /README.md: -------------------------------------------------------------------------------- 1 | # System-Monitoring-Website 2 | Cloud Native Resource Monitoring Python App on K8s 3 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Flask==2.2.3 2 | MarkupSafe==2.1.2 3 | Werkzeug==2.2.3 4 | itsdangerous==2.1.2 5 | psutil==5.8.0 6 | plotly==5.5.0 7 | tenacity==8.0.1 8 | boto3==1.9.148 9 | kubernetes==10.0.1 10 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.9-buster 2 | 3 | WORKDIR /app 4 | 5 | COPY requirements.txt . 6 | 7 | RUN pip3 install --no-cache-dir -r requirements.txt 8 | 9 | COPY . . 10 | 11 | ENV FLASK_RUN_HOST=0.0.0.0 12 | 13 | EXPOSE 5000 14 | 15 | CMD ["flask", "run"] -------------------------------------------------------------------------------- /ecr.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | 3 | ecr_client = boto3.client('ecr') 4 | 5 | repository_name = "my_monitoring_app_image_nk" 6 | response = ecr_client.create_repository(repositoryName=repository_name) 7 | 8 | repository_uri = response ['repository']['repositoryUri'] 9 | print(repository_uri) 10 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | import psutil 2 | from flask import Flask, render_template 3 | 4 | app = Flask(__name__) 5 | 6 | @app.route("/") 7 | def index(): 8 | cpu_metric = psutil.cpu_percent() 9 | mem_metric = psutil.virtual_memory().percent 10 | Message = None 11 | if cpu_metric > 80 or mem_metric > 80: 12 | Message = "High CPU or Memory Detected, scale up!!!" 13 | return render_template("index.html", cpu_metric=cpu_metric, mem_metric=mem_metric, message=Message) 14 | 15 | if __name__=='__main__': 16 | app.run(debug=True, host = '0.0.0.0') -------------------------------------------------------------------------------- /eks.py: -------------------------------------------------------------------------------- 1 | 2 | #create deployment and service 3 | from kubernetes import client, config 4 | 5 | # Load Kubernetes configuration 6 | config.load_kube_config() 7 | 8 | # Create a Kubernetes API client 9 | api_client = client.ApiClient() 10 | 11 | # Define the deployment 12 | deployment = client.V1Deployment( 13 | metadata=client.V1ObjectMeta(name="my-flask-app"), 14 | spec=client.V1DeploymentSpec( 15 | replicas=1, 16 | selector=client.V1LabelSelector( 17 | match_labels={"app": "my-flask-app"} 18 | ), 19 | template=client.V1PodTemplateSpec( 20 | metadata=client.V1ObjectMeta( 21 | labels={"app": "my-flask-app"} 22 | ), 23 | spec=client.V1PodSpec( 24 | containers=[ 25 | client.V1Container( 26 | name="my-flask-container", 27 | image="", 28 | ports=[client.V1ContainerPort(container_port=5000)] 29 | ) 30 | ] 31 | ) 32 | ) 33 | ) 34 | ) 35 | 36 | # Create the deployment 37 | api_instance = client.AppsV1Api(api_client) 38 | api_instance.create_namespaced_deployment( 39 | namespace="default", 40 | body=deployment 41 | ) 42 | 43 | # Define the service 44 | service = client.V1Service( 45 | metadata=client.V1ObjectMeta(name="my-flask-service"), 46 | spec=client.V1ServiceSpec( 47 | selector={"app": "my-flask-app"}, 48 | ports=[client.V1ServicePort(port=5000)] 49 | ) 50 | ) 51 | 52 | # Create the service 53 | api_instance = client.CoreV1Api(api_client) 54 | api_instance.create_namespaced_service( 55 | namespace="default", 56 | body=service 57 | ) -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | System Monitoring 5 | 6 | 14 | 15 | 16 |
17 |

System Monitoring

18 |
19 |
20 | {% if message %} 21 |
{{ message }}
22 | {% endif %} 23 |
24 | 77 | 78 | --------------------------------------------------------------------------------