Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/maheshrayas/kube-saver

Cost saving K8s controller to scale down and up of resources during non-business hours
https://github.com/maheshrayas/kube-saver

cost-optimization deployment downscaling kubernetes kubernetes-controller namespaces statefulsets

Last synced: 3 months ago
JSON representation

Cost saving K8s controller to scale down and up of resources during non-business hours

Awesome Lists containing this project

README

        


Kube-Saver






## Motivation

* Scale down cluster nodes by scaling down Deployments, StatefulSet, CronJob, Hpa
during non-business hours and save $$, but if you need to scale back the resources eventhough its a scaledown, don't worry, You will have a Custom Resource which will scale up all resources and wont scale down until next scaledown period.

## Installation

* Install CRD

```bash
kubectl apply -f https://raw.githubusercontent.com/maheshrayas/kube-saver/main/k8s/crds/crd.yaml
```

* Configure your rules in [rules.yaml](k8s/rules.yaml)

* Install kube-saver operator

```bash
kubectl apply -k k8s/
```

## Examples

```bash
rules:
# scale down deployment with name go-app-deployment-2 when current time/day not in uptime
- id: rules-downscale-deployments
uptime: Mon-Fri 09:00-17:00 Australia/Sydney
jmespath: "metadata.name == 'go-app-deployment-2'"
resource:
- Deployment # type of resource
replicas: 0 # either set the replicas:0 or any number during nonuptime
# scale down all deployment, statefulset, cronjob, hpa in namespace kuber when current time/day not in uptime, in this case hpa will be set to 1 as the desired replicas is set as 0
- id: rules-downscale-all-deployments-in-namespace
uptime: Mon-Fri 09:00-17:00 Australia/Sydney
jmespath: "metadata.name == 'kuber'"
resource:
- Namespace # type of resource
replicas: 0 # either set the replicas:0 or any number during nonuptime
# scale down all statefulset in namespace kuber when current time/day not in uptime
- id: rules-downscale-all-statefulset
uptime: Mon-Fri 09:00-17:00 Australia/Sydney
jmespath: "metadata.name == 'statefulset_name'"
resource:
- StatefulSet # type of resource
replicas: 0 # either set the replicas:0 or any number during nonuptime
# disable all cronjob with the labels current time/day not in uptime
- id: rules-disable-all-cronjob
uptime: Mon-Fri 09:00-17:00 Australia/Sydney
jmespath: "metadata.labels.app == 'some_random_app'"
resource:
- cronjob # type of resource
# set minReplicas of HPA to 1
- id: rules-set-hpa
uptime: Mon-Fri 09:00-17:00 Australia/Sydney
jmespath: "metadata.labels.app == 'some_random_app'"
resource:
- hpa # type of resource
replicas:1
# set replicas to 0 when the resources has label app:some_random_app but not service:some_random_service
- id: combination-of-resources
uptime: Mon-Fri 09:00-17:00 Australia/Sydney
jmespath: "metadata.labels.app == 'some_random_app' && metadata.labels.service != 'some_random_service'"
resource:
- Deployment # type of resource
- Statefulset
replicas:0

```

* Define uptime in [Olson timezone](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) format

More Examples: [here](./examples/example.md)

## How can I upscale resouce during the downtime.?

kube-saver will automatically upscale the resoures to orignial number of replicas when the current time falls between the `uptime` configured in rules.yaml. But if you want to manually scale up single deployment/statefulset or all the deployment & stateulset resources in Namespace, you have following options and it won't be scaled down until next day downtime.
Choose any of the option below:

* Configure [upscaled.yaml](./k8s/crds/upscaler.yaml) and

```bash
kubectl apply -f ./k8s/crds/upscaler.yaml

```

Or

* Redeploy your deployment.

Or

* Edit the uptime in [rules.yaml](./k8s/rules.yaml) and redeploy the operator

```bash
kubectl apply -k k8s/
```

## Tested

| Kubernetes Provider | Tested |
|----------------------|--------|
| Google Kubernetes Engine | ✅ |
| KIND(no autoscaler) | ✅ |