kubernetes资源对象之ConfigMap

2020-03-27  本文已影响0人  一舍

ConfigMap用来保存应用的配置信息。

ConfigMap的创建

ConfigMap的创建支持三种模式:

从目录创建ConfigMap

从同一目录中的多个文件创建 ConfigMap

ls configure-pod-container/configmap/
game.properties ui.properties
kubectl create c game-config-1 --from-file=configure-pod-container/configmap/
kubectl get configmaps game-config -o yaml

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2016-02-18T18:52:05Z
  name: game-config
  namespace: default
  resourceVersion: "516"
  selfLink: /api/v1/namespaces/default/configmaps/game-config
  uid: b4952dc3-d670-11e5-8cd0-68f728db1985
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

从文件创建ConfigMap

从多个文件创建ConfigMap

ls configure-pod-container/configmap/
game.properties ui.properties
kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties --from-file=configure-pod-container/configmap/ui.properties

从环境变量文件创建ConfigMap

ls configure-pod-container/configmap/
game-env-file.properties
kubectl create configmap game-config-env-file --from-env-file=configure-pod-container/configmap/game-env-file.properties
kubectl get configmap game-config-env-file -o yaml

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2017-12-27T18:36:28Z
  name: game-config-env-file
  namespace: default
  resourceVersion: "809965"
  selfLink: /api/v1/namespaces/default/configmaps/game-config-env-file
  uid: d9d1ca5b-eb34-11e7-887b-42010a8002b8
data:
  allowed: '"true"'
  enemies: aliens
  lives: "3"

当使用多个 --from-env-file 来从多个数据源创建 ConfigMap 时,仅仅最后一个 env 文件有效

从文件创建ConfigMap支持自定义键

在使用--from-file参数定义ConfigMap时,默认情况下键值是文件名,Kubernetes支持自定义除文件名称之外的键

kubectl create configmap game-config-3 --from-file=game-special-key=configure-pod-container/configmap/game.properties
kubectl get configmaps game-config-3 -o yaml

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2016-02-18T18:54:22Z
  name: game-config-3
  namespace: default
  resourceVersion: "530"
  selfLink: /api/v1/namespaces/default/configmaps/game-config-3
  uid: 05f8da22-d671-11e5-8cd0-68f728db1985
data:
  game-special-key: |
    enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.passphrase=UUDDLRLRBABAS
    secret.code.allowed=true
    secret.code.lives=30

从Json/Yaml文件创建

Kubernetes除了支持通过命令行创建ConfigMap外,还支持从json/yaml文件创建ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  special.how: very
  special.type: charm
kubectl create  -f  config.yaml
configmap "special-config" created
kubectl get configmaps special-config -o yaml

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2016-02-18T19:14:38Z
  name: special-config
  namespace: default
  resourceVersion: "651"
  selfLink: /api/v1/namespaces/default/configmaps/special-config
  uid: dadce046-d673-11e5-8cd0-68f728db1985
data:
  special.how: very
  special.type: charm

从字符创建ConfigMap

kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
kubectl get configmaps special-config -o yaml

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2016-02-18T19:14:38Z
  name: special-config
  namespace: default
  resourceVersion: "651"
  selfLink: /api/v1/namespaces/default/configmaps/special-config
  uid: dadce046-d673-11e5-8cd0-68f728db1985
data:
  special.how: very
  special.type: charm

ConfigMap的使用

ConfigMap中保存着应用的配置信息,在使用这些信息时,支持两种使用模式:

定义容器的环境变量

定义容器的环境变量,支持两种模式:

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "env" ]
      env:
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: special.how
        - name: LOG_LEVEL
          valueFrom:
            configMapKeyRef:
              name: env-config
              key: log_level
  restartPolicy: Never
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "env" ]
      envFrom:
      - configMapRef:
          name: special-config
  restartPolicy: Never

ConfigMap 中的键成为 Pod 中的环境变量名称

挂载ConfigMap 中的数据

在 Pod 的PodSpec的 volumes 部分下添加 ConfigMap 名称,这会将 ConfigMap 数据添加到指定为 volumeMounts.mountPath 的目录,键值为目录下文件的文件名。

将ConfigMap挂载到指定目录下,支持三种模式:

将键值文件直接挂载到指定目录下

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/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
ls /etc/config/
SPECIAL_LEVEL SPECIAL_TYPE

将键值文件挂载到指定目录的子目录下

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "ls /etc/config/" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: special-config
        items:
        - key: SPECIAL_LEVEL
          path: keys
  restartPolicy: Never
ls /etc/config/
SPECIAL_LEVEL

一般情况下 ConfigMap 挂载文件时,会先覆盖掉挂载目录,然后再将 ConfigMap 中的内容作为文件挂载进行。如果想不对原来的文件夹下的文件造成覆盖,只是将 configmap 中的每个 键值,按照文件的方式挂载到目录下,可以使用 subpath 参数

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: nginx
      command: ["/bin/sh","-c","sleep 36000"]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/nginx/special.how
        subPath: special.how
  volumes:
    - name: config-volume
      configMap:
        name: special-config
        items:
        - key: special.how
          path: special.how
  restartPolicy: Never
上一篇下一篇

猜你喜欢

热点阅读