-
Notifications
You must be signed in to change notification settings - Fork 40k
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
Expected behavior for Deployment replicas with HPA during update #25238
Comments
cc @kubernetes/autoscaling The scaling of a deployment is based on the |
@kubernetes/deployment |
This is more an issue when we have 20 replicas under load and roll a new Deployment and the replicas go from |
/cc @janetkuo |
There are multiple issues here. Do not set the replicas field in Deployment if you're using Also, I'm not sure HPA can be expected to be stable right now with large maxUnavailable and maxSurge values. Maybe try maxUnavailable=0 and maxSurge=1. Finally, Deployment doesn't do a good job of detecting scaling behavior right now, since it's fairly tricky to do robustly. What I'd like it to do is to distinguish scaling from other changes and then scale proportionally. #20754 |
I agree with @verdverm - this was unexpected behavior for me as well. At a minimum, I think the Deployment docs should call this out. I would like to be able to utilize the same deployment.yml file for both initial deploy and updates (all is done through CI/CD). It seems like the 'proper' way is to not specify a replicas in the Deployment (defaults to 1?) and apply the HPA profile when launching a new service. |
Your suggested changes makes the observed behavior in line with what we were expecting. Thanks for all your help! |
I wrote an explanation of apply behavior here: |
@bgrant0607 I am not noticing this. It seems when I |
Are we sure we want to deliver this for 1.6? Not much time left. As I understand, the problem here lies in |
The milestone was probably added for triaging. I don't think we can do anything for 1.6. |
I ended up implementing a hpa_replicas function and replacing its returned value in the deployment YAML. |
@pinowthebird
it's a bit more complicated than that; yes not specifying will set a value of 1 if nothing else happens; but once "not specified" and handled by hpa; further applies will indeed do the right thing and not reset the pod count to 1 (you can see doing Note that if you had a |
I'm trying to follow the recommendation in https://github.com/kubernetes/website/pull/29739/files for migrating deployments to HPA, specifically recommendation 1. Note that the patch verb exhibits the same behavior of scaling the replica count down to 1, although remove the spec.replica field from the manifest then applying doesn't.
|
@apelisse Is there a way with SSA to specify an initial value of a field, such as spec.replicas, but not take ownership of it? Discussion with @tamalsaha: |
We wrote this piece of documentation to describe how one should do that. I'd love to hear feedback and if this doesn't work! |
@apelisse thank you for the documentation, it's very clear. However, I don't think it is usable in most real word scenarios where we store our yamls in git and use helm/CI/controllers to apply new configs. |
Because they rely on multiple steps? i.e. "commit this, apply that, commit that, apply this" kind of sequence? |
@apelisse because a special logic would need to be added to any tool/system which adds the flag only when adding an hpa to an existing deployment. |
What special logic needs to be built? I guess I must be missing that part. |
The logic is:
Please correct me if I misunderstood how this new feature works. |
That is helpful if you're using |
I wanted to contribute with my 2-cents: We have faced the issue of defining |
We had the same problem, we ended up by implementing a mutating webhook with Kyverno to make sure the current Example: apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: general-deployment-hpa-replicas
annotations:
policies.kyverno.io/title: HPA replicas spec
policies.kyverno.io/category: Standard
policies.kyverno.io/severity: high
policies.kyverno.io/subject: Deployment
policies.kyverno.io/description: >-
Prevent spec.replicas to be overriden when HPA handles the deployment replicas
spec:
background: false
schemaValidation: false # Issue: https://github.com/kyverno/kyverno/issues/2748
rules:
- name: Deployment spec replicas
match:
resources:
kinds:
- Deployment
preconditions:
all:
- key: "{{ request.object.metadata.labels.hpa || '' }}"
operator: Equals
value: "true"
- key: "{{request.object.spec.replicas }}" # Allow to scale down to zero without changes
operator: GreaterThan
value: 0
- key: "{{request.operation}}"
operator: Equals
value: UPDATE
mutate:
patchStrategicMerge:
spec:
replicas: '{{request.object.status.replicas}}' |
@victorboissiere assuming that your manifests/helm charts/anything else are stored as code in git, wouldn't that create a diff when using kubectl diff/helm diff? |
In our case (deployment via Helm chart) we just removed the replicas field from the deployment manifest and let the hpa to handle all of it. |
@apelisse as far as I understand this now works properly since 1.22 if you use the server-side apply, which means to make it work for Helm and others, they need to implement server-side apply? Is that correct? |
If you use server-side apply, and remove the value from your manifest before you apply, then yes, this should work properly. |
Thanks for confirming 👍 This is good if you use kubectl, but for anyone using gitops and tools like Helm this won't work. What still needs to be done so that this issue can be resolved/fixed in general and that it works correctly with all tools? |
Thank you for this Awesome Discussion @kustodian. I Just Fixed this issue temporary with kubectl. Please Let me Know if there any workarounds to run this flag for helm and ArgoCD Only. Thanks |
@rbroggi broggi, How did you work around this? We're seeing this issue in our environment lately. |
Hey @madibaT , it was a while ago so take this with a grain of salt - I think you need to:
|
I'm still seeing this issue if the HPA is added and |
I am seeing unexpected behavior from a user standpoint when
kubectl apply -f mydeployment.yml
The existing Deployment has an HPA associated with it and the current replica count is 3. When the deployment is accepted, the number of replicas jumps to 9, then scales down to 6, the value in the deployment. Eventually, the new pods info starts flowing in and the HPA does its thing and scales back to satisfaction.
The behavior I expected is that the rollout would use the current replicas from the HPA and the strategy parameters from the Deployment to keep replica count in line with the current target set by the HPA.
k8s version: 1.2.3
The text was updated successfully, but these errors were encountered: