How to manage Java Spring applications autoscaling in Kubernetes PROPERLY? How to manage Java Spring applications autoscaling in Kubernetes PROPERLY? kubernetes kubernetes

How to manage Java Spring applications autoscaling in Kubernetes PROPERLY?


It is true that Spring applications are not the most container friendly thing out there but there are few things you can try:

  1. On startup, Spring autowires the beans and performs dependency injection, creates objects in memory, etc. All of those things are CPU intensive. If you assign less CPU to your pod, it will logically increase the startup time.Things you can do here are:
  • Use a startupProbe and give time to your application to start. Itis explained pretty good here on how to calculate the delays andthresholds

  • Adjust the maxSurge and maxUnavailable in your deploymentstrategy as it fits best to your case (for example, maybe you have 10replicas and max surge /max unavailable of 10% so your pods willrollout slowly, one by one). This will help to reduce spikes intraffic on the overall application replicas (docs are here).

  • If your use case allows, you can look into lazy loading your Springapplication, meaning that it will not create all objects uponstartup, rather it will wait until they are used. This can besomewhat dangerous due to potentially not being able to discoverissues on startup in some cases.

  1. If you have HPA enabled + defined replicas value in the deployment, you might experience issues upon deploying, I can't find the relevant GH issue ATM but you might want to run some tests there on how it behaves (scaling more than it should, etc). Things you can do here are:
  • Tweak the autoscaling thresholds and times (default is 3min, afaik) to allow your deployments to rollout smoothly without triggering the autoscale.

  • Write a custom autoscaling metric instead of scaling by CPU. This one requires some work but might solve your scaling issues for good (relevant docs).

Lastly, what you are suggesting with a sidecar looks like a hack :)Haven't tried it though so can't really tell the pros and cons.

Unfortunately, there is no silver bullet for Spring Boot (or Java) + K8s but things are getting better than they were a few years back. If I find some helpful resources. I will come back and link them here.

Hope the above helps.

Cheers