通过从 k8s 中的文件导入数据来填充 ConfigMap [英] Populate ConfigMap by importing data from file in k8s
问题描述
我需要将一堆键值对推送到文本/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.
例如,您的文件 your-file.json
的内容为 {key1: value1, key2: value2, keyN: valueN}
.
For example, you have file your-file.json
with content {key1: value1, key2: value2, keyN: valueN}
.
和带有内容的 your-file.txt
<代码>键 1:值 1键 2:值 2keyN: 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键2=值2键N=值N
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屋!