<p>I have a kubernetes pod which is based on spring boot. Locally I can start jar file with external file with this commands:</p>
<pre><code>java -jar app.jar --spring.config.location=file:///Users/home/config/application.yml
</code></pre>
<p>But how to do it in kubernetes? I use Helm charts for configuration. Is there some solution with helm charts configuration?</p>
<p>I tried this:</p>
<p>default <code>application.yml</code></p>
<pre><code>init:
  message: &quot;Test message loaded from default application.yml file&quot;
spring:
  config:
    location: ${CONFIG_LOCATION}
</code></pre>
<p><code>application-mockup-configuration.yml</code> insight helm chart:</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: application-mockup-configuration
data:
  application-mockup-configuration.yml: |
    init:
      message: &quot;Test message loaded from default external application-mockup-configuration.yml file !!!!!!!!!!!&quot;
    spring:
      application:
        name: mockup-test
      jackson:
        default-property-inclusion: non_null
      jmx:
        enabled: false
</code></pre>
<p>deoloyment.yaml from helm chart:</p>
<pre><code>  envFrom:
  - secretRef:
      name: mockup-secret-env
  - configMapRef:
      name: application-mockup-configuration-configmap-env
  env:
    - name: CONFIG_LOCATION
      value: &quot;/home/config/application-mockup-configuration.yml&quot;
  volumeMounts:
    - name: application-mockup-configuration
      mountPath: /home/config/application-mockup-configuration.yml
      subPath: application-mockup-configuration.yml
</code></pre>
<p>But I the issue is that I always get the default message from the default <code>application.yml</code> file insight the test spring boot app. External file is not overriding the value for <code>init: message.....</code> Do you know how I can solve this issue?</p>

Peter Penzov
<blockquote>
<p>&quot;Locally, I can start jar file with external file with that command: <code>java -jar app.jar --spring.config.location=file:///Users/home/config/application.yml</code>.  But how to do it in kubernetes?&quot;.</p>
</blockquote>
<p>In a Kubernetes environment..., you would <em>not</em> directly execute the <code>java -jar</code> command as you do on your local machine.<br />
Instead, this command would be defined in the Dockerfile for your application's Docker image, or in the container spec in your <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/" rel="nofollow noreferrer"><code>Deployment</code></a>.</p>
<p>The command to start your application would typically look like this:</p>
<pre><code>CMD [&quot;java&quot;, &quot;-jar&quot;, &quot;app.jar&quot;]
</code></pre>
<p>Then in your Kubernetes <code>Deployment</code>, you would add an additional argument <code>--spring.config.location=file:/home/config/application.yml</code>:</p>
<pre class="lang-yaml prettyprint-override"><code>spec:
  containers:
  - name: my-app
    image: your-image-repo/your-image-name:tag
    args: [&quot;--spring.config.location=file:/home/config/application.yml&quot;]
    volumeMounts:
      - name: config-volume
        mountPath: /home/config
  volumes:
    - name: config-volume
      configMap:
        name: {{ .Release.Name }}-configmap
        items:
        - key: application.yml
          path: application.yml
</code></pre>
<p>In that configuration, the <code>args</code> field adds the <code>--spring.config.location=file:/home/config/application.yml</code> argument to the command that starts your application. When your application starts up, it reads its configuration from the <code>application.yml</code> file that was loaded from the <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#add-configmap-data-to-a-volume" rel="nofollow noreferrer"><code>ConfigMap</code></a>.</p>
<p>Once you have applied the <a href="https://helm.sh/docs/chart_template_guide/getting_started/" rel="nofollow noreferrer">Helm chart</a> with <code>helm install</code> or <code>helm upgrade</code>, Kubernetes will take care of starting your application with the specified command and arguments.</p>
<p>When the pod starts, the Spring Boot application will run with the provided argument, pointing the configuration location to the file mounted from the <code>ConfigMap</code>.</p>
<hr />
<p>So, in details, in your <code>configmap.yaml</code>, define the configuration you want to provide to your application:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  application.yml: |
    init:
      message: &quot;Test message loaded from external application.yml file&quot;
    spring:
      application:
        name: mockup-test
      jackson:
        default-property-inclusion: non_null
      jmx:
        enabled: false
</code></pre>
<p>Then, in your <code>deployment.yaml</code>, define a Deployment object that mounts the ConfigMap as a volume, and add the <code>--spring.config.location</code> argument to the container spec:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ .Release.Name }}
    spec:
      containers:
      - name: my-app
        image: your-image-repo/your-image-name:tag
        command: [&quot;java&quot;, &quot;-jar&quot;, &quot;app.jar&quot;]
        args: [&quot;--spring.config.location=file:/home/config/application.yml&quot;]
        volumeMounts:
        - name: config-volume
          mountPath: /home/config
      volumes:
      - name: config-volume
        configMap:
          name: {{ .Release.Name }}-configmap
          items:
          - key: application.yml
            path: application.yml
</code></pre>
<p>That setup assumes that the <code>app.jar</code> file is located at the root of your Docker image. If it is in a different location, you would need to update the <code>command</code> accordingly. Similarly, replace <code>your-image-repo/your-image-name:tag</code> with the path to your actual Docker image.</p>
<p>When you deploy this Helm chart with a command like <code>helm install my-release ./my-chart</code>, Helm will create a Kubernetes Deployment and ConfigMap according to these templates.<br />
The <code>Deployment</code> will start your Spring Boot application with the specified command and arguments, which will tell the application to load its configuration from the external <code>application.yml</code> file provided by the <code>ConfigMap</code>.</p>
