Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to automatically scale down to 0 when a pods is not used for some time?

I have N number of statefulsets and each of them deployed to their unique host via nginx ingress.

For example:

abcde.example.com - Statefulset 1

pqrstu.example.com - Statefulset 2

So here i want to scale down my statefulset replicas to 0 when no one is accessing it for some time (ex: 3days). is this possible in kubernetes?

like image 827
Imrahamed Avatar asked Dec 07 '25 20:12

Imrahamed


2 Answers

HPA can be applied to Statefulsets, however there is a caveat. You will be able to autoscale this way:

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: YOUR_HPA_NAME
spec:
  maxReplicas: 3
  minReplicas: 1
  scaleTargetRef:
    apiVersion: apps/v1
    kind: StatefulSet
    name: YOUR_STATEFUL_SET_NAME
  targetCPUUtilizationPercentage: 80

but setting minReplicas: 0 will produce the following error

The HorizontalPodAutoscaler "xxxxxx" is invalid: spec.minReplicas: Invalid value: 0: must be greater than 0
like image 57
dany L Avatar answered Dec 11 '25 10:12

dany L


You can try KEDA. It is on early stages but it is a promising technology with really a bright future (IMHO).

KEDA allows for fine grained autoscaling (including to/from zero) for event driven Kubernetes workloads. KEDA serves as a Kubernetes Metrics Server and allows users to define autoscaling rules using a dedicated Kubernetes custom resource definition.

https://github.com/kedacore/keda

like image 31
Zodraz Avatar answered Dec 11 '25 09:12

Zodraz