创建ConfigMap后,数据实际会存储在K8s中Etcd,然后通过创建Pod时引用该数据
应用场景:应用程序配置
Pod使用configmap数据有两种方式
- 变量注入
- 数据卷挂载
例子:
Step1:创建ConfigMap[configMap.yaml]和Pod[configMap-pod.yaml]
shell>vim configMap.yaml
#####内容如下:#########
apiVersion: v1
kind: ConfigMap
metadata:
name: configmap-demo
data:
abc: "123"
cde: "456"
redis.properties: |
port: 6379
host: 192.168.31.18
shell>vim configMap-pod.yaml
########内容如下############
apiVersion: v1
kind: Pod
metadata:
name: configmap-demo-pod
spec:
containers:
- name: demo
image: nginx
env:
- name: ABCD
valueFrom:
configMapKeyRef:
name: configmap-demo
key: abc
- name: CDEF
valueFrom:
configMapKeyRef:
name: configmap-demo
key: cde
volumeMounts:
- name: config
mountPath: "/config"
readOnly: true
volumes:
- name: config
configMap:
name: configmap-demo
items:
- key: "redis.properties"
path: "redis.properties"
shell>kubectl apply -f configMap-pod.yaml
Step2:查看设置的configmap
shell>kubectl get configmap -n default
step3:可以进去pod里面查看变量
[root@master ~]# shell>kubectl get pod
NAME READY STATUS RESTARTS AGE
configmap-demo-pod 1/1 Running 0 3m25s
hello-deploy-fd94d7fff-nvspn 1/1 Running 2 2d4h
############进入对应的pod中########################
[root@master ~]# shell>kubectl exec -it configmap-demo-pod -- bash
###输出变量
shell>echo $ABCD
shell>cat /config/redis.properties
#################################################################
# root@configmap-demo-pod:/# cat /config/redis.properties
# port: 6379
# host: 192.168.31.10
#################################################################
shell>env