Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
Move sources to `src` directory
Fix `isLeader` method
Fix autoscale loop, when none services for autoscale enabled
  • Loading branch information
AMEST committed Jan 4, 2022
1 parent d7bfeb1 commit 136ccdd
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM python:3.9-alpine
RUN pip install flask requests docker
COPY *.py /app/
COPY src/*.py /app/
WORKDIR /app
ENTRYPOINT ["python","main.py"]
52 changes: 44 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
[![Autoscaller Build](https://github.com/AMEST/swarm-autoscaler/actions/workflows/main.yml/badge.svg?branch=master)](https://github.com/AMEST/swarm-autoscaler/actions/workflows/main.yml)
![hub.docker.com](https://img.shields.io/docker/pulls/eluki/swarm-service-autoscaler.svg)
![GitHub release (latest by date)](https://img.shields.io/github/v/release/amest/swarm-autoscaler)
![GitHub](https://img.shields.io/github/license/amest/swarm-autoscaler)

# Swarm Service Autoscaler

## Links
* **[Docker hub](https://hub.docker.com/r/eluki/swarm-service-autoscaler)**

***

## Description

The project is an application that implements the ability to dynamically change the number of service instances under high load. The application receives all services that have the `swarm.autoscaler` label enabled, calculates the average value of the CPU utilization and, based on this, either increases the number of instances or decreases it.
Expand All @@ -13,16 +19,46 @@ But the minimum and maximum values ​​of CPU utilization can be changed throu

Also, for each service, you can set the maximum and minimum number of replicas to prevent a situation with an uncontrolled increase in the number of replicas (or too much decrease)

## Usage

1. Deploy Swarm Autoscaler using [`swarm-deploy.yml`](swarm-deploy.yml) from this repository
2. Add label `swarm.autoscaler=true` for services you want to autoscale.

```yml
deploy:
labels:
- "swarm.autoscaler=true"
```
For better resource management, it is recommended to add resource constraints to your service. Then it will definitely not eat more resources than necessary, and auto-scaling will work much better and will save resources in idle time.
```yml
deploy:
resources:
limits:
cpus: '0.50'
```
## Configuration
Service configure wia environment variable
### Swarm Autoscaler configuration
* `MIN_PERCENTAGE` - minimum service cpu utilization value in percent (0.0-100.0) for decrease service
* `MAX_PERCENTAGE` - maximum service cpu utilization value in percent (0.0-100.0) for increase service
* `DISCOVERY_DNSNAME` - swarm service name for in stack communication
* `CHECK_INTERVAL` - interval between checks
* `DRY_RUN` - noop mode for check service functional without enable inc or dec service replicas count
_**The application is configured through environment variables**_
| Setting | Default Value | Description |
| --------------------------- | ------------------ | --------------------------------------------------------------------------------------- |
| `AUTOSCALER_MIN_PERCENTAGE` | 25.0 | minimum service cpu utilization value in percent (0.0-100.0) for decrease service |
| `AUTOSCALER_MAX_PERCENTAGE` | 85.0 | maximum service cpu utilization value in percent (0.0-100.0) for increase service |
| `AUTOSCALER_DNSNAME` | `tasks.autoscaler` | swarm service name for in stack communication |
| `AUTOSCALER_INTERVAL` | 300 | interval between checks in seconds |
| `AUTOSCALER_DRYRUN` | false | noop mode for check service functional without enable inc or dec service replicas count |

## Usage
...
### Services configuration

_**Services in docker swarm are configured via labels**_

| Setting | Value | Description |
| ---------------------- | ------- | -------------------------------------------------------------------------------------------------------------------------- |
| `swarm.autoscaler` | `true` | Required. This enables autoscaling for a service. Anything other than `true` will not enable it |
| `swarm.autoscaler.min` | Integer | Optional. This is the minimum number of replicas wanted for a service. The autoscaler will not downscale below this number |
| `swarm.autoscaler.max` | Integer | Optional. This is the maximum number of replicas wanted for a service. The autoscaler will not scale up past this number |
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion docker_service.py → src/docker_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def isManager(self):
def isLeader(self):
if(not self.isManager()):
return False
nodeList = self.dockerClient.nodes.list()
nodeList = self.dockerClient.nodes.list(filters={'role': 'manager'})
nodeAddr = self.nodeInfo['Swarm']['NodeAddr']
managerLeader = list(x for x in nodeList if x.attrs['ManagerStatus']['Leader'])[0]
return managerLeader.attrs['ManagerStatus']['Addr'].startswith(nodeAddr)
Expand Down
1 change: 1 addition & 0 deletions main.py → src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def autoscallerLoop():
time.sleep(60*10) # Wait 10 minute
continue
services = SwarmService.getAutoscaleServices()
services = services if services != None else []
logging.debug("Services len: %s", len(services))
for service in services:
cpuLimit = SwarmService.getServiceCpuLimitPercent(service)
Expand Down

0 comments on commit 136ccdd

Please sign in to comment.