作用:存储不加密数据到 etcd,让 Pod 以变量或者 Volume 挂载到容器中
场景:配置文件
redis.properties
redis.host=127.0.0.1
redis.port=6379
redis.password=123456
# 根据 redis.properties 创建 redis-config
[root@k8smaster ~]# kubectl create configmap redis-config --from-file=redis.properties
# 查看 configmap
[root@k8smaster ~]# kubectl get cm
# 查看 redis-config 的详情
[root@k8smaster ~]# kubectl describe cm redis-config
cm.yaml
apiVersion: 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
# 创建 yaml文件
[root@k8smaster ~]# vi cm.yaml
# 创建pod
[root@k8smaster ~]# kubectl apply -f cm.yaml
# 启动后可以查看日志
[root@k8smaster ~]# kubectl logs mypod
创建 yaml,声明变量信息 configmap 创建
myconfig.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: myconfig
namespace: default
data:
special.level: info
special.type: hello
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: myconfig
key: special.level
- name: TYPE
valueFrom:
configMapKeyRef:
name: myconfig
key: special.type
restartPolicy: Never
[root@k8smaster ~]# vi myconfig.yaml
[root@k8smaster ~]# kubectl apply -f myconfig.yaml
# 以变量形式进行挂载
[root@k8smaster ~]# vi config-var.yaml
[root@k8smaster ~]# kubectl apply -f config-var.yaml