通过从k8s中的文件导入数据来填充ConfigMap [英] Populate ConfigMap by importing data from file in k8s

查看:716
本文介绍了通过从k8s中的文件导入数据来填充ConfigMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,我要将一堆键值对推到text/json文件中.发布后,我想将键值数据导入configMap并使用kubernetes-client API在POD中使用此configMap.

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

推荐答案

您可以通过两种方式来实现.

You can do it in two ways.

在这种情况下,您将获得ConfigMap,其文件名为键,文件数据为值.

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

例如,您具有内容为{key1: value1, key2: value2, keyN: valueN}的文件your-file.json.

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

your-file.txt内容

key1: value1 key2: value2 keyN: valueN

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

结果:

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

此后,您可以将任何ConfigMap挂载到Pod,例如,让我们挂载your-file.json:

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

现在,您可以从Pod内的/etc/config/your-file.json中获取任何信息.请记住,数据是只读的.

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

您可以使用特殊语法在文件中定义成对的key: value. 这些语法规则适用:

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

  • 文件中的每一行都必须为VAR = VAL格式.
  • 以#(即注释)开头的行将被忽略.
  • 空白行将被忽略.
  • 对引号没有特殊处理(即它们将成为ConfigMap值的一部分).

您有包含内容的文件your-env-file.txt

key1=value1 key2=value2 keyN=valueN

key1=value1 key2=value2 keyN=valueN

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

结果:

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

现在您可以将ConfigMap数据用作Pod环境变量:

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

现在您可以在Pod中使用这些变量.

Now you can use these variables inside the Pod.

有关更多信息,请查看文档

For more information check for documentation

这篇关于通过从k8s中的文件导入数据来填充ConfigMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆