ConfigMap 对象能够用来管理普通的、非机密的配置信息,以明文形式存放。html
Secret 对象用来管理重要的、机密的、不能泄露的相似秘钥、密码等信息。linux
ConfigMap 对象能够实现程序的配置和程序自己的解耦,从而使程序更具移植性。api
从目录建立微信
mkdir configmap wget https://kubernetes.io/examples/configmap/game.properties -O configmap/game.properties wget https://kubernetes.io/examples/configmap/ui.properties -O configmap/ui.properties kubectl create configmap game-config --from-file=configmap/
能够看到两个文件内容合并存储到 data 中,文件名转换为 keyui
# kubectl get configmap game-config -o yaml apiVersion: v1 data: game.properties: |- enemies=aliens lives=3 enemies.cheat=true enemies.cheat.level=noGoodRotten secret.code.passphrase=UUDDLRLRBABAS secret.code.allowed=true secret.code.lives=30 ui.properties: | color.good=purple color.bad=yellow allow.textmode=true how.nice.to.look=fairlyNice kind: ConfigMap 。。。。。。
经过文件建立spa
kubectl create configmap game-config-2 --from-file=configmap/game.properties
经过多个文件建立命令行
效果和经过目录建立同样rest
kubectl create configmap game-config-2 --from-file=configmap/game.properties --from-file=configmap/ui.properties
从环境文件建立 ConfigMap日志
使用 --from-env-file 选项code
# 将样本文件下载到 `configmap/` 目录 wget https://kubernetes.io/examples/configmap/game-env-file.properties -O configmap/game-env-file.properties # kubectl create configmap game-config-env-file --from-env-file=configmap/game-env-file.properties configmap/game-config-env-file created
查看
# kubectl get configmap game-config-env-file -o yaml apiVersion: v1 data: allowed: '"true"' enemies: aliens lives: "3" kind: ConfigMap
能够看到文件内容直接存储到configmap data中做为键值对,没有使用文件名的key 对应多行值的形式
env文件特色:
定义从文件建立 ConfigMap 时要使用的键
默认文件名是键名
指定键名为 <my-key-name>
kubectl create configmap game-config-3 --from-file=<my-key-name>=<path-to-file>
命令行中指定key/value值
不从文件获取,直接命令行中指定
kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
使用卷挂载的方式来使用 ConfigMap
建立 ConfigMap,名为 special-config
apiVersion: v1 kind: ConfigMap metadata: name: special-config namespace: default data: SPECIAL_LEVEL: very SPECIAL_TYPE: charm
建立 Pod
apiVersion: v1 kind: Pod metadata: name: dapi-test-pod spec: containers: - name: test-container image: busybox command: [ "/bin/sh", "-c", "ls /etc/config/" ] volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: name: special-config restartPolicy: Never
在 Pod 配置文件中,建立一个卷,名为 config-volume,该卷挂载了名为 special-config 的 configMap
在容器中,将名为 config-volume 的卷挂载到系统路径 /etc/config 中。
查看结果
该容器执行了命令 ls /etc/config/,咱们能够看下容器日志
# kubectl logs dapi-test-pod SPECIAL_LEVEL SPECIAL_TYPE
能够看到 /etc/config 目录下出现了以 ConfigMap key 为名称的文件,内容为ConfigMap key对应的value
微信公众号:zuolinux_com