Events
17 Mar, 23 - 21 Mar, 23
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
This article describes how to containerize your Java applications for deployment on Kubernetes. For guidance on container memory, JVM heap memory, garbage collectors (GCs), and vCPU cores, see Containerize your Java applications.
Determine whether the Kubernetes node pool or pools that are available for your cluster can fit the container memory and vCPU cores that you intend to use. If the node pool can host the application, then continue on. Otherwise, provision a node pool that's appropriate for the amount of container memory and number of vCPU cores you're targeting.
Keep in mind that the cost of a VM SKU is proportional to the number of cores and amount of memory. After you determine your starting point in terms of vCPUs and memory for one container instance, determine whether you can meet your application's needs by horizontal scaling only. For reliable, always-on systems, a minimum of two replicas must be available. Scale up and out as needed.
If you must limit the CPU, ensure that you apply the same value for both limits
and requests
in the deployment file. The JVM doesn't dynamically adjust its runtime, such as the GC and other thread pools. The JVM reads the number of processors available only during startup time.
Tip
Set same value for CPU requests and CPU limits.
containers:
- image: myimage
name: myapp
resources:
limits:
cpu: "2"
requests:
cpu: "2"
When the HotSpot JVM in OpenJDK identifies that it's running inside a container, it uses values such as cpu_quota
and cpu_period
to determine how many processors are available to it. In general, any value up to 1000m
millicores are identified as a single processor machine. Any value between 1001m
and 2000m
is identified as a dual processor machine, and so forth. This information is available through the API Runtime.getRuntime().availableProcessors(). Some of the concurrent GCs might also use this value to configure their threads. Other APIs, libraries, and frameworks might also use this information to configure thread pools.
Kubernetes CPU quotas are related to the amount of time a process spends in the CPU, and not the number of CPUs available to the process. Multi-threaded runtimes such as the JVM might still use multiple processors concurrently, with multiple threads. Even if a container has a limit of one vCPU, the JVM might be instructed to see two or more available processors.
To inform the JVM of the exact number of processors it should be seeing in a Kubernetes environment, use the following JVM flag:
-XX:ActiveProcessorCount=N
Set the memory limits to the amount that you previously determined. Be sure the memory limits number is the container memory and NOT the JVM heap memory value.
Tip
Set the memory requests equal to the memory limits.
containers:
- name: myimage
image: myapp
resources:
limits:
memory: "4Gi"
requests:
memory: "4Gi"
Remember to set the JVM heap memory to the amount you previously determined. We recommend that you pass this value as an environment variable so you can easily change it without needing to rebuild the container image.
containers:
- name: myimage
image: myapp
env:
- name: JAVA_OPTS
value: "-XX:+UseParallelGC -XX:MaxRAMPercentage=75"
Events
17 Mar, 23 - 21 Mar, 23
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Containerize and deploy a Java app to Azure - Training
Containerize a Java app, push the container image to Azure Container Registry, and then deploy to Azure Kubernetes Service.
Documentation
Containerize your Java Applications - Java on Azure
This article provides an overview of recommended strategies for containerizing your Java applications.
Establish a Baseline for Containerized Java Applications - Java on Azure
This article describes how to establish a baseline for containerized Java applications
Java containerization strategies
Recommended strategies for containerizing your Java applications for deployment to the cloud.