Intelligent auto-balancer for VMManager 6 virtual machines
Project description
VMManager 6 Auto-Balancer
An intelligent auto-balancer for VMManager 6 that automatically redistributes virtual machines across cluster nodes to optimize resource utilization and prevent overloading.
🚀 Features
- 🔄 Automatic Load Balancing: Intelligent VM migration from overloaded to underloaded nodes
- 🖥️ Interactive Console UI: Rich terminal interface for real-time management
- 🎯 Smart Filtering: Target specific clusters and exclude problematic nodes
- ⚡ Configurable Thresholds: Flexible CPU and memory thresholds for optimization
- 🛡️ Safety First: Built-in checks for VM constraints, limits, and maintenance mode
- 🧪 Dry Run Mode: Test balancing strategies without actual migrations
- 📊 Detailed Logging: Comprehensive logs for monitoring and troubleshooting
- 🔁 Batch Processing: Configurable migration limits per cycle for controlled balancing
📋 Requirements
- Python 3.8+
- VMManager 6 with API access
- Network connectivity to VMManager API endpoint
🔧 Installation
📦 From Package (Recommended)
-
Install from PyPI (when published):
pip install vm-balancer
-
Or install from source:
git clone https://github.com/your-username/vm_balancer.git cd vm_balancer pip install .
-
Configure settings:
cp config.env.example .env # Edit .env with your VMManager details
🛠️ Development Installation
-
Clone the repository:
git clone https://github.com/your-username/vm_balancer.git cd vm_balancer
-
Create virtual environment:
python3 -m venv .venv source .venv/bin/activate # Linux/Mac # or .venv\Scripts\activate # Windows
-
Install in development mode:
pip install -e .
-
Configure settings:
cp config.env.example .env # Edit .env with your VMManager details
⚙️ Configuration
Environment Variables
Create a .env file with your VMManager settings:
# VMManager connection
VMMANAGER_HOST=https://your-vmmanager.com
VMMANAGER_USERNAME=admin
VMMANAGER_PASSWORD=your_password
# Balancing settings
BALANCE_INTERVAL=600 # Check interval (seconds)
CLUSTER_IDS=1,2,3 # Target clusters (empty = all)
MAX_MIGRATIONS_PER_CYCLE=1 # Migrations per cycle
# Load thresholds
CPU_OVERLOAD_THRESHOLD=7.0 # CPU allocation ratio trigger
MEMORY_OVERLOAD_THRESHOLD=70.0 # Memory usage % trigger
CPU_TARGET_THRESHOLD=6.0 # CPU target ratio
MEMORY_TARGET_THRESHOLD=80.0 # Memory target %
# Node exclusions
EXCLUDE_SOURCE_NODES=node1,node2 # Exclude as sources
EXCLUDE_TARGET_NODES=node3,node4 # Exclude as targets
# Logging
LOG_LEVEL=INFO # DEBUG, INFO, WARNING, ERROR
# SSH Monitoring (Optional)
SSH_ENABLED=false # Enable SSH load monitoring
SSH_USERNAME=root # SSH username
SSH_PRIVATE_KEY_PATH=/path/to/key # SSH private key path
SSH_PASSWORD= # SSH password (if no key)
SSH_TIMEOUT=10 # SSH timeout (seconds)
SSH_HOSTS_MAPPING={"node1": "192.168.1.10"} # Node name to IP mapping
# Telegram Notifications (Optional)
TELEGRAM_BOT_TOKEN= # Bot token
TELEGRAM_CHAT_ID= # Chat ID
SSH Load Monitoring
The balancer can optionally use SSH to get real-time load average from cluster nodes instead of relying only on VMManager API data. This provides more accurate CPU load information.
Benefits:
- Real-time load average (1min, 5min, 15min) from
/proc/loadavg - More accurate than vCPU allocation ratios
- Better migration decisions based on actual system load
Setup:
- Enable SSH monitoring in configuration
- Configure SSH credentials (key or password authentication)
- API automatically provides IP, port, and username
- Optionally override with custom hostname mapping
SSH Authentication Options:
Key-based authentication (recommended):
SSH_ENABLED=true
SSH_PRIVATE_KEY_PATH=/root/.ssh/id_rsa
SSH_USERNAME= # Optional fallback, API provides username
Password-based authentication:
SSH_ENABLED=true
SSH_PASSWORD=your_ssh_password
SSH_USERNAME= # Optional fallback, API provides username
Example SSH configuration:
# Enable SSH monitoring
SSH_ENABLED=true
SSH_TIMEOUT=10
# Authentication (choose one method)
SSH_PRIVATE_KEY_PATH=/root/.ssh/id_rsa # Key-based
# SSH_PASSWORD=mysecretpassword # Password-based
# Optional: Override API-provided hostnames
SSH_HOSTS_MAPPING='{
"node-1": "192.168.1.10",
"node-2": "192.168.1.11",
"node-3": "node3.example.com"
}'
SSH options:
--ssh-enabled # Enable SSH monitoring
--ssh-username root # SSH username
--ssh-private-key /path/to/key # SSH private key
--ssh-password mypassword # SSH password
--ssh-timeout 10 # Connection timeout
--ssh-hosts-mapping '{"node1":"ip"}' # JSON hostname mapping
Command Line Options
# Connection
--host https://vmmanager.example.com # VMManager URL
--username admin # Username
--password mypassword # Password
--cluster-ids 1 2 3 # Target clusters
# Thresholds
--cpu-overload-threshold 7.0 # CPU overload trigger
--memory-overload-threshold 70.0 # Memory overload trigger
--cpu-target-threshold 6.0 # CPU target limit
--memory-target-threshold 80.0 # Memory target limit
# Node filtering
--exclude-source-nodes node1 node2 # Exclude sources
--exclude-target-nodes node3 node4 # Exclude targets
# Migration control
--max-migrations-per-cycle 3 # Max migrations per cycle
# Operation modes
--once # Single run
--dry-run # Simulation mode
--interval 600 # Continuous mode interval
--log-level DEBUG # Logging level
--verify-ssl # SSL verification
🎮 Usage
Command Line Interface
After installation, you can use the vm-balancer command:
First Run (Recommended)
# Test without making changes
vm-balancer --dry-run --once --log-level DEBUG
Single Balancing Run
vm-balancer --once
Continuous Monitoring
vm-balancer --interval 300
Cluster-Specific Balancing
vm-balancer --cluster-ids 1 3 5 --dry-run
Python Module Usage
You can also use the package as a Python module:
# Using the module directly
python -m vm_balancer --help
# Or import in your code
python -c "from vm_balancer import VMBalancer; print('Available')"
Configuration File
The balancer looks for configuration in the following order:
- Command line arguments
- Environment variables
.envfile in current directory- Default values
Advanced Usage
# Fast balancing with multiple migrations
vm-balancer --max-migrations-per-cycle 3 --once
# Conservative balancing with exclusions
vm-balancer --exclude-source-nodes problematic-node \
--exclude-target-nodes maintenance-node \
--max-migrations-per-cycle 1
🧠 How It Works
🔍 Overload Detection
A node is considered overloaded when:
- CPU allocation ratio > threshold (default: 7:1 vCPU:pCPU)
- OR Memory usage > threshold (default: 70%)
- AND Node is not in maintenance mode
- AND Node is not excluded from migrations
🎯 Target Selection Logic
The target node selection process follows a sophisticated multi-stage algorithm:
Stage 1: Initial Candidate Filtering
A node qualifies as a potential target when:
- Node State: Not in maintenance mode
- VM Creation: VM creation is allowed on the node
- VM Limits: Under configured VM limit (if set)
- Exclusions: Not in the excluded target nodes list
- CPU Capacity: CPU load score < target threshold (default: 6.0)
- Memory Capacity: Memory usage < target threshold (default: 80%)
Stage 2: VM-Specific Compatibility Check
For each VM migration, the system verifies:
- Resource Estimation: VM resources won't cause node overload after migration
- Estimated CPU ratio:
(current_cpu + vm_cpu) / total_cpu < overload_threshold - Estimated memory usage:
current_memory% + (vm_memory/total_memory)*100 < overload_threshold
- Estimated CPU ratio:
- QEMU Compatibility: Target node QEMU version ≥ source node QEMU version
- Live Capacity: Real-time resource availability
Stage 3: Selection Priority
From compatible nodes, selection prioritizes:
- Lowest CPU load score (combination of allocation ratio + VM density)
- Lowest memory usage percentage
- First available node meeting all criteria
CPU Load Score Calculation
The system uses an advanced CPU scoring algorithm:
cpu_load_score = allocation_ratio + (vm_density_factor * 0.5)
Where:
allocation_ratio= allocated_vCPUs / physical_CPUsvm_density_factor= min(vm_count / physical_CPUs, 2.0)
This considers both resource allocation and management complexity.
🔄 VM Selection Strategy
Migration candidates must be:
- ✅ Currently running (active state)
- ✅ No mounted ISO images
- ✅ No active snapshots
- ✅ Balancer enabled for VM
- ✅ Not migrated in last hour
- ✅ Priority: Smaller VMs first (less disruptive)
🛡️ Safety Features
- Migration Limits: Configurable per-cycle limits prevent system overload
- State Validation: Pre-migration VM and node state verification
- Recent Migration Tracking: Prevents VM ping-ponging
- Resource Compatibility: QEMU version and resource requirement checks
- Dry Run Mode: Test strategies without actual changes
📊 Monitoring & Logs
All activities are logged to vm_balancer.log and console output.
Log Levels:
- DEBUG: Detailed node and VM analysis
- INFO: Migration decisions and status updates
- WARNING: Potential issues and skipped operations
- ERROR: Failures and critical problems
Sample Log Output:
2024-01-15 10:30:00 [INFO] Starting balance cycle for 3 clusters
2024-01-15 10:30:01 [INFO] Cluster 'Production' (ID: 1) - Found 2 overloaded nodes
2024-01-15 10:30:02 [INFO] Migrating VM 'web-server-01' from 'node-heavy' to 'node-light'
2024-01-15 10:30:45 [INFO] Migration completed successfully in 43 seconds
🤝 Contributing
We welcome contributions! Please see CONTRIBUTING.md for guidelines.
Development Setup
- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Make your changes
- Add tests if applicable
- Submit a pull request
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
📦 Releases
Latest Release
Download the latest version from GitHub Releases
Available Packages
- Source Code: Complete source with all files
- Portable Package: Ready-to-run for Linux/Mac/Windows
- Windows Package: Optimized for Windows with setup scripts
Quick Install from Release
# Linux/Mac
wget https://github.com/DenisKoleda/vm_balancer/releases/latest/download/vm-balancer-X.X.X-portable.tar.gz
tar -xzf vm-balancer-X.X.X-portable.tar.gz
cd vm-balancer-portable && ./run.sh
# Windows: Download vm-balancer-X.X.X-windows.zip and run install.bat
🔗 Using the VM Balancer as a library allows you to integrate its functionality into your own applications or scripts
Simple Example
from vm_balancer import VMBalancer
import asyncio
async def main():
# Create a balancer with configuration
balancer = VMBalancer(config_path='.env', dry_run=True)
# Perform a single balancing run
await balancer.run_once()
if __name__ == "__main__":
asyncio.run(main())
Component Usage
from vm_balancer import VMManagerAPI, TelegramNotifier
# API client
api = VMManagerAPI(
host="https://vmmanager.example.com",
username="admin",
password="password"
)
# Notifications
notifier = TelegramNotifier(
bot_token="your_bot_token",
chat_id="your_chat_id",
enabled=True
)
# Get clusters
clusters = await api.get_clusters()
🔗 Related Projects
⚠️ Disclaimer
This tool performs live VM migrations. Always test in a development environment first. The authors are not responsible for any data loss or service disruption.
📞 Support
- 🐛 Bug Reports: GitHub Issues
- 💡 Feature Requests: GitHub Discussions
- 📖 Documentation: Wiki
- 📋 Release Guide: RELEASE.md
Made with ❤️ for the VMManager community
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file vm_balancer-2.0.1.tar.gz.
File metadata
- Download URL: vm_balancer-2.0.1.tar.gz
- Upload date:
- Size: 62.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
120614ff6e970e1eed9d9fb56d019558e28b7be853c042bac50467df6a77da35
|
|
| MD5 |
3b03e20be910e0ea39761a6780cb809b
|
|
| BLAKE2b-256 |
c6a30628f318ad2a4b6cfa5e9bfb6c3b85296319675a3dc9b7dde1947a77f8a5
|
File details
Details for the file vm_balancer-2.0.1-py3-none-any.whl.
File metadata
- Download URL: vm_balancer-2.0.1-py3-none-any.whl
- Upload date:
- Size: 34.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd3acd90e33e5ba8f6afb346db64556720137d78b227f8508d41403965120bfa
|
|
| MD5 |
a0683b6380814c35f048c32d2d09e29e
|
|
| BLAKE2b-256 |
94d6ece29d13e028837dfa8d9ae5eabde15024c350839f9eee45cf2955280d0e
|