LXC AutoScale is a resource management daemon that automatically adjusts CPU and memory for LXC containers based on real-time usage metrics. It can operate locally on the Proxmox host or connect remotely via SSH.
LXC AutoScale monitors container resource usage and adjusts CPU and memory allocations based on pre-configured thresholds. It can run locally on the Proxmox host or connect remotely via SSH.
git clone https://github.com/fabriziosalmi/proxmox-lxc-autoscale.git
cd proxmox-lxc-autoscale/lxc_autoscale
If you’re using environment variables instead of YAML configuration, copy the example file and configure it:
cp .env.example .env
nano .env # Edit with your settings
[!WARNING] Never commit
.envfiles containing passwords or sensitive information to version control.
docker build -t lxc-autoscale .
Modify the YAML configuration file (lxc_autoscale.yaml) with the Proxmox host SSH parameters and the required use_remote_proxmox option to make the application execute commands on remote hosts:
use_remote_proxmox: true
ssh_user: "your-ssh-username"
ssh_password: "your-ssh-password"
proxmox_host: "your-proxmox-host-ip-or-hostname"
ssh_port: 22
[!TIP] For better security, use SSH keys instead of passwords by setting
ssh_key_pathand leavingssh_passwordempty.
Using the Default Configuration:
docker run -d --name lxc_autoscale lxc-autoscale
Using a Custom Configuration File:
docker run -d --name lxc_autoscale \
-v /path/to/your/lxc_autoscale.yaml:/app/lxc_autoscale.yaml \
lxc-autoscale
docker logs lxc_autoscale
The easiest way to install (or update) the service:
curl -sSL https://raw.githubusercontent.com/fabriziosalmi/proxmox-lxc-autoscale/main/install.sh | bash
Verify the service is running:
systemctl status lxc_autoscale.service
Example output when the service is running and performing scaling:
root@proxmox:~# systemctl status lxc_autoscale.service
● lxc_autoscale.service - LXC AutoScale Daemon
Loaded: loaded (/etc/systemd/system/lxc_autoscale.service; enabled; preset: enabled)
Active: active (running) since Mon 2024-08-19 01:38:07 CEST; 7s ago
Docs: https://github.com/fabriziosalmi/proxmox-lxc-autoscale
Main PID: 40462 (python3)
Tasks: 1 (limit: 18849)
Memory: 9.1M
CPU: 5.766s
CGroup: /system.slice/lxc_autoscale.service
└─40462 /usr/bin/python3 /usr/local/bin/lxc_autoscale/lxc_autoscale.py
Aug 19 01:38:07 proxmox systemd[1]: Started lxc_autoscale.service - LXC AutoScale Daemon.
Aug 19 01:38:07 proxmox python3[40462]: 2024-08-19 01:38:07 - Starting resource allocation process...
Aug 19 01:38:09 proxmox python3[40462]: 2024-08-19 01:38:09 - Container 1006 is not running. Skipping adjustments.
Aug 19 01:38:12 proxmox python3[40462]: 2024-08-19 01:38:12 - Initial resources before adjustments: 2 cores, 11678 MB>
Aug 19 01:38:12 proxmox python3[40462]: 2024-08-19 01:38:12 - Decreasing cores for container 104 by 2...
Aug 19 01:38:13 proxmox python3[40462]: 2024-08-19 01:38:13 - Final resources after adjustments: 4 cores, 11678 MB me>
Aug 19 01:38:13 proxmox python3[40462]: 2024-08-19 01:38:13 - Resource allocation process completed. Next run in 300
The core of LXC AutoScale is the configuration file, which controls scaling behaviour. This section explains the key parameters.
The main configuration file is located at:
/etc/lxc_autoscale/lxc_autoscale.yaml
Back up the file before making changes:
cp /etc/lxc_autoscale/lxc_autoscale.yaml /etc/lxc_autoscale/lxc_autoscale.yaml.backup
The configuration file uses YAML format. Below is an explanation of the default parameters.
# Default configuration values
DEFAULT:
# Log file path
log_file: /var/log/lxc_autoscale.log
# Lock file path
lock_file: /var/lock/lxc_autoscale.lock
# Backup directory path
backup_dir: /var/lib/lxc_autoscale/backups
# Percentage of CPU cores to reserve (e.g., 10%)
reserve_cpu_percent: 10
# Amount of memory (in MB) to reserve (e.g., 2048 MB)
reserve_memory_mb: 2048
# Start hour for off-peak energy-saving mode (e.g., 10 PM)
off_peak_start: 22
# End hour for off-peak energy-saving mode (e.g., 6 AM)
off_peak_end: 6
# Behaviour mode (e.g., 'normal', 'conservative', 'aggressive')
behaviour: normal
# Default CPU upper threshold (%)
cpu_upper_threshold: 80
# Default CPU lower threshold (%)
cpu_lower_threshold: 20
# Default Memory upper threshold (%)
memory_upper_threshold: 70
# Default Memory lower threshold (%)
memory_lower_threshold: 20
# Default minimum number of CPU cores
min_cores: 1
# Default maximum number of CPU cores
max_cores: 4
# Default minimum memory (MB)
min_memory: 256
# Default core increment
core_min_increment: 1
# Default core max increment: 2
core_max_increment: 2
# Default memory increment (MB)
memory_min_increment: 256
# Default memory decrement (MB)
min_decrease_chunk: 256
ignore_lxc:
- "104" # Update to string format for consistency
# Tier configurations
TIER_webservers:
lxc_containers:
- "102" # Update to string format
cpu_upper_threshold: 70
cpu_lower_threshold: 20
memory_upper_threshold: 80
memory_lower_threshold: 20
min_cores: 1
max_cores: 4
min_memory: 4096
core_min_increment: 1
core_max_increment: 2
memory_min_increment: 1024
min_decrease_chunk: 1024
TIER_other:
lxc_containers:
- "103" # Update to string format
cpu_upper_threshold: 60
cpu_lower_threshold: 20
memory_upper_threshold: 50
memory_lower_threshold: 20
min_cores: 1
max_cores: 2
min_memory: 256
core_min_increment: 1
core_max_increment: 1
memory_min_increment: 128
min_decrease_chunk: 64
poll_interval)How often (in seconds) the daemon polls container metrics. Default: 300. Shorter intervals respond faster but increase host overhead.
cpu_upper_threshold and cpu_lower_threshold)When CPU usage exceeds cpu_upper_threshold, cores are added. When it falls below cpu_lower_threshold, cores are removed.
memory_upper_threshold and memory_lower_threshold)When memory usage exceeds memory_upper_threshold, memory is increased. When it falls below memory_lower_threshold, memory is reduced.
core_min_increment, core_max_increment, memory_min_increment)Control the size of each scaling step for CPU cores and memory.
reserve_cpu_percent and reserve_memory_mb)Reserves a percentage of host CPU and a fixed amount of memory that cannot be allocated to containers.
log_file)Path to the log file. Review logs to monitor behaviour and troubleshoot issues.
energy_mode)When enabled, reduces container resources to their configured minimums during off-peak hours (defined by off_peak_start and off_peak_end).
Tiers allow you to apply different scaling rules to groups of containers.
TIER_TEST:
cpu_upper_threshold: 90
cpu_lower_threshold: 10
memory_upper_threshold: 90
memory_lower_threshold: 10
min_cores: 2
max_cores: 12
min_memory: 1024
lxc_containers:
- 100
- 101
TIER_TEST can be used for non-critical or test containers, allowing wider resource ranges while still freeing resources when usage is low.[!TIP] For more configuration examples check the TIER collection with 40 snippets customized to fit minimal and recommended requirements for the most popular self-hosted applications.
Horizontal scaling clones containers when group-level resource usage exceeds thresholds. This feature is experimental.
HORIZONTAL_SCALING_GROUP_1:
base_snapshot_name: "101"
min_instances: 2
max_instances: 5
starting_clone_id: 99000
clone_network_type: "static" # Options: "static" or "dhcp"
static_ip_range: ["192.168.100.195", "192.168.100.200"] # Leave empty [] if using DHCP
horiz_cpu_upper_threshold: 95
horiz_memory_upper_threshold: 95
group_tag: "horiz_scaling_group_1"
lxc_containers:
- 101
[!WARNING] Horizontal scaling is an experimental feature. Test it thoroughly in a non-production environment before using it in production.
[!NOTE] If using DHCP for network configuration, set
clone_network_type: "dhcp"andstatic_ip_range: [].
Use systemd to manage the LXC AutoScale daemon:
systemctl start lxc_autoscale.service
systemctl stop lxc_autoscale.service
systemctl restart lxc_autoscale.service
systemctl status lxc_autoscale.service
To ensure that LXC AutoScale starts automatically at boot, use:
systemctl enable lxc_autoscale.service
/var/log/lxc_autoscale.log/var/log/lxc_autoscale.jsonTo view the logs in real-time:
tail -f /var/log/lxc_autoscale.log
For the JSON log install and use jq for better readability:
apt install jq -y
cat /var/log/lxc_autoscale.json | jq .
Frequent scaling actions may indicate that thresholds are too tight. Adjust cpu_upper_threshold, memory_upper_threshold, or increment values to reduce unnecessary scaling.
Notifications on scaling events can be sent to one or more endpoints like Email, Gotify, and Uptime Kuma (push webhook). Configure these options in your /etc/lxc_autoscale/lxc_autoscale.yaml configuration file:
# Gotify Configuration
gotify_url: 'http://gotify-host'
gotify_token: 'YOUR_GOTIFY_TOKEN'
# Email (SMTP) Configuration
smtp_server: 'smtp.example.com'
smtp_port: 587
smtp_username: 'your-username'
smtp_password: 'your-password'
smtp_from: 'lxc-autoscale@yourdomain.com'
smtp_to:
- 'admin@yourdomain.com'
- 'alerts@yourdomain.com'
# Uptime Kuma Webhook Configuration
uptime_kuma_webhook_url: 'http://uptime-kuma-host:3001/api/push/YOUR_PUSH_ID?status=up&msg=OK&ping='
[!TIP] You can enable one, multiple, or all notification methods. Leave the configuration empty or remove it if you don’t want to use a specific notification method.
To automatically uninstall LXC AutoScale:
curl -sSL https://raw.githubusercontent.com/fabriziosalmi/proxmox-lxc-autoscale/main/uninstall.sh | bash
To manually remove LXC AutoScale, follow these steps:
# Disable and stop service
systemctl disable lxc_autoscale.service
systemctl stop lxc_autoscale.service
# Use this to force kill the application if needed:
# kill -9 $(ps aux | grep lxc_autoscale | grep -v grep | awk '{print $2}')
# Make backup of configuration file if needed
# cp -rp /etc/lxc_autoscale/lxc_autoscale.yaml /etc/lxc_autoscale.yaml.backup
# Remove files
rm -f /etc/systemd/system/lxc_autoscale.service
rm -rf /usr/local/bin/lxc_autoscale/
rm -rf /etc/lxc_autoscale/
rm -rf /var/lib/lxc_autoscale/
rm -f /var/log/lxc_autoscale.log
rm -f /var/log/lxc_autoscale.json
# Reload systemd
systemctl daemon-reload
If the service fails to start:
python3 -c "import yaml; yaml.safe_load(open('/etc/lxc_autoscale/lxc_autoscale.yaml'))"
If there’s a YAML syntax error, it will be displayed.
pip3 install -r /usr/local/bin/lxc_autoscale/requirements.txt
journalctl -u lxc_autoscale.service -n 50
If containers are not being scaled:
pct list
Check if containers are in the ignore list: Review the ignore_lxc setting in /etc/lxc_autoscale/lxc_autoscale.yaml
Verify LXCFS is configured correctly: See the LXCFS configuration section in the main README
If LXC AutoScale is consuming too many resources:
Increase the poll interval: Set a higher value for poll_interval in the configuration (e.g., 600 seconds instead of 300)
Reduce the number of monitored containers: Add less critical containers to the ignore_lxc list
If you see permission denied errors:
Verify the service is running as root: Check the service file at /etc/systemd/system/lxc_autoscale.service
Check file permissions:
ls -la /etc/lxc_autoscale/
ls -la /var/log/lxc_autoscale.log
After modifying the configuration:
systemctl restart lxc_autoscale.service
systemctl status lxc_autoscale.service
If using remote execution via SSH:
ssh -p <port> <user>@<proxmox_host> "pct list"
Check SSH credentials: Ensure ssh_user, ssh_password (or ssh_key_path), and proxmox_host are correctly set in the configuration
use_remote_proxmox is set to true in the configuration fileA media server container (e.g., Plex or Jellyfin) can have more CPU and memory allocated during peak usage hours. Set cpu_upper_threshold and memory_upper_threshold lower (e.g., 75%) for faster scale-up response.
When running multiple LXC containers for development, assign different tiers with appropriate thresholds and core limits to keep containers from competing for resources.
For a web server container that experiences traffic spikes, horizontal scaling can clone the container automatically. Set a conservative horiz_cpu_upper_threshold to avoid unnecessary clones.
Revisit thresholds and tier settings when adding new containers or when workloads change.
Review log files to understand scaling behaviour. Frequent actions may indicate overly tight thresholds.
Conservative thresholds reduce over-allocation; aggressive thresholds allow faster scaling. Tune based on your workload.
Test configuration changes on non-critical containers before applying to production.
Grouping containers with similar requirements into tiers simplifies management and ensures consistent scaling.