Configuring JVM Container Startup Arguments

There are several standard performance-related startup arguments automatically provided when using the latest versions of java-service-base as a base image. This base image is used when including docker-maven-plugin-tile, web-docker-tile, and jib-maven-plugin-tile and the new java-service-base default version is set as of version 1.5.1.

If these tiles nor java-service-base are configured (for instance, when the standard Java or Java AppD base image is used), the necessary startup arguments should be specified within the Kubernetes Deployment manifest itself, within the containers env field. In addition, if a different value for ActiveProcessorCount is desired (default is 4), then it should be overridden in the Kubernetes Deployment, as well.

Default java-service-base Java Arguments

The default arguments passed into the java container startup command are set in the docker-maven-plugin or jib-maven-plugin configuration in java-service-base:

java-service-base 5.4.x
<plugin>
    <groupId>com.google.cloud.tools</groupId>
    <artifactId>jib-maven-plugin</artifactId>
    <version>${jib-maven-plugin.version}</version>
    <configuration>
        <!-- snip -->
        <container>
            <appRoot>/app/resources</appRoot>
            <entrypoint>/app/resources/entrypoint.sh</entrypoint>
            <environment>
                <TRACE_URL>http://zipkin:9411/api/v1/spans</TRACE_URL>
                <JAVA_OPTS>-XX:MaxRAMPercentage=50 -XX:ActiveProcessorCount=4 -Xss512k</JAVA_OPTS>
            </environment>
        </container>
    </configuration>
</plugin>
java-service-base 4.2.x
<plugin>
    <groupId>io.fabric8</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>${docker.maven.plugin.version}</version>
    <configuration>
       <!-- snip -->
        <images>
            <image>
                <build>
                    <env>
                        <JAVA_OPTS>-XX:MaxRAMPercentage=50 -XX:ActiveProcessorCount=4 -Xss512k</JAVA_OPTS>
                    </env>

The hard-coded garbage collector argument has been removed in favor of the JVM selecting it according to the container’s system characteristics, including the ActiveProcessorCount provided.

Overriding JAVA_OPTS

In order to customize the "base" Java startup arguments, the desired arguments should be set in the Kubernetes deployment.yaml file manually. If utilizing a local template, this change would be made in the template - otherwise it is made in the kubernetes/deployment.yaml at the root of the project.

In the env field of the container specification, the JAVA_OPTS environment variable should be added with the desired value.

For example, to override the ActiveProcessorCount with 2, the deployment.yaml would look something like:

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: $NAMESPACE
  labels:
    service: <service>-v1
  name: <service>
spec:
  replicas: $REPLICA_COUNT
  selector:
    matchLabels:
      service: <service>-v1
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
     # snip
    spec:
      containers:
      - env:
        - name: VAULT_ADDR
          valueFrom:
            configMapKeyRef:
              name: consul-vault-urls
              key: VAULT_ADDR
        - name: SECRET_PATH
          value: secret/$NAMESPACE/<service>/v1
        # snip
        - name: JAVA_OPTS
          value: -XX:MaxRAMPercentage=50 -XX:ActiveProcessorCount=2 -Xss512k (1)
1 Set the value to specify the same arguments the same except the ActiveProcessorCount, which is set to 2.
Setting the JAVA_OPTS environment variable in the Kubernetes configuration for the container overwrites the value provided at the "image" level, therefore you must repeat the entire set of arguments - these are not just appended.