proxmox-lxc-autoscale-ml

LXC AutoScale API examples

The LXC AutoScale API offers powerful capabilities for managing and automating LXC containers on a Proxmox server. By leveraging this API, you can dynamically adjust resources, automate routine tasks, and ensure your containers are running optimally based on real-time conditions. Below are practical examples of how you can use the API to enhance your container management. Each example provides a detailed command that you can implement directly in your environment.

To improve readability and make it easier for readers to quickly find the most interesting points, I suggest organizing the summary section into logical groups based on the nature of the actions being automated. Here’s a revised version with subsections:

Summary

Resource Scaling

Monitoring and Alerts

Backup and Recovery

Cloning and Testing

Maintenance and Cleanup

Reporting

Health and Stability


Dynamic CPU Scaling

Adjust the number of CPU cores based on time of day.

Increase cores at 8:00 AM:

0 8 * * * curl -X POST http://proxmox:5000/scale/cores \
-H "Content-Type: application/json" \
-d '{"vm_id": 104, "cores": 8}'

Decrease cores at 8:00 PM:

0 20 * * * curl -X POST http://proxmox:5000/scale/cores \
-H "Content-Type: application/json" \
-d '{"vm_id": 104, "cores": 4}'

Automated Memory Management

Increase and decrease RAM allocation based on workload demand.

Increase RAM at 9:00 AM:

0 9 * * * curl -X POST http://proxmox:5000/scale/ram \
-H "Content-Type: application/json" \
-d '{"vm_id": 104, "memory": 8192}'

Decrease RAM at 7:00 PM:

0 19 * * * curl -X POST http://proxmox:5000/scale/ram \
-H "Content-Type: application/json" \
-d '{"vm_id": 104, "memory": 4096}'

Scheduled Storage Expansion

Automatically expand storage when needed, such as before a large data import.

Increase storage at midnight:

0 0 * * * curl -X POST http://proxmox:5000/scale/storage/increase \
-H "Content-Type: application/json" \
-d '{"vm_id": 104, "disk_size": 5}'

Daily Snapshot Creation

Create a daily snapshot of a container to ensure quick recovery.

Create a snapshot at 6:00 AM:

0 6 * * * curl -X POST http://proxmox:5000/snapshot/create \
-H "Content-Type: application/json" \
-d '{"vm_id": 104, "snapshot_name": "daily_snapshot_$(date +\%F)"}'

Snapshot Rollback for Recovery

Rollback to the latest snapshot in case of issues.

Rollback at 11:00 PM if needed:

0 23 * * * curl -X POST http://proxmox:5000/snapshot/rollback \
-H "Content-Type: application/json" \
-d '{"vm_id": 104, "snapshot_name": "daily_snapshot_$(date +\%F)"}'

On-Demand Container Cloning

Clone a container to test new software updates or changes.

Create a clone:

curl -X POST http://proxmox:5000/clone/create \
-H "Content-Type: application/json" \
-d '{"vm_id": 104, "new_vm_id": 105, "new_vm_name": "test_clone"}'

Clean Up Cloned Containers

Delete a cloned container after testing is complete.

Delete the clone:

curl -X DELETE http://proxmox:5000/clone/delete \
-H "Content-Type: application/json" \
-d '{"vm_id": 105}'

Monitor Container Resources

Regularly check and log the resource usage of a container.

Log resource usage every hour:

0 * * * * curl -X GET "http://proxmox:5000/resource/vm/status?vm_id=104" >> /var/log/lxc_vm_status.log

Node Resource Usage Monitoring

Track the resource usage of the Proxmox node to identify potential issues.

Check node status at 5-minute intervals:

*/5 * * * * curl -X GET "http://proxmox:5000/resource/node/status?node_name=proxmox" >> /var/log/node_status.log

Automated Health Check

Perform regular health checks on the API server.

Health check every 5 minutes:

*/5 * * * * curl -X GET http://proxmox:5000/health/check

Document Available API Routes

List all available API routes for reference.

List routes:

curl -X GET http://proxmox:5000/routes

Temporary Resource Boost for Maintenance

Temporarily increase resources before running a maintenance task.

Boost resources at 11:55 PM:

55 23 * * * curl -X POST http://proxmox:5000/scale/cores \
-H "Content-Type: application/json" \
-d '{"vm_id": 104, "cores": 8}' && \
curl -X POST http://proxmox:5000/scale/ram \
-H "Content-Type: application/json" \
-d '{"vm_id": 104, "memory": 16384}'

Scale back after maintenance:

30 0 * * * curl -X POST http://proxmox:5000/scale/cores \
-H "Content-Type: application/json" \
-d '{"vm_id": 104, "cores": 4}' && \
curl -X POST http://proxmox:5000/scale/ram \
-H "Content-Type: application/json" \
-d '{"vm_id": 104, "memory": 8192}'

Preemptive Resource Scaling

Automatically scale resources if CPU usage exceeds 75%.

Monitor and scale up if needed:

* * * * * curl -X GET "http://proxmox:5000/resource/vm/status?vm_id=104" | jq '.cpu' | \
if [ $(jq '.cpu' response.json) -gt 75 ]; then curl -X POST http://proxmox:5000/scale/cores -d '{"cores": 6}'; fi

Automate Cleanup Tasks

Remove old snapshots after a specified number of days to save space.

Cleanup old snapshots every Sunday at midnight:

0 0 * * 0 curl -X GET "http://proxmox:5000/snapshot/list?vm_id=104" | jq -r '.[] | select(.timestamp < (now - 7 * 86400)) | .name' | \
while read snapshot; do curl -X POST http://proxmox:5000/snapshot/delete -H "Content-Type: application/json" -d '{"vm_id": 104, "snapshot_name": "$snapshot"}'; done

Rolling Snapshot Backup

Maintain a rolling backup by keeping only the last 7 snapshots.

Create and manage rolling backups:

0 2 * * * curl -X POST http://proxmox:5000/snapshot/create \
-H "Content-Type: application/json" \
-d '{"vm_id": 104, "snapshot_name": "rolling_backup_$(date +\%F)"}' && \
curl -X GET "http://proxmox:5000/snapshot/list?vm_id=104" | jq -r '.[] | select(.timestamp < (now - 7 * 86400)) | .name' | \
while read snapshot; do curl -X POST http://proxmox:5000/snapshot/delete -H "Content-Type: application/json" -d '{"vm_id": 104, "snapshot_name": "$snapshot"}'; done

Real-Time Traffic-Based Scaling

Scale up resources dynamically based on real-time network traffic.

Scale cores based on traffic:

* * * * * curl -X GET "http://proxmox:5000/resource/vm/status?vm_id=104" | jq '.network.traffic' | \
if [ $(jq '.network.traffic' response.json) -gt 1000 ]; then curl -X POST http://proxmox:5000/scale/cores -d '{"cores": 8}'; fi

Clone and Test Environment

Automatically clone the environment for testing, then delete it after use.

Clone at midnight and delete at 4:00 AM:

0 0 * * * curl -X POST http://proxmox:5000/clone/create \
-H "Content-Type: application/json" \
-d '{"vm_id": 104, "new_vm_id": 106, "new_vm_name": "test_clone"}'

0 4 * * * curl -X DELETE http://proxmox:5000/clone/delete \
-H "Content-Type: application/json" \
-d '{"vm_id": 106}'

Monitor and Alert on Resource Thresholds

Set up monitoring and alerting when certain resource thresholds are exceeded.

Monitor CPU and alert if usage exceeds 85%:

* * * * * curl -X GET "http://proxmox:5000/resource/vm/status?vm_id=104" | \
if [ $(jq '.cpu' response.json) -gt 85 ]; then curl -X POST -d "alert: CPU usage high" http://alertmanager:9093/alert; fi

Health Check and Restart Unresponsive Containers

Automatically restart a container if it becomes unresponsive.

Health check and restart:

* * * * * curl -X GET "http://proxmox:5000/health/check?vm_id=104" | \
if [ $(jq '.status' response.json) != "healthy" ]; then curl -X POST http://proxmox:5000/vm/restart -H "Content-Type: application/json" -d '{"vm_id": 104}'; fi

Daily Performance Reports

Generate and send a daily report on container performance.

Generate and send report at 7:00 AM:

0 7 * * * curl -X GET "http://proxmox:5000/resource/vm/status?vm_id=104" | \
mail -s "Daily Performance Report" admin@example.com

Automated Pre-Deployment Snapshots

Create a snapshot before deploying changes to a container.

Snapshot before deployment:

0 3 * * * curl -X POST http://proxmox:5000/snapshot/create \
-H "Content-Type: application/json" \
-d '{"vm_id": 104, "snapshot_name": "pre_deploy_snapshot_$(date +\%F)"}'

Automatic Node Resource Balancing

Automatically rebalance resources across nodes when usage is high.

Check and rebalance at 5-minute intervals:

*/5 * * * * curl -X GET "http://proxmox:5000/resource/node/status?node_name=proxmox" | jq '.memory' | \
if [ $(jq '.memory.usage' response.json) -gt 80 ]; then curl -X POST http://proxmox:5000/scale/ram -d '{"vm_id": 104, "memory": 4096}'; fi

Log Cleanup Automation

Automatically clean up logs to save disk space.

Cleanup logs every week:

0 0 * * 0 curl -X POST http://proxmox:5000/vm/execute \
-H "Content-Type: application/json" \
-d '{"vm_id": 104, "command": "find /var/log -type f -name '*.log' -mtime +7 -delete"}'

Periodic Resource Scaling Based on Historical Data

Automatically scale resources based on historical usage trends.

Scale resources on Monday based on last week’s usage:

0 6 * * 1 curl -X GET "http://proxmox:5000/resource/vm/status?vm_id=104" | jq '.cpu' | \
if [ $(jq '.cpu' response.json) -gt 70 ]; then curl -X POST http://proxmox:5000/scale/cores -d '{"cores": 6}'; fi

Pre-Scaling for High-Traffic Events

Increase resources before an anticipated high-traffic event.

Pre-scale cores at 9:55 AM:

55 9 * * * curl -X POST http://proxmox:5000/scale/cores \
-H "Content-Type: application/json" \
-d '{"vm_id": 104, "cores": 8}'

Real-Time Usage Alerts

Set up real-time alerts for CPU or memory usage exceeding thresholds.

Alert if memory usage exceeds 90%:

* * * * * curl -X GET "http://proxmox:5000/resource/vm/status?vm_id=104" | \
if [ $(jq '.memory.usage' response.json) -gt 90 ]; then curl -X POST -d "alert: High memory usage" http://alertmanager:9093/alert; fi

Automatic Downgrade After Off-Peak

Downgrade resources after an off-peak period to save on resource allocation.

Reduce RAM after 11:00 PM:

0 23 * * * curl -X POST http://proxmox:5000/scale/ram \
-H "Content-Type: application/json" \
-d '{"vm_id": 104, "memory": 2048}'