Kubernetes 如何将从.txt创建的configmap添加到pod?
我正在尝试从config.txt文件创建一个简单的配置映射:Kubernetes 如何将从.txt创建的configmap添加到pod?,kubernetes,kubernetes-pod,configmap,Kubernetes,Kubernetes Pod,Configmap,我正在尝试从config.txt文件创建一个简单的配置映射: config.txt: ---------- key1=val1 key2=val2 这是pod yaml: apiVersion: v1 kind: Pod metadata: labels: run: nginx name: nginx spec: containers: - image: nginx name: nginx command: [ "/bin/sh",
config.txt:
----------
key1=val1
key2=val2
这是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
通过运行
kubectl create configmap keyvalcfgmap--from file=-o yaml>configmap.yaml
并应用创建的configmap,我可以在pod中使用它。问题是怎么做?我尝试将它添加为一个卷,或者使用--from file=
甚至envFrom
调用它,但我能得到的最好结果是,该卷只是装载了文件本身,而不是configmap。您可以像这样使用envFrom
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
第一个选项是装载文件本身。第二个选项给了我以下错误:
错误:验证“nginx pod.yaml”时出错:验证数据时出错:[ValidationError(pod.spec.containers[0].env[0].valueFrom.configMapKeyRef):io.k8s.api.core.v1.ConfigMapKeySelector中的未知字段“key1”,ValidationError(pod.spec.containers[0].env[0].valueFrom.configMapKeyRef):未知字段io.k8s.api.core.v1.ConfigMapKeySelector中的“key2”,ValidationError(Pod.spec.containers[0].env[0].valueFrom.configMapKeyRef):io.k8s.api.core.v1.ConfigMapKeySelector中缺少必需字段“key”
env:
- name: NAME
valueFrom:
configMapKeyRef:
name: keyvalcfgmap #<--------------Here
key: key1
- name: NAME
valueFrom:
configMapKeyRef:
name: keyvalcfgmap #<--------------Here
key: key2