10

I have the documentation regarding the configmap:

https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#define-container-environment-variables-using-configmap-data

From what I understand is I can create a config map(game-config-2) from two files (game.properties and ui.properties) using

kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/kubectl/game.properties --from-file=configure-pod-container/configmap/kubectl/ui.properties

Now I see the configmap

kubectl describe configmaps game-config-2
Name:           game-config-2
Namespace:      default
Labels:         <none>
Annotations:    <none>

Data
====
game.properties:        158 bytes
ui.properties:          83 bytes

How can I use that configmap? I tried this way:

    envFrom:
    - configMapRef:
        name: game-config-2

But this is not working, the env variable is not picking from the configmap. Or can I have two configMapRef under envFrom?

1
  • does your expected environment variables are in those files as key-value pair? If so then you should use --from-env-file instead of --from-file. Commented Feb 12, 2019 at 12:38

6 Answers 6

13

Yes, a pod or deployment can get env From a bunch of configMapRef entries:

    spec:
      containers:
      - name: encouragement-api
        image: registry-......../....../encouragement.api
        ports:
        - containerPort: 80
        envFrom:
          - configMapRef:
              name: general-config
          - configMapRef:
              name: private-config

Best to create them from yaml files for k8s law and order:

config_general.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: general-config
data:
  HOSTNAME: Develop_hostname
  COMPUTERNAME: Develop_compname
  ASPNETCORE_ENVIRONMENT: Development

encouragement-api/config_private.yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: private-config
data:
  PRIVATE_STUFF: real_private

apply the two configmaps:

kubectl apply -f config_general.yaml
kubectl apply -f encouragement-api/config_private.yaml

Run and exec into the pod and run env |grep PRIVATE && env |grep HOSTNAME

I have config_general.yaml laying around in the same repo as the developers' code, they can change it however they like. Passwords and sensitive values are kept in the config_private.yaml file which is sitting elsewhere (a S3 encrypted bucket) and the values there are base64 encoded for an extra bit of security.

3
  • 2
    Just a small note - for passwords and sensitive data it's better to use k8s Secret instead of ConfigMap. They are specifically designed for that case
    – Ruslan Bes
    Commented Jul 30, 2021 at 16:22
  • 1
    agree with vdd + base64 encode is NOT a security at all, it's just encoded : everybody can read it with "online base64 decode" or even with a command line like "base64 -d <<< aUFtR3Jvb3QK"
    – Gremi64
    Commented Jun 9, 2023 at 14:48
  • indeed, though secrets are hardly better unless they're encrypted Commented Jun 12, 2023 at 8:22
6

One solution to this problem is to create a ConfigMap with a multiple data key/values:

apiVersion: v1
kind: ConfigMap
metadata:
  name: conf
data:
  game.properties: |
    <paste file content here>
  ui.properties: |
    <paste file content here>

Just don't forget | symbol before pasting content of files.

5
  • I have created this config map but when I am using it in the pod yaml. It is not picking up the env values. I have highlighted the same in the question. Commented Feb 12, 2019 at 19:40
  • in pod.yaml you have envFrom: - configMapRef: name: conf? Commented Feb 12, 2019 at 20:22
  • can you post output of kubectl describe pod <your_pod_name>? Commented Feb 12, 2019 at 20:35
  • Instead of pasting the file content, can we refer to files? Commented Oct 7, 2021 at 18:48
  • Maybe to check this one: stackoverflow.com/questions/49478036/… Commented Oct 8, 2021 at 16:27
2

Multiple --from-env-file are not allowed. Multiple --from-file will work for you.

Eg:

cat config1.txt 
    var1=val1

cat config2.txt 
    var3=val3
    var4=val4

kubectl create cm details2 --from-env-file=config1.txt --from-env-file=config2.txt -o yaml --dry-run

Output

apiVersion: v1
data:
  var3: val3
  var4: val4
kind: ConfigMap
  name: details2

k create cm details2 --from-file=config1.txt --from-file=config2.txt -o yaml --dry-run 

Output

apiVersion: v1
data:
  config1.txt: |
    var1=val1
  config2.txt: |
    var3=val3
    var4=val4
kind: ConfigMap
  name: details2
2

If you use Helm, it is much simpler. Create a ConfigMap template like this

kind: ConfigMap
apiVersion: v1
metadata:
  name: {{ .Values.configMapName }}
data:
  {{ .Values.gameProperties.file.name }}: |
  {{ tpl (.Files.Get .Values.gameProperties.file.path) }}
  {{ .Values.uiProperties.file.name }}: |
  {{ tpl (.Files.Get .Values.uiProperties.file.path) }}

and two files with the key:value pairs like this game.properties

GAME_NAME: NFS

and another files ui.properties

GAME_UI: NFS UI

and values.yaml should like this

configMapName: game-config-2
gameProperties:
  file:
    name: game.properties
    path: "properties/game.properties"
uiProperties:
  file:
    name: ui.properties
    path: "properties/ui.properties"

You can verify if templates interpolate the values from values.yaml file by helm template ., you can expect this as output

kind: ConfigMap
apiVersion: v1
metadata:
  name: game-config-2
data:
  game.properties: |
    GAME_NAME: NFS
  ui.properties: |
    GAME_UI: NFS UI
0

am not sure if you can load all key:value pairs from a specific file in a configmap as environemnt variables in a pod. you can load all key:value pairs from a specific configmap as environemnt variables in a pod. see below

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  SPECIAL_LEVEL: very
  SPECIAL_TYPE: charm
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: gcr.io/google_containers/busybox
      command: [ "/bin/sh", "-c", "env" ]
      envFrom:
      - configMapRef:
          name: special-config
  restartPolicy: Never

Verify that pod shows below env variables

SPECIAL_LEVEL=very
SPECIAL_TYPE=charm
-2

As @Emruz_Hossain mentioned , if game.properties and ui.properties have only env variables then this can work for you

kubectl create configmap game-config-2 --from-env-file=configure-pod-container/configmap/kubectl/game.properties --from-env-file=configure-pod-container/configmap/kubectl/ui.properties
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.