VOOZH about

URL: https://thenewstack.io/how-to-optimize-java-apps-on-kubernetes/

⇱ How to Optimize Java Apps on Kubernetes  - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2022-07-22 06:09:18
How to Optimize Java Apps on Kubernetes 
contributed,sponsor-stormforge,sponsored,sponsored-post-contributed,
Kubernetes

How to Optimize Java Apps on Kubernetes 

Kubernetes allows us to restrict the resources that the containers can use. But key challenges remain in running Java apps efficiently on Kubernetes.
Jul 22nd, 2022 6:09am by Patrick Tavares
👁 Featued image for: How to Optimize Java Apps on Kubernetes 
Feature image via Pixabay.
StormForge sponsored this post. Insight Partners is an investor in StormForge and TNS.
Editor’s Note: This is the fourth of a five-part series covering Kubernetes resource management and optimization. In this article, we walk through the process of optimizing Java applications using machine learning. Previous articles in the series explained Kubernetes resource types, requests and limits and using machine learning to automate optimization.
Patrick Tavares
Patrick is a senior solutions architect at StormForge. He has more than 20 years of experience in systems engineering, architecture, development and consulting with various companies like Red Hat, Verizon Business and Deloitte.

Java is an object-oriented language that’s been around since the 1990s. Originally designed to make application development easier, consistent upgrades through the years have kept Java up to date and increased its cross-platform capabilities. Consequently, Java is still a favorite for many developers, even in the modern world of containers and Kubernetes.

While Java is a favorite for many reasons, there are also significant challenges related to resource management, such as resource contention. Kubernetes solved part of the resource management problem by addressing the resource contention issues.

Before containers, we would deploy processes onto a machine and give them access to all its memory and CPU processing time. This meant that each process viewed the unused CPU time and memory as its own. It’s like having a bunch of children who all believe they own the playground. Conflicts would frequently occur whenever more than one process claimed more resources to remain operational.

Fast forward to today.

Kubernetes allows us to deploy containers to a machine and restrict the resources that the containers can see and use. You can think of it as subdividing a playroom with walls to ensure each child has access only to their own allocated play area. This allows Kubernetes to have greater control over resource use within a cluster. But key challenges remain in running Java apps efficiently on Kubernetes.

This article will delve into those issues and cover best practices for optimizing Java apps on Kubernetes.

StormForge continuously rightsizes Kubernetes workloads to ensure applications are both cost effective and performant while removing developer toil. As a vertical rightsizing solution, StormForge is autonomous, tunable, and works seamlessly with horizontal pod autoscalers (HPAs) at enterprise scale. Insight Partners is an investor in StormForge and TNS.
Learn More

Why Optimizing a Java Application Is Painful

While you can tune a Java application for cost or performance, optimizing is about providing the best tradeoff between these objectives given your specific business goals. In other words, providing the right level of performance to meet service-level agreements (SLAs) at the lowest possible cost. There isn’t a one-size-fits-all solution. For instance, a transaction-focused app would value throughput while a computationally intensive app might put more value on completion time. Therefore, it’s essential to understand the priorities for your application to create the proper blend of cost with performance.

Another factor to consider is that a Java application runs on a Java virtual machine (JVM) to which the deployment allocates memory heap size. We see how this works later in this tutorial.

When deploying a Java application as a microservice without specifying requests and limits, Kubernetes determines the resources to be allocated. The challenge here is that it’s usually very generous with the assigned resources, making our cluster expensive to operate. Suppose we decide to allocate the resources manually. Then we must choose one of these three options:

  • Over-provision resources to ensure we aren’t frequently facing out-of-memory or CPU throttling scenarios
  • Scale back resources and accept the risks of out-of-memory errors and CPU throttling
  • Spend significant time and effort determining the right balance of resources through manual trial and error

What about the point where the resources aren’t too high or too low and the performance is just right? Yes, there is such a point. However, manually getting to that “sweet spot” is like searching for a needle in a haystack. It’s a long, near-impossible task. We would need to configure CPU limits/requests, memory limits/requests, replicas, the JVM heap, garbage collection and many other parameters. And a change in any of these parameters will affect overall resource use and application performance. It’s a balancing act that can quickly get out of hand.

But we don’t have to take risks or guess. Machine learning-based optimization solutions are available that allow us to run experiments on our Kubernetes-based, Java applications and recommend configurations with the best performance at the lowest possible cost.

For the remainder of this article, we’ll walk step by step through the process of optimizing an example Java app using one such solution, StormForge Optimize Pro. Note that this process will be different if you’re using a different optimization solution.

Java Microservice Optimization Demo

This demo explains how to optimize a Java application running in Kubernetes to get the best performance at the lowest possible resource cost. We do this in a non-prod environment using a process of experimentation.

For example, let’s create an experiment that simulates 80 different trials with different parameters, computes the cost and performance level, and then recommends optimal configurations.

Before we start, here’s what we need:

Creating the Cluster and Configuring the Necessary Tools

To create a Kubernetes cluster using version 1.21, run this command:

$ minikube start --kubernetes-version v1.21.0

Set the environment variables by running these three commands separately:

`$ mkdir -p $HOME/.kube
$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config`

Check the configuration:

$ kubectl get nodes

Download and install the StormForge CLI from the install documentation.

Log in to your StormForge account:

$ stormforge login --url

Verify the API connection:

$ stormforge ping

Initialize StormForge by running this command:

$ stormforge init

Verify that you configured the StormForge Controller properly:

$ stormforge check controller

Creating and Running Our Experiment

In this section, we will download the demo project, create a deployment, and create and run an experiment that consists of 80 trials. After the experiment is complete, we’ll view the recommended configurations and apply one of them.

You can find the example we use here. Our initial configurations are in the `experiment.yaml` file.

Before starting our experiment, let’s do one last check.

Jump to your StormForge app and confirm that you added a cluster. It should appear on the cluster page like this:

👁 Image

If the cluster is there, you are ready for takeoff! If you can’t see it, refresh the web page.

Clone the example experiments repo:

`$ git clone https://github.com/thestormforge/examples.git
$ cd examples`

Create and launch the Java-tuning experiment:

$ kubectl apply -f jvm/experiment.yaml

Check the current status of the experiment by running this command:

$ kubectl get trials -o wide -w

You should now see the experiment status similar to the screenshot below.

👁 Image

When complete, go back to your StormForge app and navigate to the experiment by first clicking on the jvm-reactors application.

👁 Image

Click the renaissance-associated scenario.

👁 Image

Click the jvm.reactors experiment run.

👁 Image

From the experiment results screen, choose the highlighted recommended trial. It should look like this:

👁 Image

Run this command to get the pod name of the trial:

$ kubectl get pods

It should look like this:

👁 Image

After getting the pod name, run this command to view the pod configurations:

$ kubectl describe pod <pod name>

The blue box in the screenshot below shows us the recommended configurations:

👁 Image

Here is a table summarizing the changes that will give the best performance at the lowest possible cost.

👁 Image

Click Export the configuration to download the YAML file containing the optimal configurations. That’s it. You now have the configuration discovered by ML-based experimentation for your Java microservice optimized for duration and cost by tuning memory, CPU, heap size and garbage collection.

Conclusion

The Java programming language has been around for many years and has been used to build powerful apps. However, running a Java application on Kubernetes presents a unique challenge because of the multitude of configuration settings that need to be considered for each unique application. If not correctly configured, the application can fail to run as expected and drastically increase costs.

StormForge uses machine learning to help you find answers to questions such as “What configurations would result in the best performance at the lowest possible price? Beyond Java, StormForge can optimize any workload on Kubernetes. Use it to maximize the value of your Kubernetes cluster.

Stay tuned for the final article in the Kubernetes resource management and optimization series covering database optimization.

StormForge continuously rightsizes Kubernetes workloads to ensure applications are both cost effective and performant while removing developer toil. As a vertical rightsizing solution, StormForge is autonomous, tunable, and works seamlessly with horizontal pod autoscalers (HPAs) at enterprise scale. Insight Partners is an investor in StormForge and TNS.
Learn More
TRENDING STORIES
Patrick Tavares is a senior solutions architect at StormForge. He has more than 20 years of experience in systems engineering, architecture, development and consulting with various companies like Red Hat, Verizon Business and Deloitte.
Read more from Patrick Tavares
StormForge sponsored this post. Insight Partners is an investor in StormForge and TNS.
SHARE THIS STORY
TRENDING STORIES
TNS owner Insight Partners is an investor in: StormForge.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.