K8S实战(十四)| ConfigMap 对象

前言

ConfigMap 对象能够用来管理普通的、非机密的配置信息,以明文形式存放。html

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

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

更新历史

经过目录/文件建立 ConfigMap

从目录建立微信

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文件特色:

  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: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  SPECIAL_LEVEL: very
  SPECIAL_TYPE: charm

建立 Pod

cat pod-configmap-volume.yaml

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

结束语

  1. 使用卷模式挂载的 ConfigMap 能够自动更新。
  2. 使用环境变量模式的 ConfigMap 没法自动更新。
  3. 能够把 ConfigMap 理解为 Linux 中的 /etc 目录。
  4. ConfigMap 必须先于引用的 Pod 存在,不然 Pod 没法启动。
  5. 每一个 ConfigMap 只能被同一命名空间中的 Pod 引用。

联系我

微信公众号:zuolinux_com

微信扫码关注