前言

ConfigMap 对象能够用来治理一般的、非秘密的配置信息,以明文模式寄存。

Secret 对象用来治理重要的、秘密的、不能泄露的相似秘钥、明码等信息。

ConfigMap 对象能够实现程序的配置和程序自身的解耦,从而使程序更具移植性。

更新历史

  • 20200705 - 初稿 - 左程立
  • 原文地址 - https://blog.zuolinux.com/2020/07/05/about-configmap.html

通过目录/文件创建 ConfigMap

从目录创立

mkdir configmapwget https://kubernetes.io/examples/configmap/game.properties -O configmap/game.propertieswget https://kubernetes.io/examples/configmap/ui.properties -O configmap/ui.propertieskubectl create configmap game-config --from-file=configmap/

能够看到两个文件内容合并存储到 data 中,文件名转换为 key

# kubectl get configmap game-config -o yamlapiVersion: v1data:  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=fairlyNicekind: ConfigMap。。。。。。

通过文件创建

kubectl create configmap game-config-2 --from-file=configmap/game.properties

通过多个文件创建

成果和通过目录创立一样

kubectl create configmap game-config-2 --from-file=configmap/game.properties --from-file=configmap/ui.properties

从环境文件创建 ConfigMap

应用 --from-env-file 选项

# 将样本文件下载到 `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 yamlapiVersion: v1data:  allowed: '"true"'  enemies: aliens  lives: "3"kind: ConfigMap

能够看到文件内容间接存储到configmap data中作为键值对,没有应用文件名的key 对应多行值的模式

env文件特点:

  1. env 文件中的每一行必须为 VAR = VAL 格局。
  2. 以#结尾的行(即正文)将被疏忽。
  3. 空行将被疏忽。
  4. 引号没有非凡解决(即它们将成为 ConfigMap 值的一部分)。
  5. 当应用多个 --from-env-file 时,仅仅最初一个 env 文件无效。
  6. 作为卷模式在 Pod 中应用时,会呈现多个文件,文件名为 data 中的 key。

定义从文件创建 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

在 Pod 中挂载 ConfigMap

应用卷挂载的形式来应用 ConfigMap

创立 ConfigMap,名为 special-config

cat configmap-multikeys.yaml

apiVersion: v1kind: ConfigMapmetadata:  name: special-config  namespace: defaultdata:  SPECIAL_LEVEL: very  SPECIAL_TYPE: charm

创立 Pod

cat pod-configmap-volume.yaml

apiVersion: v1kind: Podmetadata:  name: dapi-test-podspec:  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-podSPECIAL_LEVELSPECIAL_TYPE

能够看到 /etc/config 目录下呈现了以 ConfigMap key 为名称的文件,内容为ConfigMap key对应的value

结束语

  1. 应用卷模式挂载的 ConfigMap 能够自动更新。
  2. 应用环境变量模式的 ConfigMap 无奈自动更新。
  3. 能够把 ConfigMap 了解为 Linux 中的 /etc 目录。
  4. ConfigMap 必须先于援用的 Pod 存在,否则 Pod 无奈启动。
  5. 每个 ConfigMap 只能被同一命名空间中的 Pod 援用。

分割我

微信公众号:zuolinux_com