4

I have an application in a container which reads a YAML file which contains data like

              initializationCount=0
              port=980

Now that I want to remove those hard coded values inside the application and get them out of the container. Hence I created a configMap with all configuration values. I used the config map keys as environmental variables while deploying the pod.

My issue is that, If I want to use these environment variables in my yaml file like

            initializationCount=${iCount}
            port=${port}

The API which reads this YAML file throws number format Exception since the env variables are always strings. I do not have control over the API which reads my yaml file.

I have tried

            initializationCount=!!int ${iCount} 

but it does not work.

3
  • I don't think ConfigMap's work with integers. From k8 doc - "ConfigMap is similar to Secrets, but provides a means of working with strings that don’t contain sensitive information.". However you may be able to use helm to template your yml file and use a values file to substitute the initializationCount and port values as integers. Commented Oct 31, 2019 at 20:18
  • Saw the answer posted. This is closer to your use case. I was suggesting the same but to use helm to do it. Commented Oct 31, 2019 at 20:19
  • Can you show the actual YAML file you're using? That key=value syntax isn't YAML. Commented Nov 1, 2019 at 11:01

1 Answer 1

4

Rather than pulling in the configmap values as environment variables, try mounting the configmap as a volume at runtime.

The configmap should have one key which is the name of your YAML file. the value for that key should be the contents of the file.

This data will be mounted to the container's filesystem when the pod initializes. That way your app will read the config YAML the same way it has been, but the values will be externalized in the configmap.

Something like this:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: my-app
      image: my-app:latest
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: app-config
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  config.yaml: |
    initializationCount=0
    port=980

Kubernetes docs here

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer. what does "the values will be externalized in the configmap" exactly mean. I understand we are mounting a file into container during creating a container. Is it or anything more?
It just means that the config for the running environment is not in the source code. Runtime configuration is external to the codebase.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.