Kubernetes args with java options

Hello, everyone! I’m not good at kubernetes and I’m trying to figure out how to work with this. Currently I am working with java application and I’m going to deploy it witin container.
I have prepared Docker file with

ENTRYPOINT ["java", "-jar", "java_j.jar"]

in my java application.
I have prepared some helm charts too. And the main my question is:
Is it possible to use only one variable to specify all java options ineterested by me in it to use it within container.args (Deployment.yaml)?

{root}/values.yaml:

TEST_JAVA_OPTS = "-XX:+UseSerialGC"
TEST_JAVA_MEMORY_OPTS = "-Xmx256m -XX:MetaspaceSize=64m"

{root}/templates/Deployment.yaml

...
spec:
   containers:
      - name: test-java-service
        command:
           - java
           - '{{ .Values.TEST_JAVA_MEMORY_OPTS }}'
           - '{{ .Values.TEST_JAVA_OPTS }}'
           - -jar
           - java_j.jar
  ...

For now it doesn’t work to me because each my application startup failes with Improperly specified VM option. I guess it tryies to give java entire string as one java option. That is wrong of course.
My purpose is to avoid a lot of variables for each java option and to let change it in Deployment directly (I know that there is a possibility to set enviroment variables in Dockerfile at ENTRYPOINT part but let assume this option is disabled for us)

Thank you!

Kubernetes version: 1.28.12

1 Like

Yes, you can achieve this by passing all Java options as a single environment variable and then using container.args to split them correctly. Instead of specifying each option as a separate Helm value, concatenate them in values.yaml and use eval or bash to process them properly.

Solution

Modify your values.yaml:

JAVA_OPTS: "-XX:UseSerialGC -Xmx256m -XX:MetaspaceSize=64m"

Modify Deployment.yaml:

spec:
  containers:
    - name: test-java-service
      command:
        - java
      args:
        - "{{- with .Values.JAVA_OPTS }}{{ . }}{{- end }}"
        - -jar
        - java_j.jar

Alternatively, you can use an env variable and reference it in args:

env:
  - name: JAVA_OPTS
    value: "{{ .Values.JAVA_OPTS }}"
args:
  - "-jar"
  - "java_j.jar"

Then modify your Dockerfile:

ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar java_j.jar"]

This approach ensures the Java options are correctly parsed and prevents improper VM option errors. :rocket:

Scarlet, hello!
Thank you for your answer
May I ask what this construction do:

"{{- with .Values.JAVA_OPTS }}{{ . }}{{- end }}"

UPD: I got it why it’s used but why do we need this construction?
UPD2: I tried with the 1st solution. It still gives me the same issue

1 Like