Passing java_opts to spring boot applications in kubernetes

12/13/2019

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
-- magic
docker
kubernetes
kubernetes-deployment
kubernetes-pod
spring-boot

3 Answers

12/13/2019

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"]
-- Dylan
Source: StackOverflow

5/9/2020

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
-- pokkie
Source: StackOverflow

12/13/2019

You can use command in k8s deployment manifest:

containers:
- name: mycontainer
  env:
  - name: NAME
    value: VALUE
  command: [ "java"]
  args: ["-jar", "-D..."]
-- Burak Serdar
Source: StackOverflow