Scale down Kubernetes deployments after work hours
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
Henning Jacobs f7b59f77c4 #22 parse downtime-replicas as int 2 years ago
deploy v0.9 2 years ago
helm-chart update: add extra args values for helm-chart (#37) 2 years ago
kube_downscaler #22 parse downtime-replicas as int 2 years ago
tests #22 parse downtime-replicas as int 2 years ago
.gitignore Grace period for new deployments (#9) 3 years ago
.travis.yml run flake8 and docker build on test (#26) 2 years ago
Dockerfile update dependencies (+ Alpine 3.9) 2 years ago
LICENSE initial commit 4 years ago
Makefile run flake8 and docker build on test (#26) 2 years ago
Pipfile pykube-ng already expands home dir (~) 2 years ago
Pipfile.lock pykube-ng already expands home dir (~) 2 years ago
README.rst Adds support for 'downtime-replicas' deployment annotation (#38) 2 years ago
pipenv-install.py Refactor code, Add Pipenv and Stackset support (#20) 2 years ago
setup.py initial commit 4 years ago
tox.ini Refactor code, Add Pipenv and Stackset support (#20) 2 years ago

README.rst

Kubernetes Downscaler

Travis CI Build Status

Code Coverage

Docker pulls

Scale down Kubernetes deployments and/or statefulsets during non-work hours.

Deployments are interchangeable by statefulset for this whole guide.

It will scale down the deployment's replicas if all of the following conditions are met:

  • current time is not part of the "uptime" schedule or current time is part of the "downtime" schedule. The schedules are being evaluated in following order:
    • downscaler/downtime annotation on the deployment/stateful set
    • downscaler/uptime annotation on the deployment/stateful set
    • downscaler/downtime annotation on the deployment/stateful set's namespace
    • downscaler/uptime annotation on th deployment/stateful set's namespace
    • --default-uptime cli argument
    • --default-downtime cli argument
    • DEFAULT_UPTIME environment variable
    • DEFAULT_DOWNTIME environment variable
  • the deployment's namespace is not part of the exclusion list (kube-system is excluded by default)
  • the deployment's name is not part of the exclusion list
  • the deployment is not marked for exclusion (annotation downscaler/exclude: "true")
  • there are no active pods that force the whole cluster into uptime (annotation downscaler/force-uptime: "true")

The deployment by default will be scaled down to zero replicas. This can be configured with a deployment annotation of downscaler/downtime-replicas. (eg: downscaler/downtime-replicas: "1")

Example use cases:

  • Deploy the downscaler to a test (non-prod) cluster with a default uptime or downtime time range to scale down all deployments during the night and weekend.
  • Deploy the downscaler to a production cluster without any default uptime/downtime setting and scale down specific deployments by setting the downscaler/uptime (or downscaler/downtime) annotation. This might be useful for internal tooling frontends which are only needed during work time.

You need to combine the downscaler with an elastic cluster autoscaler to actually save cloud costs. The official cluster autoscaler and the kube-aws-autoscaler were tested to work fine with the downscaler.

Usage

Deploy the downscaler into your cluster via (also works with Minikube):

The example configuration uses the --dry-run as a safety flag to prevent downscaling --- remove it to enable the downscaler, e.g. by editing the deployment:

Configuration

The downscaler is configured via command line args, environment variables and/or Kubernetes annotations.

Time definitions (e.g. DEFAULT_UPTIME) accept a comma separated list of specifications, e.g. the following configuration would downscale all deployments for non-work hours:

To only downscale during the weekend and already Friday after 20:00:

Each time specification must have the format <WEEKDAY-FROM>-<WEEKDAY-TO-INCLUSIVE> <HH>:<MM>-<HH>:<MM> <TIMEZONE>. The timezone value can be any Olson timezone, e.g. "US/Eastern", "PST" or "UTC".

Available command line options:

--dry-run

Dry run mode: do not change anything, just print what would be done

--debug

Debug mode: print more information

--once

Run loop only once and exit

--interval

Loop interval (default: 30s)

--namespace

Namespace (default: all namespaces)

--kind

Downscale resources of this kind (default: deployment)

--grace-period

Grace period in seconds for new deployments before scaling them down (default: 15min). The grace period counts from time of creation of the deployment, i.e. updated deployments will immediately be scaled down regardless of the grace period.

--default-uptime

Default time range to scale up for (default: always), can also be configured via environment variable DEFAULT_UPTIME or via the annotation downscaler/uptime on each deployment

--default-downtime

Default time range to scale down for (default: never), can also be configured via environment variable DEFAULT_DOWNTIME or via the annotation downscaler/downtime on each deployment

--exclude-namespaces

Exclude namespaces from downscaling (default: kube-system), can also be configured via environment variable EXCLUDE_NAMESPACES

--exclude-deployments

Exclude specific deployments from downscaling (default: kube-downscaler, downscaler), can also be configured via environment variable EXCLUDE_DEPLOYMENTS

--exclude-statefulsets

Exclude specific statefulsets from statefulsets, can also be configured via environment variable EXCLUDE_STATEFULSETS

Namespace Defaults

DEFAULT_UPTIME, DEFAULT_DOWNTIME, FORCE_UPTIME and exclusion can also be configured using Namespace annotations. Where configured these values supersede the other global default values.

The following annotations are supported on the Namespace level:

  • downscaler/uptime
  • downscaler/downtime
  • downscaler/force-uptime
  • downscaler/exclude

Contributing

Easiest way to contribute is to provide feedback! We would love to hear what you like and what you think is missing. Create an issue or ping try_except_ on Twitter.

PRs are welcome. Please also have a look at issues labeled with "help wanted".

License

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.