How to use values from a ConfigMap with key/value separated by equal symbol (=)?

4/3/2019

Given I have created a ConfigMap with a file like that :

VARIABLE1=foo
VARIABLE2=bar

Is there a way to access those values in Kubernetes or does it have to be in the YAML format?

-- ZedTuX
configmap
kubectl
kubernetes

1 Answer

4/3/2019

Let's say you have a file called z with the contents above. You have two options to make that into a ConfigMap.

Option 1 (--from-file)

$ kubectl create cm cm1 --from-file=z

This will result in an object like this:

apiVersion: v1
kind: ConfigMap
metadata:
  name: cm1
data:
  z: |
    VARIABLE1=foo
    VARIABLE2=bar

There is no direct way to project a single value from this ConfigMap as it contains just one blob. However you can, from a shell used in command of a container source that blob (if you project it as a file) and then use the resulting environment variables.

Option 2 (--from-env-file)

$ kubectl create cm cm2 --from-env-file=z

This will result in an object like this:

apiVersion: v1
kind: ConfigMap
metadata:
  name: cm2
data:
  VARIABLE1: foo
  VARIABLE2: bar

As you can see the different variables became separate key-value pairs in this case.

There are many more examples in the reference documentation

-- Janos Lenart
Source: StackOverflow