diff --git a/README.md b/README.md index 31879cc..1bb98a5 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -Kubernetes Downscaler +Python Kubernetes Downscaler ===================== -This is a fork of [hjacobs/kube-downscaler](https://codeberg.org/hjacobs/kube-downscaler) which is no longer maintained. Here the original text of the kube-downscaler README: +This is a fork of [hjacobs/kube-downscaler](https://codeberg.org/hjacobs/kube-downscaler) which is no longer maintained. **Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)* -- [Concepts](#concepts) - - [Algorithm](#algorithm) - - [Minimum replicas](#minimum-replicas) - - [Specific workload](#specific-workload) - - [Example use cases](#example-use-cases) -- [Usage](#usage) - - [Deployment](#deployment) - - [Example configuration](#example-configuration) - - [Notes](#notes) -- [Configuration](#configuration) - - [Uptime / downtime spec](#uptime--downtime-spec) - - [Alternative logic, based on periods](#alternative-logic-based-on-periods) - - [Command Line Options](#command-line-options) - - [Namespace Defaults](#namespace-defaults) -- [Contributing](#contributing) -- [License](#license) +- [Python Kubernetes Downscaler](#python-kubernetes-downscaler) + - [Concepts](#concepts) + - [Algorithm](#algorithm) + - [Minimum replicas](#minimum-replicas) + - [Specific workload](#specific-workload) + - [Example use cases](#example-use-cases) + - [Usage](#usage) + - [Helm Chart](#helm-chart) + - [Example configuration](#example-configuration) + - [Notes](#notes) + - [Configuration](#configuration) + - [Uptime / downtime spec](#uptime--downtime-spec) + - [Alternative logic, based on periods](#alternative-logic-based-on-periods) + - [Command Line Options](#command-line-options) + - [Namespace Defaults](#namespace-defaults) + - [Contributing](#contributing) + - [License](#license) @@ -43,7 +44,7 @@ Scale down / "pause" Kubernetes workload (`Deployments`, `StatefulSets`, and/or ### Algorithm -`Kube-downscaler` will scale down the deployment\'s replicas if all of the following +`py-kube-downscaler` 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 is part of the \"downtime\" schedule. @@ -122,21 +123,9 @@ were tested to work fine with the downscaler. ## Usage -### Deployment +### Helm Chart -Deploy the downscaler into your cluster via (also works with -[kind](https://kind.sigs.k8s.io/) or -[Minikube](https://github.com/kubernetes/minikube)): - -``` {.sourceCode .bash} -$ kubectl apply -f deploy/ -``` - -In case you are deploying `kube-downscaler` to another namespace than -`default`, for example if your context is pointing to `my-namespace`. -Make sure you change the `deploy/rbac.yaml` Service Account -configuration `namespace: default` to the destination namespace -`my-namespace`, instead of `default`. +For detailed information on deploying the `py-kube-downscaler` using our Helm chart, please refer to the [Helm Chart README](./chart/README.md#Deploy-py-kube-downscaler-using-Helm-chart) in the chart directory. ### Example configuration @@ -146,7 +135,7 @@ prevent downscaling \-\-- remove it to enable the downscaler, e.g. by editing the deployment: ``` {.sourceCode .bash} -$ kubectl edit deploy kube-downscaler +$ kubectl edit deploy py-kube-downscaler ``` The example deployment manifests come with a configured uptime @@ -186,7 +175,7 @@ with Deployments, consider the following: 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 + condition** where `py-kube-downscaler` scales down the Deployment and HPA upscales it as its `minReplicas` is higher. To enable Downscaler on HPA with `--downtime-replicas=1`, @@ -276,7 +265,7 @@ Available command line options: : 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 + where the deployer of py-kube-downscaler only has access to a given namespace (instead of cluster access). If used simultaneously with `--exclude-namespaces`, none is applied. @@ -329,7 +318,7 @@ Available command line options: `--exclude-deployments` : Exclude specific deployments/statefulsets/cronjobs from downscaling - (default: kube-downscaler, downscaler), can also be configured via + (default: py-kube-downscaler, downscaler), can also be configured via environment variable `EXCLUDE_DEPLOYMENTS`. Despite its name, this option will match the name of any included resource type (Deployment, StatefulSet, CronJob, ..). @@ -351,10 +340,10 @@ Available command line options: `--matching-labels` -: Optional: list of workload\'s labels which are covered by the kube-downscaler +: Optional: list of workload\'s labels which are covered by the py-kube-downscaler scope. All workloads whose labels don't match any in the list are ignored. For backwards compatibility, if this argument is not specified, - kube-downscaler will apply to all resources. + py-kube-downscaler will apply to all resources. ### Namespace Defaults diff --git a/chart/README.md b/chart/README.md index fa98b0f..8ea395c 100644 --- a/chart/README.md +++ b/chart/README.md @@ -1,13 +1,97 @@ -# Kube Downscaler Helm Chart +# Python Kubernetes Downscaler - Helm Chart -This repository offers a convenience chart for the `kube-downscaler` [project](https://codeberg.org/hjacobs/kube-downscaler). +This repository offers a Helm chart for the `py-kube-downscaler`. ## Important values | Key | Type | Example | Description | -| ------------------ | ------ | ----------------------------------------------------------------------------------------------------- | ----------------------------------------------- | +|--------------------|--------|-------------------------------------------------------------------------------------------------------|-------------------------------------------------| | image.tag | string | `"23.2.0@sha256:4129e7e7551eb451ee2b43680ef818f3057304ad50888f79ec9722afab6c29ff"` | Tag of the image to use | | arguments | list | `[--interval=60,--include-resources=deployments,statefulsets,horizontalpodautoscalers,scaledobjects]` | Arguments to pass to the kube-downscaler binary | | excludedNamespaces | list | `["namespace-a", "namespace-b"]` | Namespaces to exclude from downscaling | -How the downscaler can be configured is described in the [kube-downscaler documentation](https://codeberg.org/hjacobs/kube-downscaler). +# Deploy py-kube-downscaler using Helm chart + +This directory contains tutorial to deploy py-kube-downscaler. + +## Configuring your Deployment to downscale + +Please add below annotations based on timezone your deployment should run: + +``` +metadata: + annotations: + downscaler/uptime: "Mon-Fri 07:00-19:00 US/Eastern" +``` + +Note: For more configuration details please, +refer [here](https://github.com/caas-team/py-kube-downscaler?tab=readme-ov-file#configuration). + +## Architecture + +The diagram below depicts how a py-kube-downscaler agent controls applications. +![Alt text](images/architecture.png?raw=true "Kube py-kube-downscaler diagram") + +## Quick Start + +Below are instructions to quickly install and configure py-kube-downscaler. + +### Installing py-kube-downscaler + +1. Make sure you're connected to the right cluster: + +``` +kubectl config current-context +``` + +2. Before deploying, make sure to update *values.yaml* in py-kube-downscaler chart depending on whether you want RBAC + roles deployed or not: + +```yaml +rbac: + create: false +``` + +Note: In case RBAC is enabled, a new service account will be created for py-kube-downscaler with certain privileges, +otherwise the 'default' one will be used. + +3. Deploy py-kube-downscaler: + +``` +cd chart +helm install . --namespace py-kube-downscaler --name py-kube-downscaler +``` + +4. Check the deployed release status: + +``` +helm list -n py-kube-downscaler +``` + +``` +NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE +py-kube-downscaler 1 Tue Sep 25 02:07:58 2018 DEPLOYED py-kube-downscaler-0.5.1 0.5.1 py-kube-downscaler + +``` + +5. Check whether py-kube-downscaler pod is up and running: + +``` +kubectl get pods -n py-kube-downscaler +``` + +``` +NAME READY STATUS RESTARTS AGE +py-kube-downscaler-py-kube-downscaler-7f58c6b5b7-rnglz 1/1 Running 0 6m +``` + +6. Check the Kubernetes event logs, to make sure of successful deployment of py-kube-downscaler: + +``` +kubectl get events -w +``` + +## Acknowledgments + +Thanks to [Kube-downscaler](https://github.com/hjacobs/kube-downscaler) project authored +by [Henning Jacobs](https://github.com/hjacobs). diff --git a/chart/images/architecture.png b/chart/images/architecture.png new file mode 100644 index 0000000..bbf428f Binary files /dev/null and b/chart/images/architecture.png differ diff --git a/deploy/config.yaml b/deploy/config.yaml deleted file mode 100644 index f097956..0000000 --- a/deploy/config.yaml +++ /dev/null @@ -1,7 +0,0 @@ -apiVersion: v1 -kind: ConfigMap -metadata: - name: kube-downscaler -data: - # downscale for non-work hours - DEFAULT_UPTIME: "Mon-Fri 07:30-20:30 CET" diff --git a/deploy/deployment.yaml b/deploy/deployment.yaml deleted file mode 100644 index 72648f3..0000000 --- a/deploy/deployment.yaml +++ /dev/null @@ -1,41 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - labels: - application: kube-downscaler - version: v23.2.0 - name: kube-downscaler -spec: - replicas: 1 - selector: - matchLabels: - application: kube-downscaler - template: - metadata: - labels: - application: kube-downscaler - version: v23.2.0 - spec: - serviceAccountName: kube-downscaler - containers: - - name: downscaler - image: hjacobs/kube-downscaler:23.2.0 - args: - # dry run by default, remove to perform downscaling - - --dry-run - # run every minute - - --interval=60 - envFrom: - - configMapRef: - name: kube-downscaler - optional: true - resources: - limits: - memory: 100Mi - requests: - cpu: 5m - memory: 100Mi - securityContext: - readOnlyRootFilesystem: true - runAsNonRoot: true - runAsUser: 1000 diff --git a/deploy/kustomization.yaml b/deploy/kustomization.yaml deleted file mode 100644 index 15c1172..0000000 --- a/deploy/kustomization.yaml +++ /dev/null @@ -1,6 +0,0 @@ -apiVersion: kustomize.config.k8s.io/v1beta1 -kind: Kustomization -resources: - - deployment.yaml - - rbac.yaml - - config.yaml diff --git a/deploy/rbac.yaml b/deploy/rbac.yaml deleted file mode 100644 index 751b09c..0000000 --- a/deploy/rbac.yaml +++ /dev/null @@ -1,104 +0,0 @@ -apiVersion: v1 -kind: ServiceAccount -metadata: - name: kube-downscaler ---- -apiVersion: rbac.authorization.k8s.io/v1 -kind: ClusterRole -metadata: - name: kube-downscaler -rules: -- apiGroups: - - "" - resources: - - pods - - namespaces - verbs: - - get - - watch - - list -- apiGroups: - - apps - resources: - - deployments - - statefulsets - verbs: - - get - - watch - - list - - update - - patch -- apiGroups: - - argoproj.io - resources: - - rollouts - verbs: - - get - - watch - - list - - update - - patch -- apiGroups: - - autoscaling - resources: - - horizontalpodautoscalers - verbs: - - get - - watch - - list - - update - - patch -- apiGroups: - - batch - resources: - - cronjobs - verbs: - - get - - watch - - list - - update - - patch -- apiGroups: - - keda.sh - resources: - - scaledobjects - verbs: - - get - - watch - - list - - update - - patch -- apiGroups: - - zalando.org - resources: - - stacks - verbs: - - get - - watch - - list - - update - - patch -- apiGroups: - - "" - resources: - - events - verbs: - - get - - create - - watch - - list - - update - - patch ---- -apiVersion: rbac.authorization.k8s.io/v1 -kind: ClusterRoleBinding -metadata: - name: kube-downscaler -roleRef: - apiGroup: rbac.authorization.k8s.io - kind: ClusterRole - name: kube-downscaler -subjects: -- kind: ServiceAccount - name: kube-downscaler - namespace: default diff --git a/kube_downscaler/cmd.py b/kube_downscaler/cmd.py index c37979e..f72bb14 100644 --- a/kube_downscaler/cmd.py +++ b/kube_downscaler/cmd.py @@ -81,8 +81,8 @@ def get_parser(): ) parser.add_argument( "--exclude-deployments", - help="Exclude specific deployments from downscaling. Despite its name, this option will match the name of any included resource type (Deployment, StatefulSet, CronJob, ..). (default: kube-downscaler,downscaler)", - default=os.getenv("EXCLUDE_DEPLOYMENTS", "kube-downscaler,downscaler"), + help="Exclude specific deployments from downscaling. Despite its name, this option will match the name of any included resource type (Deployment, StatefulSet, CronJob, ..). (default: py-kube-downscaler,downscaler)", + default=os.getenv("EXCLUDE_DEPLOYMENTS", "py-kube-downscaler,downscaler"), ) parser.add_argument( "--downtime-replicas", diff --git a/kube_downscaler/helper.py b/kube_downscaler/helper.py index 545dbf7..6094475 100644 --- a/kube_downscaler/helper.py +++ b/kube_downscaler/helper.py @@ -107,7 +107,7 @@ def create_event(resource, message: str, reason: str, event_type: str, dry_run: { "metadata": { "namespace": resource.namespace, - "generateName": "kube-downscaler-", + "generateName": "py-kube-downscaler-", }, "type": event_type, "count": 1, @@ -124,7 +124,7 @@ def create_event(resource, message: str, reason: str, event_type: str, dry_run: "uid": resource.metadata.get("uid"), }, "message": message, - "source": {"component": "kube-downscaler"}, + "source": {"component": "py-kube-downscaler"}, }, ) if not dry_run: