Passing java_opts to spring boot applications in kubernetes
Asked Answered
H

4

5

Currently, we are building Docker images with an entrypoint and passing this image to a Kubernetes deployment.

Is there any way to pass the entrypoint directly to Kubernetes dynamically so that it starts spring boot applications?

What are the different ways of passing this entrypoint directly in Kubernetes?

### Runtime image ###
FROM openjdk:8-jre-alpine

#Set working dir
WORKDIR /test

# Copy the executable JAR
COPY /*.jar /test/

# Run the app
ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -Djsversion=1.0 -D<variable>=<service-1> -D<variable>=<service-2>   -jar  *.jar
Huonghupeh answered 13/12, 2019 at 16:46 Comment(0)
S
3

You can use command in k8s deployment manifest:

containers:
- name: mycontainer
  env:
  - name: NAME
    value: VALUE
  command: [ "java"]
  args: ["-jar", "-D..."]
Spall answered 13/12, 2019 at 16:56 Comment(5)
Thanks for the inputs.. I have 100's of microservices with same arguments,... Is it possible to do using configmaps... as it is easy for me to change in one place,... otherwise I need to change the argument values in each and every deployment yaml fileHuonghupeh
You can't put the command into a config map, but you can put the environment variable values into a config map.Spall
Yes, I got it... but how do I pass the config-maps along with command... so that it will start the java process along with container. Could you please provide me with an sample yaml file... I am a new-bie, kindly please helpHuonghupeh
Take a look at this: kubernetes.io/docs/tasks/configure-pod-container/… You can use configmaps to set your environment variables for pods, and build your images to use those environment variables,Spall
Thanks @Burak for the help much appreciated!!! created the configmap and passed the env variables as below command: [ "/bin/bash", "-c", "$(SPECIAL_LEVEL_KEY)" ] env: - name: SPECIAL_LEVEL_KEY valueFrom: configMapKeyRef: name: javaconfig key: JAVA_OPTS Now I am able to call and start the java services. ThanksHuonghupeh
L
3

If you are using JAVA version 9+ be aware that name of env variable JAVA_OPTS changed.

More in this thread https://mcmap.net/q/57224/-what-is-the-difference-between-jdk_java_options-and-java_tool_options-when-using-java-11

Lynd answered 5/8, 2021 at 14:25 Comment(0)
H
2

You could also include it in your kubernetes deployment yaml file alongside the rest of your environment variables that you wish to pass into your docker container:

- name: JAVA_OPTS
  value: >-
        -Djava.security.egd=file:/dev/./urandom 
        -Djsversion=1.0 -D<variable>=<service-1> -D<variable>=<service-2>
- name: SERVER_PORT
  value: 8080
Hilversum answered 9/5, 2020 at 10:9 Comment(0)
B
0

You can set them as env variables in your pod's config

env:
      - name: MY_POD_IP
        valueFrom:
          fieldRef:
            fieldPath: status.podIP
      - name: profile
        value: "dev"

then use them in Dockerfile's like so

CMD ["sh", "-c", "java -jar -Dspring.profiles.active=$profile rest.war"]
Bubal answered 13/12, 2019 at 16:52 Comment(1)
Thanks for the comments... I don't want to place CMD/Entrypoint in Dockerfile... and I am looking to pass with kubernetes deployment dynamically so that I no need to build the docker images for any changes in JAVA_OPTS parametersHuonghupeh

© 2022 - 2024 — McMap. All rights reserved.