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

8/27/2019

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?

-- Imrahamed
google-kubernetes-engine
kubernetes-ingress

2 Answers

8/30/2019

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
-- dany L
Source: StackOverflow

9/6/2019

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

-- Zodraz
Source: StackOverflow