ConfigMap 用于保存配置数据的键值对,可以用来保存单个属性,也可以用来保存配置文件。ConfigMap 跟 secret 很类似,但它可以更方便地处理不包含敏感信息的字符串。
可以使用 kubectl create configmap
从文件、目录或者 key-value 字符串创建等创建 ConfigMap。也可以通过 kubectl create -f file
创建。
kubectl create configmap config-test --from-literal=user=andriy --from-literal=pwd=password
echo -e "user=andriy\npwd=password" | tee cm.env
user=andriy
pwd=password
kubectl create configmap cm-env-test --from-env-file=cm.env
mkdir config
echo "andriy" > config/user
echo "password" > config/pwd
kubectl create configmap cm-dir-test --from-file=config/
cat << EOF > cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-yaml-test
data:
user: andriy
pwd: password
EOF
kubectl create -f cm.yaml
使用
kubectl get cm cm-env-test -o go-template='{{.data}}'
命令可以查看configmap的key-value值
ConfigMap 可以通过三种方式在 Pod 中使用,三种分别方式为:设置环境变量、设置容器命令行参数以及在 Volume 中直接挂载文件或目录。
[warning] 注意
- ConfigMap 必须在 Pod 引用它之前创建
- 使用 envFrom 时,将会自动忽略无效的键
- Pod 只能使用同一个命名空间内的 ConfigMap
apiVersion: v1
kind: Pod
metadata:
name: test-pod
spec:
containers:
- name: test-container
image: busybox
command: ["/bin/sh", "-c", "env"]
env:
- name: USERNAME
valueFrom:
configMapKeyRef:
name: cm-env-test
key: user
- name: PASSWORD
valueFrom:
configMapKeyRef:
name: cm-env-test
key: pwd
restartPolicy: Never
apiVersion: v1
kind: Pod
metadata:
name: test-pod
spec:
containers:
- name: test-container
image: busybox
command: ["/bin/sh", "-c", "env"]
envFrom:
- configMapRef:
name: cm-env-test
restartPolicy: Never
上述pod创建之后可以kubectl logs test-pod
查看输出信息
apiVersion: v1
kind: Pod
metadata:
name: configmap-volue-test
spec:
containers:
- name: test-container
image: gcr.io/google_containers/busybox
command: ["/bin/sh", "-c", "echo $(USERNAME) $(PASSWORD)" ]
env:
- name: USERNAME
valueFrom:
configMapKeyRef:
name: cm-env-test
key: user
- name: PASSWORD
valueFrom:
configMapKeyRef:
name: cm-env-test
key: pwd
restartPolicy: Never
Pod日志输出
kubectl logs configmap-volue-test
andriy password
apiVersion: v1
kind: Pod
metadata:
name: configmap-vol-test
spec:
containers:
- name: test-container
image: gcr.io/google_containers/busybox
command: ["/bin/sh", "-c", "cat /etc/config/*"]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: cm-env-test
restartPolicy: Never
Pod日志输出
kubectl logs configmap-vol-test
passwordandriy
apiVersion: v1
kind: Pod
metadata:
name: file-cm-test
spec:
containers:
- name: test-file-con
image: gcr.io/google_containers/busybox
command: ["/bin/sh","-c","cat /etc/config/keys/username"]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: cm-env-test
items:
- key: user
path: keys/username
restartPolicy: Never
Pod日志输出
kubectl logs file-cm-test
andriy
apiVersion: v1
kind: Pod
metadata:
name: file-test-pod
spec:
containers:
- name: test-container
image: gcr.io/google_containers/busybox
command: ["/bin/sh","-c","sleep 36000"]
volumeMounts:
- name: config-volume
mountPath: /etc/config
- name: config-volume2
mountPath: /etc/config2
volumes:
- name: config-volume
configMap:
name: cm-env-test
items:
- key: user
path: keys/username
- key: pwd
path: keys/password
- name: config-volume2
configMap:
name: cm-env-test
items:
- key: user
path: keys/username
restartPolicy: Never
在一般情况下 configmap 挂载文件时,会先覆盖掉挂载目录,然后再将 congfigmap 中的内容作为文件挂载进去。如果想不对原来的文件夹下的文件造成覆盖,只是将 configmap 中的每个 key,按照文件的方式挂载到目录下,可以使用 subpath 参数。
apiVersion: v1
kind: Pod
metadata:
name: cm-test-pod
spec:
containers:
- name: test-container
image: nginx
command: ["/bin/sh","-c","sleep 36000"]
volumeMounts:
- name: config-volume
mountPath: /etc/nginx/username
subPath: username
volumes:
- name: config-volume
configMap:
name: cm-env-test
items:
- key: user
path: username
restartPolicy: Never
Pod验证
kubectl exec -it cm-test-pod -- cat /etc/nginx/username
andriy