Skip to content
This repository was archived by the owner on Oct 3, 2020. It is now read-only.

Commit 1aa67b3

Browse files
authored
moved to codeberg.org
1 parent 5cae86f commit 1aa67b3

File tree

1 file changed

+1
-231
lines changed

1 file changed

+1
-231
lines changed

README.rst

Lines changed: 1 addition & 231 deletions
Original file line numberDiff line numberDiff line change
@@ -1,231 +1 @@
1-
=====================
2-
Kubernetes Downscaler
3-
=====================
4-
5-
.. image:: https://travis-ci.org/hjacobs/kube-downscaler.svg?branch=master
6-
:target: https://travis-ci.org/hjacobs/kube-downscaler
7-
:alt: Travis CI Build Status
8-
9-
.. image:: https://coveralls.io/repos/github/hjacobs/kube-downscaler/badge.svg?branch=master;_=1
10-
:target: https://coveralls.io/github/hjacobs/kube-downscaler?branch=master
11-
:alt: Code Coverage
12-
13-
.. image:: https://img.shields.io/docker/pulls/hjacobs/kube-downscaler.svg
14-
:target: https://hub.docker.com/r/hjacobs/kube-downscaler
15-
:alt: Docker pulls
16-
17-
.. image:: https://img.shields.io/badge/calver-YY.MM.MICRO-22bfda.svg
18-
:target: http://calver.org/
19-
:alt: CalVer
20-
21-
Scale down Kubernetes Deployments, StatefulSets, and/or HorizontalPodAutoscalers during non-work hours.
22-
23-
Deployments are interchangeable by statefulsets/horizontalpodautoscalers for this whole guide unless explicitly stated otherwise.
24-
25-
It will scale down the deployment's replicas if all of the following conditions are met:
26-
27-
* current time is not part of the "uptime" schedule or current time is part of the "downtime" schedule. The schedules are evaluated in the following order:
28-
* ``downscaler/downscale-period`` or ``downscaler/downtime`` annotation on the deployment/stateful set
29-
* ``downscaler/upscale-period`` or ``downscaler/uptime`` annotation on the deployment/stateful set
30-
* ``downscaler/downscale-period`` or ``downscaler/downtime`` annotation on the deployment/stateful set's namespace
31-
* ``downscaler/upscale-period`` or ``downscaler/uptime`` annotation on the deployment/stateful set's namespace
32-
* ``--upscale-period`` or ``--default-uptime`` CLI argument
33-
* ``--downscale-period`` or ``--default-downtime`` CLI argument
34-
* ``UPSCALE_PERIOD`` or ``DEFAULT_UPTIME`` environment variable
35-
* ``DOWNSCALE_PERIOD`` or ``DEFAULT_DOWNTIME`` environment variable
36-
* the deployment's namespace is not part of the exclusion list:
37-
* If you provide an exclusion list, it will be used in place of the default (which includes only kube-system).
38-
* the deployment's name is not part of the exclusion list
39-
* the deployment is not marked for exclusion (annotation ``downscaler/exclude: "true"`` or ``downscaler/exclude-until: "2024-04-05"``)
40-
* there are no active pods that force the whole cluster into uptime (annotation ``downscaler/force-uptime: "true"``)
41-
42-
The deployment by default will be scaled down to zero replicas. This can be configured with a deployment or its namespace's annotation of ``downscaler/downtime-replicas`` (e.g. ``downscaler/downtime-replicas: "1"``) or via CLI with ``--downtime-replicas``.
43-
In case of HorizontalPodAutoscalers, the ``minReplicas`` field cannot be set to zero and thus ``downscaler/downtime-replicas`` should be at least ``1``.
44-
45-
Example use cases:
46-
47-
* 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.
48-
* 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.
49-
This might be useful for internal tooling frontends which are only needed during work time.
50-
51-
You need to combine the downscaler with an elastic cluster autoscaler to actually **save cloud costs**.
52-
The `official cluster autoscaler <https://github.com/kubernetes/autoscaler/tree/master/cluster-autoscaler>`_ and the `kube-aws-autoscaler <https://github.com/hjacobs/kube-aws-autoscaler>`_ were tested to work fine with the downscaler.
53-
54-
Usage
55-
=====
56-
57-
Deploy the downscaler into your cluster via (also works with kind_ or Minikube_):
58-
59-
.. code-block:: bash
60-
61-
$ kubectl apply -f deploy/
62-
63-
In case you are deploying ``kube-downscaler`` to another namespace than ``default``, for example if your context is pointing to ``my-namespace``. Make
64-
sure you change the ``deploy/rbac.yaml`` Service Account configuration ``namespace: default`` to the destination namespace ``my-namespace``, instead of ``default``.
65-
66-
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:
67-
68-
.. code-block:: bash
69-
70-
$ kubectl edit deploy kube-downscaler
71-
72-
The example deployment manifests come with a configured uptime (``deploy/config.yaml`` sets it to "Mon-Fri 07:30-20:30 CET"), you can overwrite this per namespace or deployment, e.g.:
73-
74-
.. code-block:: bash
75-
76-
$ kubectl run nginx --image=nginx
77-
$ kubectl annotate deploy nginx 'downscaler/uptime=Mon-Fri 09:00-17:00 America/Buenos_Aires'
78-
79-
Note that the default grace period of 15 minutes applies to the new nginx deployment, i.e. if the current time is not within Mon-Fri 9-17 (Buenos Aires timezone), it will downscale not immediately, but after 15 minutes.
80-
The downscaler will eventually log something like:
81-
82-
::
83-
84-
INFO: Scaling down Deployment default/nginx from 1 to 0 replicas (uptime: Mon-Fri 09:00-17:00 America/Buenos_Aires, downtime: never)
85-
86-
Note that in cases where a HorizontalPodAutoscaler (HPA) is used along with Deployments, consider the following:
87-
88-
* If downscale to 0 replicas is desired, the annotation should be applied on the Deployment. This is a special case, since ``minReplicas`` of 0 on HPA is not allowed. Setting Deployment replicas to 0 essentially disables the HPA. In such a case, the HPA will emit events like "failed to get memory utilization: unable to get metrics for resource memory: no metrics returned from resource metrics API" as there is no Pod to retrieve metrics from.
89-
* If downscale greater than 0 is desired, the annotation should be applied on the HPA. This allows for dynamic scaling of the Pods even during downtime based upon the external traffic as well as maintain a lower ``minReplicas`` during downtime if there is no/low traffic. If the Deployment is annotated instead of the HPA, it leads to a race condition where kube-downscaler scales down the Deployment and HPA upscales it as its ``minReplicas`` is higher.
90-
91-
To enable Downscaler on HPA with `--downtime-replicas=1`, ensure to add the following annotations to Deployment and HPA.
92-
93-
.. code-block:: bash
94-
95-
$ kubectl annotate deploy nginx 'downscaler/exclude=true'
96-
$ kubectl annotate hpa nginx 'downscaler/downtime-replicas=1'
97-
$ kubectl annotate hpa nginx 'downscaler/uptime=Mon-Fri 09:00-17:00 America/Buenos_Aires'
98-
99-
100-
Configuration
101-
=============
102-
103-
The downscaler is configured via command line args, environment variables and/or Kubernetes annotations.
104-
105-
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:
106-
107-
.. code-block:: bash
108-
109-
DEFAULT_UPTIME="Mon-Fri 07:30-20:30 Europe/Berlin"
110-
111-
To only downscale during the weekend and Friday after 20:00:
112-
113-
.. code-block:: bash
114-
115-
DEFAULT_DOWNTIME="Sat-Sun 00:00-24:00 CET,Fri-Fri 20:00-24:00 CET'
116-
117-
Each time specification can be in one of two formats:
118-
119-
* Recurring specifications have the format ``<WEEKDAY-FROM>-<WEEKDAY-TO-INCLUSIVE> <HH>:<MM>-<HH>:<MM> <TIMEZONE>``. The timezone value can be any `Olson timezone <https://en.wikipedia.org/wiki/Tz_database>`_, e.g. "US/Eastern", "PST" or "UTC".
120-
* Absolute specifications have the format ``<TIME_FROM>-<TIME_TO>`` where each ``<TIME>`` is an ISO 8601 date
121-
and time of the format ``<YYYY>-<MM>-<DD>T<HH>:<MM>:<SS>[+-]<TZHH>:<TZMM>``.
122-
123-
Alternative logic, based on periods
124-
===================================
125-
126-
Instead of strict uptimes or downtimes, you can chose time periods for upscaling or downscaling. The time definitions are the same. In this case, the upscale or downscale happens only on time periods, rest of times will be ignored.
127-
128-
If upscale or downscale periods are configured, uptime and downtime will be ignored. This means that some options are mutually exclusive, e.g. you can either use ``--downscale-period`` or ``--default-downtime``, but not both.
129-
130-
This definition will downscale your cluster between 19:00 and 20:00. If you upscale your cluster manually, it won't be scaled down until next day 19:00-20:00.
131-
132-
.. code-block:: bash
133-
134-
DOWNSCALE_PERIOD="Mon-Sun 19:00-20:00 Europe/Berlin"
135-
136-
Command Line Options
137-
====================
138-
139-
Available command line options:
140-
141-
``--dry-run``
142-
Dry run mode: do not change anything, just print what would be done
143-
``--debug``
144-
Debug mode: print more information
145-
``--once``
146-
Run loop only once and exit
147-
``--interval``
148-
Loop interval (default: 30s)
149-
``--namespace``
150-
Restrict the downscaler to work only in a single namespace (default: all namespaces). This is mainly useful for deployment scenarios where the deployer of kube-downscaler only has access to a given namespace (instead of cluster access). If used simultaneously with ``--exclude-namespaces``, none is applied.
151-
``--include-resources``
152-
Downscale resources of this kind as comma separated list. [deployments, statefulsets, stacks, horizontalpodautoscalers] (default: deployments)
153-
``--grace-period``
154-
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.
155-
``--upscale-period``
156-
Alternative logic to scale up only in given period of time (default: never), can also be configured via environment variable ``UPSCALE_PERIOD`` or via the annotation ``downscaler/upscale-period`` on each deployment
157-
``--downscale-period``
158-
Alternative logic to scale down only in given period of time (default: never), can also be configured via environment variable ``DOWNSCALE_PERIOD`` or via the annotation ``downscaler/downscale-period`` on each deployment
159-
``--default-uptime``
160-
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
161-
``--default-downtime``
162-
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
163-
``--exclude-namespaces``
164-
Exclude namespaces from downscaling (list of regex patterns, default: kube-system), can also be configured via environment variable ``EXCLUDE_NAMESPACES``. If used simultaneously with ``--namespace``, none is applied.
165-
``--exclude-deployments``
166-
Exclude specific deployments/statefulsets/cronjobs from downscaling (default: kube-downscaler, downscaler), can also be configured via environment variable ``EXCLUDE_DEPLOYMENTS``.
167-
Despite its name, this option will match the name of any included resource type (Deployment, StatefulSet, CronJob, ..).
168-
``--downtime-replicas``
169-
Default value of replicas to downscale to, the annotation ``downscaler/downtime-replicas`` takes precedence over this value.
170-
``--deployment-time-annotation``
171-
Optional: name of the annotation that would be used instead of the creation timestamp of the resource.
172-
This option should be used if you want the resources to be kept scaled up during a grace period (``--grace-period``) after a deployment.
173-
The format of the annotation's timestamp value must be exactly the same as for Kubernetes' ``creationTimestamp``: ``%Y-%m-%dT%H:%M:%SZ``.
174-
Recommended: set this annotation by your deployment tooling automatically.
175-
176-
Namespace Defaults
177-
==================
178-
179-
``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.
180-
181-
.. code-block:: yaml
182-
183-
apiVersion: v1
184-
kind: Namespace
185-
metadata:
186-
name: foo
187-
labels:
188-
name: foo
189-
annotations:
190-
downscaler/uptime: Mon-Sun 07:30-18:00 CET
191-
192-
The following annotations are supported on the Namespace level:
193-
194-
* ``downscaler/upscale-period``
195-
* ``downscaler/downscale-period``
196-
* ``downscaler/uptime``: set "uptime" for all resources in this namespace
197-
* ``downscaler/downtime``: set "downtime" for all resources in this namespace
198-
* ``downscaler/force-uptime``: force scaling up all resources in this namespace - can be ``true``/``false`` or a period
199-
* ``downscaler/exclude``: set to ``true`` to exclude all resources in the namespace
200-
* ``downscaler/exclude-until``: temporarily exclude all resources in the namespace until the given timestamp
201-
* ``downscaler/downtime-replicas``: overwrite the default target replicas to scale down to (default: zero)
202-
203-
Contributing
204-
============
205-
206-
Easiest way to contribute is to provide feedback! We would love to hear what you like and what you think is missing.
207-
Create an issue or `ping try_except_ on Twitter`_.
208-
209-
PRs are welcome. Please also have a look at `issues labeled with "help wanted"`_.
210-
211-
212-
License
213-
=======
214-
215-
This program is free software: you can redistribute it and/or modify
216-
it under the terms of the GNU General Public License as published by
217-
the Free Software Foundation, either version 3 of the License, or
218-
(at your option) any later version.
219-
220-
This program is distributed in the hope that it will be useful,
221-
but WITHOUT ANY WARRANTY; without even the implied warranty of
222-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
223-
GNU General Public License for more details.
224-
225-
You should have received a copy of the GNU General Public License
226-
along with this program. If not, see http://www.gnu.org/licenses/.
227-
228-
.. _ping try_except_ on Twitter: https://twitter.com/try_except_
229-
.. _issues labeled with "help wanted": https://github.com/hjacobs/kube-downscaler/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22
230-
.. _kind: https://kind.sigs.k8s.io/
231-
.. _Minikube: https://github.com/kubernetes/minikube
1+
Moved to https://codeberg.org/hjacobs/kube-downscaler

0 commit comments

Comments
 (0)