The original address: https://blog.lanweihong.com/p…

preface

In the micro-service architecture, a series of components, such as gateway, registry, configuration center, service tracking, authentication center, etc. will occupy a certain amount of memory when they are deployed to the server. There are also various business services, which may account for hundreds of megabytes or even more than G once they are deployed. So the deployment of this set of components and services at the same time must not consume more memory? To prevent these services from running out of system resources and causing downtime, we have to configure memory limits for these services.

In the K8S environment, if you configure only the memory limit, but do not configure the JVM parameters of the Java application, it may cause the Java application to run in memory exceed the memory limit, send OOM error. Therefore, JVM parameters and K8S memory limits are configured when deploying Java applications to the K8S environment.

JVM parameter configuration method

Depending on the Java version, there are different methods, as shown in the table below:

JDK version The JVM parameter
<8u131 -Xms64m -Xmx128m
8u131-191 -XX:+UnlockExperimentalVMOptions,-XX:+UseCGroupMemoryLimitForHeap
>8u191 -XX:UseContainerSupport(enabled by default),ActiveProcessorCount; Percentage allocated heap memory:MaxRAMPercentage,InitialRAMPercentage,MinRAMPercentage

When the JDK8 version is less than 131, when starting the Java program, add the parameter -xms64m -Xmx128m; Java u131 + 8 and 9 + Java version, add two parameters: – XX: XX: + UnlockExperimentalVMOptions – + UseCGroupMemoryLimitForHeap; If the JDK8 version is above 191, use MaxRAMPercentage. The MaxRAMPercentage value ranges from 0.0 to 100.0. The default value is 25.0.

configuration

8 u131 JDK version

Configure in the Dockerfile:

ENV JVM_OPTS -Xms256m -Xmx512m
ENTRYPOINT exec java $JVM_OPTS -jar lanweihong.jar

Note that these parameters only set the size of the heap allocated, the actual memory limit should be greater than this.

Java 8U131 + and Java 9+ versions

For Java 8U131 + and Java 9+ versions, in the Dockerfile file, set the environment variable:

ENV JVM_OPTS -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -XX:MaxRAMFraction=2 ENTRYPOINT exec java  $JVM_OPTS -jar lanweihong.jar

Among them – XX: + UnlockExperimentalVMOptions – XX: + UseCGroupMemoryLimitForHeap Settings – XX: MaxRAM memory limit for the cgroup, Here, -XX: maxramFraction is 2, so the memory allocated by the JVM is MaxRAM/maxramFraction = 4G / 2 = 2G. It is not set to 1 to prevent the JVM from occupying all the memory. Causes other processes (such as Shell, MySQL, etc.) to have no available memory.

JDK version 8 u191 +

Configure in the Dockerfile:

ENV jvm_opts-xx :MaxRAMPercentage=80.0 entryPoint exec Java $jvm_opts-jar lanweihoun.jar

Setting the amount of memory available to the JVM to 80% of total memory is obviously more flexible and more secure.

If it is packaged with the jib-maven-plugin, you can add the configuration in the pom.xml:

. <container> <mainClass>com.lanweihong.gateway.GatewayApplication</mainClass> <! > <jvmFlag> <jvmFlag>-XX:MaxRAMPercentage=80.0</jvmFlag> </jvmFlag> </container>...

Java-xx :MaxRAMPercentage=80.0…

K8s configuration

Set memory limit; Add: in the K8S deployment profile:

containers: resources: requests: memory: "512Mi" # cpu: "500m" limits: memory: "512Mi" # cpu: "500m" env: - name: JVM_OPTS values: - XX: MaxRAMPercentage = 80.0

Update the publishing service using Kubectl Apply-f.

conclusion

  1. JDK8 versions below 191 are recommended to upgrade to the latest version, or after 191, Java 8U191 and Java 10+ are supportedUserContainerSupport, set JVM startup parametersMaxRAMPercentage, the specific value is set according to the situation;
  2. If you do not upgrade the JDK version, configure according to the JDK version as described above;
  3. Kubernetes configures Limit.

Reference documentation

  1. How To Configure Java Heap Size Inside a Docker Container
  2. Kubernetes_pod_javajdk_ Dynamic JVM heap size limit