Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hpa: Prevent scaling below MinReplicas if desiredReplicas is zero #48997

Merged
merged 1 commit into from Jul 17, 2017

Conversation

johanneswuerbach
Copy link
Contributor

@johanneswuerbach johanneswuerbach commented Jul 16, 2017

What this PR does / why we need it:
Prevent a HPA scaling below minReplicas if desiredReplicas is calculated as 0.

Example events of a HPA continuously scaling between 1 and MinReplicas:

2h        59s        22    horizontal-pod-autoscaler            Normal        SuccessfulRescale    New size: 1; reason: All metrics below target
2h        29s        22    horizontal-pod-autoscaler            Normal        SuccessfulRescale    New size: 15; reason: Current number of replicas below Spec.MinReplicas

Which issue this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close that issue when PR gets merged): fixes #49028

Special notes for your reviewer:

Release note:

hpa: Prevent scaling below MinReplicas if desiredReplicas is zero

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jul 16, 2017
@k8s-ci-robot
Copy link
Contributor

Hi @johanneswuerbach. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jul 16, 2017
@k8s-github-robot k8s-github-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. release-note Denotes a PR that will be considered when it comes time to generate release notes. labels Jul 16, 2017
desiredReplicas = *hpa.Spec.MinReplicas
} else {
desiredReplicas = 1
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure whether fallthrough would be more in-line with the coding style, happy to change if preferred.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about as this:

case desiredReplicas == 0:
    if hpa.Spec.MinReplicas != nil {
        //  never scale down to 0, reserved for disabling autoscaling
        setCondition(hpa, autoscalingv2.ScalingLimited, v1.ConditionTrue, "TooFewReplicas", "the desired replica count was zero and less than the minimum replica count")
        desiredReplicas = *hpa.Spec.MinReplicas
    } else {
        //  never scale down to 0, reserved for disabling autoscaling
        setCondition(hpa, autoscalingv2.ScalingLimited, v1.ConditionTrue, "TooFewReplicas", "the desired replica count was zero")
        desiredReplicas = 1
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of duplicating the condition I now aligned the order of cases to

} else if hpa.Spec.MinReplicas != nil && currentReplicas < *hpa.Spec.MinReplicas {
rescaleReason = "Current number of replicas below Spec.MinReplicas"
desiredReplicas = *hpa.Spec.MinReplicas
} else if currentReplicas == 0 {
rescaleReason = "Current number of replicas must be greater than 0"
desiredReplicas = 1
, which also prevents the broken behaviour. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for this, thanks @johanneswuerbach

desiredReplicas = 1
// make sure we aren't below our minimum if one is defined
if hpa.Spec.MinReplicas != nil {
desiredReplicas = *hpa.Spec.MinReplicas
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about logging a warning message here instead of setting the desiredReplicas silently?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm not sure I understand. In case of near zero load (which causes the desired replicas to be calculated as 0), the hpa should still try to downscale to a possible minimum.

This PR changes this behaviour only to scale to the defined minimum instead of hardcoded 1, which causes all except one running pod to be killed and then HPA scaling to the defined min during the next iteration with Current number of replicas below Spec.MinReplicas, which causes some unnecessary pod churn and increased load on the cluster.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I mean both adding a warning message and setting desiredReplicas as your current PR here, make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, isn't this desired behaviour that low-usage defaults to scaling to min replicas?

If I read https://kubernetes.io/docs/api-reference/v1.7/#horizontalpodautoscalerspec-v1-autoscaling correctly, minReplicas will default to 1 and can't be unset. Maybe the desiredReplicas == 0 case could be completely dropped and just be ensured that minReplicas is set. In that case the case hpa.Spec.MinReplicas != nil && desiredReplicas < *hpa.Spec.MinReplicas: should catch also the 0 case and handle it correctly.

@piosz piosz assigned DirectXMan12 and unassigned piosz Jul 17, 2017
@MaciekPytel
Copy link
Contributor

/ok-to-test

@k8s-ci-robot k8s-ci-robot removed the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jul 17, 2017
@johanneswuerbach
Copy link
Contributor Author

Semi-unrelated question: Is this something that would be back-ported to 1.7 if approved or is there anything I would need to do for that?

@MaciekPytel
Copy link
Contributor

@johanneswuerbach I am strongly convinced we need to cherry-pick it on 1.7 and make sure it gets included in the next patch release, as it is a serious regression in hpa. Let's wait for @DirectXMan12 (I think it's still pretty early in his timezone) to review this and than we can try to get it cherrypicked.

@DirectXMan12
Copy link
Contributor

/lgtm

this is supposed to have an associated issue. Please create one, add it to the PR body.

am strongly convinced we need to cherry-pick it on 1.7 and make sure it gets included in the next patch release, as it is a serious regression in hpa

Agreed, please do cherry-pick this to any appropriate versions.

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 17, 2017
@johanneswuerbach
Copy link
Contributor Author

Created and referenced #49028

@piosz
Copy link
Member

piosz commented Jul 17, 2017

/approve no-issue

@mikedanese
Copy link
Member

/lgtm

@mikedanese
Copy link
Member

/approve no-issue

@k8s-github-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: DirectXMan12, johanneswuerbach, mikedanese, piosz

Associated issue: 49028

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these OWNERS Files:

You can indicate your approval by writing /approve in a comment
You can cancel your approval by writing /approve cancel in a comment

@k8s-github-robot k8s-github-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 17, 2017
@MaciekPytel MaciekPytel added this to the v1.7 milestone Jul 17, 2017
@k8s-github-robot
Copy link

Automatic merge from submit-queue (batch tested with PRs 48997, 48595, 48898, 48711, 48972)

@k8s-github-robot k8s-github-robot merged commit aed912b into kubernetes:master Jul 17, 2017
@wojtek-t wojtek-t added the cherry-pick-approved Indicates a cherry-pick PR into a release branch has been approved by the release branch manager. label Jul 18, 2017
k8s-github-robot pushed a commit that referenced this pull request Jul 18, 2017
…48997-upstream-release-1.7

Automatic merge from submit-queue

Automated cherry pick of #48997

Cherry pick of #48997 on release-1.7.

#48997: hpa: Prevent scaling below MinReplicas if desiredReplicas is
@k8s-cherrypick-bot
Copy link

Commit found in the "release-1.7" branch appears to be this PR. Removing the "cherrypick-candidate" label. If this is an error find help to get your PR picked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cherry-pick-approved Indicates a cherry-pick PR into a release branch has been approved by the release branch manager. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

hpa scales below MinReplicas if desiredReplicas is zero