keda icon indicating copy to clipboard operation
keda copied to clipboard

desiredReplicas ignored in scaledObject with trigger cron

Open gerbil opened this issue 3 months ago • 3 comments

Report

desiredReplicas is ignored completely when using trigger cron for CRD based deployment

ScaledObject:

apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
  name: backend
  namespace: test
spec:
  scaleTargetRef:
    apiVersion: deployment.xxx.com/v1alpha1
    kind: ApplicationDeployment
    name: backend
  triggers:
    - metadata:
        desiredReplicas: '5'
        end: 30 * * * *
        start: 0 * * * *
        timezone: Europe/Riga
      type: cron

Expected Behavior

desiredReplicas should be respected by scaledObject cron based feature

triggers:
    - metadata:
        desiredReplicas: '5'

Actual Behavior

ScaledObject ref always restored to replicas: 1 ignoring desiredReplicas

2024-05-15T11:00:11Z	INFO	scaleexecutor	Successfully updated ScaleTarget	{"scaledobject.Name": "backend", "scaledObject.Namespace": "ivp-test", "scaleTarget.Name": "backend", "Original Replicas Count": 0, "New Replicas Count": 1}

Steps to Reproduce the Problem

  1. Create CRD based deployment with replicas = 5
  2. Create ScaledObject for the ref in step1 with triggers: -metadata: desiredReplicas: '5'
  3. Wait for scale down action
  4. Wait for scale up action

Logs from KEDA operator

2024-05-15T10:33:11Z	INFO	scaleexecutor	Successfully set ScaleTarget replicas count to ScaledObject minReplicaCount	{"scaledobject.Name": "backend", "scaledObject.Namespace": "ivp-test", "scaleTarget.Name": "backend", "Original Replicas Count": 5, "New Replicas Count": 0}
2024-05-15T11:00:11Z	INFO	scaleexecutor	Successfully updated ScaleTarget	{"scaledobject.Name": "backend", "scaledObject.Namespace": "ivp-test", "scaleTarget.Name": "backend", "Original Replicas Count": 0, "New Replicas Count": 1}

KEDA Version

2.14.0

Kubernetes Version

1.27

Platform

Microsoft Azure

Scaler Details

No response

Anything else?

No response

gerbil avatar May 15 '24 11:05 gerbil

This is by design, I believe, but I still think we should kill it in favor of https://github.com/kedacore/keda/issues/3566 @kedacore/keda-maintainers

tomkerkhove avatar May 16 '24 12:05 tomkerkhove

What exactly is by design here? desiredReplicas is a part of ScaledObject/triggers spec and it's not working as expected.

gerbil avatar May 17 '24 06:05 gerbil

Hello @gerbil It's working as expected because the scaling to 5 is happening, as we can see here:

2024-05-15T10:33:11Z	INFO	scaleexecutor	Successfully set ScaleTarget replicas count to ScaledObject minReplicaCount	{"scaledobject.Name": "backend", "scaledObject.Namespace": "ivp-test", "scaleTarget.Name": "backend", "Original Replicas Count": 5, "New Replicas Count": 0}

KEDA is scaling from 0 to 1 because KEDA relies on the HPA to scaling from 1 to N, so the "usual" process is that KEDA scales from 0 to 1 when there is at least 1 active trigger, and then the HPA scales the workload from 1 to N. desiredReplicas is the value that the trigger will return to the HPA Controller. In the same way as with other scalers, if your queue returns 50, KEDA won't scale from 0 to 50 directly. This is explained here: https://keda.sh/docs/2.14/concepts/scaling-deployments/#activating-and-scaling-thresholds

Given this, if you want to scale from 0 to 5 directly, it's doable. You can use idleReplicaCount option to allow scaling to 0 with min replicas 5, and then KEDA will scaling from 0 to 5 and from 5 to 0 without passing from 1 and the HPA:

apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
  name: backend
  namespace: test
spec:
  minReplicaCount: 5
  idleReplicaCount: 0
  scaleTargetRef:
    apiVersion: deployment.xxx.com/v1alpha1
    kind: ApplicationDeployment
    name: backend
  triggers:
    - metadata:
        desiredReplicas: '5'
        end: 30 * * * *
        start: 0 * * * *
        timezone: Europe/Riga
      type: cron

JorTurFer avatar May 18 '24 09:05 JorTurFer