ScaleBee is a Go-based autoscaler for Docker Swarm services that uses Prometheus metrics to automatically scale services up or down based on CPU usage.
- π Automatic scaling of Docker Swarm services based on CPU metrics
- π Integration with Prometheus for real-time metrics
- βοΈ Configurable CPU thresholds for scaling decisions
- π Respects minimum and maximum replica constraints
- π Continuous monitoring with configurable intervals
- π³ Runs as a Docker Swarm service
ScaleBee collects container metrics directly from Docker and exposes them to Prometheus, then uses those metrics to automatically adjust replica counts:
- Collect metrics from Docker socket every 10 seconds (CPU, memory)
- Expose metrics on
:9090/metricsendpoint in Prometheus format - Query Prometheus for service CPU metrics every 60 seconds (configurable)
- Scale up when CPU exceeds upper threshold (default: 85%)
- Scale down when CPU falls below lower threshold (default: 25%)
- Respect limits defined by min/max replica labels
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β ScaleBee β
β ββββββββββββββββββ ββββββββββββββββββββ β
β β Metrics ββββββββββΆβ Autoscaler β β
β β Exporter β β Engine β β
β β :9090/metrics β β β β
β ββββββββββ¬ββββββββ ββββββββββ¬ββββββββββ β
β β β β
βββββββββββββΌβββββββββββββββββββββββββββΌββββββββββββ-βββ
β β
β β Docker API
β Scrape β (scale services)
βΌ βΌ
ββββββββββββββββ βββββββββββββββββββββββ
β Prometheus β β Docker Swarm β
β β β Services β
ββββββββββββββββ β ββββββ ββββββ β
β β S1 β β S2 β ... β
β ββββββ ββββββ β
βββββββββββββββββββββββ
- Docker Swarm cluster initialized
- Docker socket access for ScaleBee container
-
Deploy the monitoring stack (Prometheus + OpenTelemetry Collector):
docker stack deploy -c deploy/docker-compose.yml autoscale
-
Label your services for autoscaling:
version: "3.7" services: myapp: image: myapp:latest deploy: labels: - "swarm.autoscaler=true" - "swarm.autoscaler.minimum=2" - "swarm.autoscaler.maximum=10" replicas: 2 resources: limits: cpus: '0.5' memory: 512M
-
Deploy your application:
docker stack deploy -c your-app.yml myapp
ScaleBee is configured through environment variables:
| Variable | Default | Description |
|---|---|---|
PROMETHEUS_URL |
http://prometheus:9090 |
URL of the Prometheus server |
LOOP |
yes |
Enable continuous monitoring (yes or no) |
INTERVAL_SECONDS |
15 |
Seconds between autoscaling checks |
CPU_PERCENTAGE_UPPER_LIMIT |
75 |
CPU % threshold for scaling up |
CPU_PERCENTAGE_LOWER_LIMIT |
20 |
CPU % threshold for scaling down |
MEMORY_PERCENTAGE_UPPER_LIMIT |
80 |
Memory % threshold for scaling up |
MEMORY_PERCENTAGE_LOWER_LIMIT |
20 |
Memory % threshold for scaling down |
METRICS_ENABLED |
yes |
Enable built-in metrics exporter |
METRICS_PORT |
9090 |
Port for metrics HTTP server |
Scaling Logic:
- Scale up when either CPU or Memory exceeds their upper limits
- Scale down when both CPU and Memory are below their lower limits
Services must have the following labels to enable autoscaling:
| Label | Required | Description |
|---|---|---|
swarm.autoscaler |
β Yes | Set to "true" to enable autoscaling |
swarm.autoscaler.minimum |
Minimum number of replicas (e.g., "2") |
|
swarm.autoscaler.maximum |
Maximum number of replicas (e.g., "10") |
See the deploy/docker-compose.yml for a complete example including:
- ScaleBee (autoscaler + metrics exporter)
- Prometheus for metrics storage and querying
ScaleBee exposes Prometheus metrics at http://scalebee:9090/metrics:
# HELP container_cpu_usage_percent CPU usage percentage
# TYPE container_cpu_usage_percent gauge
container_cpu_usage_percent{service="myapp",task="myapp.1.xyz",container_id="abc123"} 45.2
# HELP container_memory_usage_mb Memory usage in MB
# TYPE container_memory_usage_mb gauge
container_memory_usage_mb{service="myapp",task="myapp.1.xyz",container_id="abc123"} 128.5
# Build binary
go build -o scalebee .
# Build Docker image
docker build -t scalebee:latest .
# Run locally (requires Docker socket access)
export PROMETHEUS_URL=http://localhost:9090
./scalebee.
βββ main.go # Entry point with HTTP server
βββ pkg/
β βββ autoscaler/ # Autoscaling logic
β β βββ autoscaler.go
β βββ docker/ # Docker Swarm service management
β β βββ service.go
β βββ metrics/ # Docker stats β Prometheus exporter
β β βββ exporter.go
β βββ prometheus/ # Prometheus query client
β βββ client.go
βββ deploy/ # Deployment configurations
β βββ docker-compose.yml # Stack: ScaleBee + Prometheus
β βββ prometheus.yml # Prometheus config
β βββ example-service.yml # Example autoscaled service
βββ Dockerfile # Multi-stage build
βββ README.md
go test ./...- Triggered when average CPU >
CPU_PERCENTAGE_UPPER_LIMIT(default 85%) - Increases replicas by 1
- Will not exceed
swarm.autoscaler.maximumlabel
- Triggered when average CPU <
CPU_PERCENTAGE_LOWER_LIMIT(default 25%) - Decreases replicas by 1
- Will not go below
swarm.autoscaler.minimumlabel
- On each check, ensures replicas are within min/max bounds
- Useful for services that drift from their configured limits
ScaleBee logs all scaling decisions:
2024/12/10 13:53:00 ScaleBee - Docker Swarm Autoscaler
2024/12/10 13:53:00 Prometheus URL: http://prometheus:9090
2024/12/10 13:53:00 CPU Upper Limit: 85%
2024/12/10 13:53:00 CPU Lower Limit: 25%
2024/12/10 13:53:05 Retrieved 3 service metrics from Prometheus
2024/12/10 13:53:05 Service: myapp_web, Avg CPU: 92.34%
2024/12/10 13:53:05 Service myapp_web has autoscale label
2024/12/10 13:53:05 Service myapp_web is above 85% CPU usage
2024/12/10 13:53:05 Scaling up service myapp_web to 4This is a complete rewrite of the original bash-based autoscaler in Go. Key improvements:
- β Better error handling and logging
- β Type safety and compile-time checks
- β Easier testing and maintenance
- β Native Docker SDK integration
- β Structured configuration
- β No dependencies on external tools (jq, curl, etc.)
This project is inspired by jcwimer/docker-swarm-autoscaler and reimplemented using the Docker Compose SDK.
MIT License - See LICENSE file for details
Contributions are welcome! Please feel free to submit a Pull Request.