Skip to content

arkya-art/flask-autoscaling

Repository files navigation

Flask Auto-Scaling with Google Cloud Platform (GCP)

Objective

The purpose of this project is to set up a local Virtual Machine (VM) that monitors CPU usage and automatically scales workloads to Google Cloud Platform (GCP) when CPU usage exceeds 75%.

Table of Contents

Project Overview

This project simulates a real-world workload scenario where a local VM is monitored for CPU usage. Once CPU usage exceeds 75%, the system automatically migrates the high-CPU tasks to a Google Cloud Platform (GCP) VM. This allows the local system to maintain performance while scaling workloads seamlessly to the cloud.

Setup Instructions

1. Creation of a Local Virtual Machine

1.1 Virtual Machine Setup

To simulate a local environment, we set up a Virtual Machine (VM) using VirtualBox. The VM was configured with the following settings:

  • OS: Ubuntu 18.04 LTS
  • Memory: 4GB RAM
  • CPU: 2 cores
  • Storage: 20GB (dynamically allocated)
  • Network: NAT mode with port forwarding for SSH access

Once the VM was created, we updated it with the latest security patches and installed the necessary dependencies like Python, SSH, and monitoring tools. SSH was configured to allow remote access to the VM.

1.2 Deploying a Sample Application

A Flask-based web application was deployed on the VM to simulate real-world usage. The app exposes two routes:

  • /status: Confirms that the app is running.
  • /compute: Initiates a CPU-intensive task that increases CPU usage.

This app acts as the proof-of-concept for testing resource consumption and auto-scaling behavior.

2. Resource Monitoring Implementation

2.1 Monitoring CPU Usage

A Python-based script was developed to monitor CPU usage. The script:

  • Uses the psutil library to fetch CPU utilization.
  • Continuously checks CPU usage at regular intervals.
  • If usage exceeds 75%, it triggers the migration script.

2.2 Simulating High-CPU Workload

A high-CPU task script was created that runs matrix multiplication for a predefined duration, simulating an intense workload. This helps in verifying the monitoring system’s response to high resource usage.

3. Cloud Auto-Scaling Configuration

3.1 Creating a Google Cloud Compute Instance

To handle the migrated workloads, a Google Cloud Compute Engine VM was created with the following configuration:

  • Machine Type: e2-medium
  • vCPUs: 2
  • RAM: 4GB
  • SSH Access: Enabled for remote execution
  • Firewall Rules: HTTP traffic permitted

This cloud instance acts as a backup system to offload workloads from the local VM.

3.2 Migrating High-CPU Tasks to GCP

Once the CPU usage exceeds 75%, the system triggers a migration script that:

  • Verifies if the GCP instance is running. If not, it starts the instance.
  • Establishes an SSH connection to the cloud instance.
  • Transfers the high-CPU task and executes it remotely using the nohup command for background execution.

4. Verifying Migration and Logs

After migration, the system verifies that the task is actively running on the GCP instance:

  • Process Monitoring: Uses ps aux to check the running processes on the cloud instance.
  • Logs Retrieval: Logs are collected from the GCP instance to track execution details.
  • SSH Monitoring: SSH connections were used to verify the CPU usage and ensure the task was running as expected.

The verification ensures that auto-scaling works seamlessly, with workloads migrating to the cloud without disrupting the local system.

System Workflow

  1. Local VM Setup: A Flask app runs on the local VM, offering two endpoints (/status and /compute).
  2. CPU Monitoring: A monitoring script tracks CPU usage.
  3. Auto-Scaling Trigger: If CPU usage exceeds 75%, the system triggers the auto-scaling mechanism.
  4. Cloud VM Activation: The GCP instance is checked and started if not running.
  5. Task Migration: The high-CPU task is transferred to GCP and executed in the background.
  6. Verification: After migration, logs are checked to ensure the task is running on GCP.

Architecture Design

Below is a diagram illustrating the architecture of the system

System Architecture

References

Conclusion

This project demonstrates the integration of local and cloud-based environments to manage resource usage effectively. By leveraging auto-scaling with GCP, we can offload high-CPU tasks seamlessly, ensuring optimal performance both on local and cloud infrastructures.

About

Flask-based auto-scaling system that monitors local VM CPU usage and seamlessly migrates high-CPU workloads to Google Cloud Platform (GCP) for scalable performance

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Packages

 
 
 

Contributors