When creating a Deployment and HorizontalPodAutoscaler together in a Helm chart, should the deployment’s .spec.replicas be set to null, or should it be unset, or should it be set to some value between the hpa’s minReplicas and maxReplicas?
When you create a hpa, the hpa controller manages the deployment’s .spec.replicas, so when you update the deployment’s other fields you shouldn’t change the replicas.
Comparing to kubectl apply declarative config, you can modify other fields of the deployment without modifying .spec.replicas if you leave .spec.replicas unset the first time the deployment is created, so that the 3-way diff ignores that field when the deployment is applied in the future. Or to omit the field after creation time, you have to use kubectl apply edit-last-applied to avoid accidentally scaling down to 1 when removing the field (kubernetes/kubernetes#67135). So with kubectl apply it is possible to apply a deployment while not touching .spec.replicas.
What is the correct way to helm upgrade a deployment’s other fields without changing its scaling?
I checked here and If I understand correctly there are 2 ways of doing that.
1.Add if statement as a workaround.
There is comment about it added by @naseemkullah.
so the workaround is to add an if statement around the deployment's spec.replicas to not template it if HPA is enabled
2.Remove replicas field completely, then it should respect the replicas number managed by HPA.
There is comment about it added by @tianchengli.
If I remove replicas field completely, it will respect the replicas number managed by HPA.
To answer your two actual questions:
replicas field in your Deployment or StatefulSet. Leave it out. When helm renders your Deployment template during helm upgrade and patches the Deployment object in Kubernetes, the replicas field will not be part of the patch and the existing field will be left alone (it will continue being managed by the HPA controller).replicas field as stated above.If you initially specify a replicas field in the Helm template and deploy it, and also configure an HPA to autoscale the Deployment, you will run into a nasty situation as described in Helm issue #7090 when you attempt to remove or otherwise modify the replicas field in the template. I left a comment describing how to safely escape and correct this when desperate (by manually editing the Helm release object), but it's a bit involved. Cheers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With