Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
647 views
in Technique[技术] by (71.8m points)

kubernetes - Populate ConfigMap by importing data from file in k8s

I have a requirement where i push bunch of key value pairs to a text/json file. Post that, i want to import the key value data into a configMap and consume this configMap within a POD using kubernetes-client API's.

Any pointers on how to get this done would be great.

TIA

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do it in two ways.

Create ConfigMap from file as is.

In this case you will get ConfigMap with filename as a key and filedata as a value.

For example, you have file your-file.json with content {key1: value1, key2: value2, keyN: valueN}.

And your-file.txt with content

key1: value1 key2: value2 keyN: valueN

kubectl create configmap name-of-your-configmap --from-file=your-file.json
kubectl create configmap name-of-your-configmap-2 --from-file=your-file.txt

As result:

apiVersion: v1
kind: ConfigMap
metadata:
  name: name-of-your-configmap
data:
  your-file.json: |
    {key1: value1, key2: value2, keyN: valueN}

apiVersion: v1
kind: ConfigMap
metadata:
  name: name-of-your-configmap-2
data:
  your-file.txt: |
    key1: value1
    key2: value2
    keyN: valueN

After this you can mount any of ConfigMaps to a Pod, for example let's mount your-file.json:

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh","-c","cat /etc/config/keys" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: name-of-your-configmap
        items:
        - key: your-file.json
          path: keys
restartPolicy: Never

Now you can get any information from your /etc/config/your-file.json inside the Pod. Remember that data is read-only.

Create ConfigMap from file with environment variables.

You can use special syntax to define pairs of key: value in file. These syntax rules apply:

  • Each line in a file has to be in VAR=VAL format.
  • Lines beginning with # (i.e. comments) are ignored.
  • Blank lines are ignored.
  • There is no special handling of quotation marks (i.e. they will be part of the ConfigMap value)).

You have file your-env-file.txt with content

key1=value1 key2=value2 keyN=valueN

kubectl create configmap name-of-your-configmap-3 --from-env-file=you-env-file.txt

As result:

apiVersion: v1
kind: ConfigMap
metadata:
  name: name-of-your-configmap-3
data:
  key1: value1
  key2: value2
  keyN: valueN

Now you can use ConfigMap data as Pod environment variables:

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod-2
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "env" ]
      env:
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: name-of-your-configmap-3
              key: key1
        - name: LOG_LEVEL
          valueFrom:
            configMapKeyRef:
              name: name-of-your-configmap-3
              key: key2
        - name: SOME_VAR
          valueFrom:
            configMapKeyRef:
              name: name-of-your-configmap-3
              key: keyN
  restartPolicy: Never

Now you can use these variables inside the Pod.

For more information check for documentation


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...