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?
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
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.