<p>I'm configuring app to works in Kubernetes google cloud cluster. I'd like to pass parameters to <code>application.properties</code> in Spring boot application via <code>configMap</code>. I'm trying to pass value by <strong>Environment Variable</strong>.</p>
<p>I've created config map in google cloud Kubernetes cluster in namespace <code>default</code> like below:</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata: 
  name: app-config
data:
  config-key: 12345789abde 

kubectl create -f app-config.yaml -n default
configmap/app-config created
</code></pre>
<p>I'm checking if configMap has been created:</p>
<p><a href="https://i.stack.imgur.com/qYEoJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qYEoJ.png" alt="enter image description here" /></a></p>
<p>key value pair looks fine:</p>
<p><a href="https://i.stack.imgur.com/gPgBK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gPgBK.png" alt="enter image description here" /></a></p>
<p>I'm trying to deploy Spring boot app with using <code>cloudbuild.yaml</code>(it works when I not use configMap). The content is:</p>
<pre><code>substitutions:
  _CLOUDSDK_COMPUTE_ZONE: us-central1-c  # default value
  _CLOUDSDK_CONTAINER_CLUSTER: kubernetes-cluster-test      # default value

steps:
  - id: 'Build docker image'
    name: 'gcr.io/cloud-builders/docker'
    args: ['build', '-t', 'gcr.io/${_TECH_RADAR_PROJECT_ID}/${_TECH_CONTAINER_IMAGE}:$SHORT_SHA', '.']
  - id: 'Push image to Container Registry'
    name: 'gcr.io/cloud-builders/docker'
    args: ['push', 'gcr.io/${_TECH_RADAR_PROJECT_ID}/${_TECH_CONTAINER_IMAGE}:$SHORT_SHA']
  - id: 'Set image in yamls'
    name: 'ubuntu'
    args: ['bash','-c','sed -i &quot;s,${_TECH_CONTAINER_IMAGE},gcr.io/${_TECH_RADAR_PROJECT_ID}/${_TECH_CONTAINER_IMAGE}:$SHORT_SHA,&quot; deployment.yaml']
  - id: 'Create or update cluster based on last docker image'
    name: 'gcr.io/cloud-builders/kubectl'
    args: ['apply', '-f', 'deployment.yaml']
    env:
      - 'CLOUDSDK_COMPUTE_ZONE=${_CLOUDSDK_COMPUTE_ZONE}'
      - 'CLOUDSDK_CONTAINER_CLUSTER=${_CLOUDSDK_CONTAINER_CLUSTER}'
  - id: 'Expose service to outside world via load balancer'
    name: 'gcr.io/cloud-builders/kubectl'
    args: [ 'apply', '-f', 'service-load-balancer.yaml' ]
    env:
      - 'CLOUDSDK_COMPUTE_ZONE=${_CLOUDSDK_COMPUTE_ZONE}'
      - 'CLOUDSDK_CONTAINER_CLUSTER=${_CLOUDSDK_CONTAINER_CLUSTER}'
options:
  logging: CLOUD_LOGGING_ONLY
</code></pre>
<p><code>deployment.yaml</code> with reference to config map (container is also in default namespace) is:</p>
<pre><code>apiVersion: &quot;apps/v1&quot;
kind: &quot;Deployment&quot;
metadata:
  name: &quot;java-kubernetes-clusters-test&quot;
  namespace: &quot;default&quot;
  labels:
    app: &quot;java-kubernetes-clusters-test&quot;
spec:
  replicas: 3
  selector:
    matchLabels:
      app: &quot;java-kubernetes-clusters-test&quot;
  template:
    metadata:
      labels:
        app: &quot;java-kubernetes-clusters-test&quot;
    spec:
      containers:
        - name: &quot;app&quot;
          image: &quot;kubernetes-cluster-test-image&quot;
          env:
            - name: CONFIG_KEY
              valueFrom:
                configMapKeyRef:
                  name: app-config
                  key: config-key
</code></pre>
<p>Spring boot is not able to read placeholder and I'm obtaining an error in attempt to deply app to google cloud like below:</p>
<p><a href="https://i.stack.imgur.com/igoGi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/igoGi.png" alt="enter image description here" /></a></p>
<p>I'm trying to reference to env with name CONFIG_KEY in <code>application.properties</code>:</p>
<pre><code>com.example.dockerkubernetes.property.value=${CONFIG_KEY}
</code></pre>
<p>and then in Spring Boot controller:</p>
<pre><code>@RestController
@RequiredArgsConstructor
public class MyController {

    @Value(&quot;${com.example.dockerkubernetes.property.value}&quot;)
    private String testValue;

    public String getConfigMapTestKey() {
        return this.testValue;
    }
}
</code></pre>
<p>Has anyone any idea why it dosen't work? Maybe some permissions are missing?
I would be grateful for help.</p>

Krzysztof Michalski
<p>You can read properties from the environment, but that could require a bit of coding &amp; refactoring (depending on the rest of your code) to import and use such values, i.e. into a <code>Properties</code> object.</p>
<p>If you want to use an <code>application.properties</code> file directly you're better off mounting the single file into the app's container by <a href="https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath" rel="nofollow noreferrer">using SubPath</a>.</p>
<p>Make sure you mount the file exactly as specified in the linked official docs or you are likely to get errors related to the container's FS permissions</p>
