一、ConfigMap概述
1)ConfigMap 是一种 API 对象,用来将非机密性的数据保存到键值对中。使用时, Pods 可以将其用作环境变量、命令行参数或者存储卷中的配置文件。
2)ConfigMap 将您的环境配置信息和容器镜像解耦,便于应用配置的修改,ConfigMap组件可以很好的帮助我们实现应用和配置分离,避免因为修改配置项而重新构建镜像。
3)ConfigMap跟Secret很类似,但它可以更方便地处理不包含敏感信息的字符串。
二、创建configmap
[root@master-191 ~]# vim configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: you-config
namespace: default
data:
special.level: first
special.type: happy
[root@master-191 ~]# kubectl create -f configmap.yaml
configmap "you-config" created
[root@master-191 ~]# kubectl get cm
NAME DATA AGE
you-config 2 6s
三、引用configmap文件
1)变量方式
[root@master-191 ~]# vim config-var.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: busybox
image: busybox
command: [ "/bin/sh", "-c", "echo $(LEVEL) $(TYPE)" ]
env:
- name: LEVEL
valueFrom:
configMapKeyRef:
name: you-config
key: special.level
- name: TYPE
valueFrom:
configMapKeyRef:
name: you-config
key: special.type
restartPolicy: Never
[root@master-191 ~]# kubectl create -f config-var.yaml
pod "mypod" created
[root@master-191 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
mypod 0/1 ContainerCreating 0 3s
[root@master-191 ~]# kubectl get pod
No resources found, use --show-all to see completed objects.
[root@master-191 ~]# kubectl logs -f mypod
first happy
2)volume挂载方式
ConfigMap 支持同一个目录下挂载多个 key 和多个目录。例如下面将 special.level 和 special.type 挂载到 / etc/config 下。并且将 special.how 同时挂载到 / etc/config2 下。
[root@master-191 ~]# vim config-vol.yaml
apiVersion: v1
kind: Pod
metadata:
name: qing-pod
spec:
containers:
- name: test-container
image: 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: you-config
items:
- key: special.level
path: keys/special.level
- key: special.type
path: keys/special.type
- name: config-volume2
configMap:
name: you-config
items:
- key: special.level
path: keys/special.level
restartPolicy: Never
[root@master-191 ~]# kubectl create -f config-vol.yaml
pod "qing-pod" created
[root@master-191 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
qing-pod 1/1 Running 0 4s
[root@master-191 ~]# kubectl exec -it qing-pod sh
/ # ls /etc/
config config2 group hostname hosts localtime mtab network passwd resolv.conf shadow
/ # ls /etc/config
keys
/ # ls /etc/config2
keys
/ # ls /etc/config/keys/
special.level special.type
/ # ls /etc/config2/keys/
special.level
/ # cat /etc/config2/keys/special.level
first/ #
kubenetes官网:https://kubernetes.io/zh/docs
kubenetes社区:https://www.kubernetes.org.cn
kuboard.cn官网:https://kuboard.cn/learning