Skip to content

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

License

Notifications You must be signed in to change notification settings

maheshrayas/kube-saver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

    kubectl apply -f https://raw.githubusercontent.com/maheshrayas/kube-saver/main/k8s/crds/crd.yaml
  • Configure your rules in rules.yaml

  • Install kube-saver operator

    kubectl apply -k k8s/

Examples

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

More Examples: here

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 and

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

    Or

  • Redeploy your deployment.

    Or

  • Edit the uptime in rules.yaml and redeploy the operator

    kubectl apply -k k8s/

Tested

Kubernetes Provider Tested
Google Kubernetes Engine
KIND(no autoscaler)