作用:与secret类似,但用于存储不加密的数据到etcd中,让pod以变量或者Volume挂载到容器使用。
使用场景:配置文件
完整k8s教程可参考:https://www.zfcdn.xyz/showinfo-3-36255-0.html
演示:
创建一个redis配置文件,文件名为:redis.properties 写入如下配置规则。
执行如下命令创建configmap:redis.host=127.0.0.1
redis.port=6379
redis.password=123456absaa
执行后结果:kubectl create configmap redis-config --from-file=redis.properties
查看创建的configmap[root@master ~]# kubectl create configmap redis-config --from-file=redis.properties
configmap/redis-config created
查看redis-config里面的内容:[root@master ~]# kubectl get cm
NAME DATA AGE
kube-root-ca.crt 1 31d
redis-config 1 35s
命令:
执行结果:kubectl describe cm redis-config
1、以Volume形式挂载Pod容器中[root@master ~]# kubectl describe cm redis-config
Name: redis-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
redis.properties:
----
redis.host=127.0.0.1
redis.port=6379
redis.password=123456absaa
BinaryData
====
Events: <none>
创建 cm.yaml 文件并写入如下规则:
执行如下命令生成podapiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: busybox
image: busybox
command: ["/bin/sh","-c","cat /etc/config/redis.properties"]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: redis-config
restartPolicy: Never
执行结果:kubectl apply -f cm.yaml
执行如下命令查看pod文件内容。[root@master ~]# kubectl apply -f cm.yaml
pod/mypod created
执行结果:kubectl logs mypod
2、以变量形式挂载到Pod容器中[root@master ~]# kubectl logs mypod
redis.host=127.0.0.1
redis.port=6379
redis.password=123456absaa
创建 myconfig.yaml 文件并写入如下规则:
执行如下命令生成configmapapiVersion: v1
kind: ConfigMap
metadata:
name: myconfig
namespace: default
data:
special.level: info
special.type: hello
查看configmapkubectl apply -f myconfig.yaml
创建 config-var.yaml 文件,写入如下规则:kubectl get cm
执行如下命令创建podapiVersion: 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: myconfig
key: special.level
- name: TYPE
valueFrom:
configMapKeyRef:
name: myconfig
key: special.type
restartPolicy: Never
查看生成的podkubectl apply -f config-var.yaml
查看pod里面的变量内容kubectl get pods
执行结果:kubectl logs mypod
[root@master ~]# kubectl logs mypod
info hello
文章评论 本文章有个评论