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%.
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.
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.
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.
A Python-based script was developed to monitor CPU usage. The script:
- Uses the
psutillibrary to fetch CPU utilization. - Continuously checks CPU usage at regular intervals.
- If usage exceeds 75%, it triggers the migration script.
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.
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.
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
nohupcommand for background execution.
After migration, the system verifies that the task is actively running on the GCP instance:
- Process Monitoring: Uses
ps auxto 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.
- Local VM Setup: A Flask app runs on the local VM, offering two endpoints (
/statusand/compute). - CPU Monitoring: A monitoring script tracks CPU usage.
- Auto-Scaling Trigger: If CPU usage exceeds 75%, the system triggers the auto-scaling mechanism.
- Cloud VM Activation: The GCP instance is checked and started if not running.
- Task Migration: The high-CPU task is transferred to GCP and executed in the background.
- Verification: After migration, logs are checked to ensure the task is running on GCP.
Below is a diagram illustrating the architecture of the system
- Grafana Documentation – Guide on creating dashboards and visualizing metrics.
- Google Cloud Compute Engine – Information on managing VM instances in Google Cloud.
- Python Subprocess Module – Used for handling process execution and migration in
autoscale.py. - VirtualBox Documentation – Official documentation for setting up VMs and configuring networking.
- Auto-Scaling Best Practices – Cloud-based resource scaling strategies.
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.
