Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kubernetes: How to create and use configmap from multiple files

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?

like image 891
Vikas Rathore Avatar asked Feb 12 '19 11:02

Vikas Rathore


People also ask

How do I create a ConfigMap from Yaml file in Kubernetes?

The simplest way to create a ConfigMap is to store a bunch of key-value strings in a ConfigMap YAML file and inject them as environment variables into your Pods. After that, you can reference the environment variables in your applications using whatever methods is necessary for your programming language.

What command can create a ConfigMap object called app config with the contents of the file app config Yaml?

Mapping keys from ConfigMaps to pods There, we will define the ConfigMaps as YAML files and then use the kubectl create command to generate the ConfigMaps. In this configuration, we are mapping environmental variables to values within each ConfigMap.


3 Answers

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.

like image 156
Nahshon paz Avatar answered Nov 06 '22 03:11

Nahshon paz


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.

like image 27
Ivan Aracki Avatar answered Nov 06 '22 01:11

Ivan Aracki


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
like image 36
Alok Adhao Avatar answered Nov 06 '22 01:11

Alok Adhao