Killerz0ne
Killerz0ne

Reputation: 344

how to add configmap created from a .txt to a pod?

I am trying to make a simple config map from a config.txt file:

config.txt:
----------
key1=val1
key2=val2

this is the pod yaml:

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: nginx
  name: nginx
spec:
  containers:
  - image: nginx
    name: nginx
    command: [ "/bin/sh", "-c", "env" ]
    env:
      - name: KEY_VALUES
        valueFrom:
          configMapKeyRef:
            name: keyvalcfgmap
            key1: key1
            key2: key2

by running kubectl create configmap keyvalcfgmap --from-file=<filepath> -o yaml > configmap.yaml and applying the created configmap, I supposedly can use it in a pod. the question is how? I tried adding it as a volume or calling it using --from-file= and even envFrom but the best I could get was that the volume just mounted the file itself and not the configmap.

Upvotes: 0

Views: 1003

Answers (1)

Dashrath Mundkar
Dashrath Mundkar

Reputation: 9174

You can use envFrom like this

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
  - name: test-container
    image: k8s.gcr.io/busybox
    command: [ "/bin/sh", "-c", "env" ]
    envFrom:
    - configMapRef:
        name: keyvalcfgmap        #<--------------Here
  restartPolicy: Never

or you can use configmap as env variables

env:
  - name: NAME
    valueFrom:
      configMapKeyRef:
        name: keyvalcfgmap        #<--------------Here
        key: key1
  - name: NAME
    valueFrom:
      configMapKeyRef:
        name: keyvalcfgmap       #<--------------Here
        key: key2

Upvotes: 1

Related Questions