K8S 之 configmap 的理解

本贴最后更新于 1939 天前,其中的信息可能已经东海扬尘

创建 configmap 的另一种方法(通过文件)

Kubernetes外挂配置管理—ConfigMap介绍 - 振宇要低调 - 博客园

configmap 中文指引

configmap 的组成要素: name 、 key&value

configmap 到 pod 的使用方式: 通过变量、volume 的映射

1. 何为细粒度:

configmap yaml 文件内格式:

data:
  special.passwd: yaodidiao
  special.user: zhenyuyaodidiao  ## 

yaml 中卷挂载内容:

spec:
  containers:
    - name: testvolume
      image: busybox
      imagePullPolicy: IfNotPresent
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
      command:
      - sleep
      - "360000"
  volumes:
    - name: config-volume
      configMap:
        name: game-config-4

挂载到 pod 后文件目录格式:

# cd /etc/config/
/etc/config # ls
special.passwd  special.user
/etc/config # cat special.user 
zhenyuyaodidiao/etc/config #    ## configmap  的key 为文件名字,变量值为文件内容

2. 何为粗粒度:

configmap yaml 文件内格式:

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

yaml 中卷挂载内容

spec:
  containers:
    - name: testvolume
      image: busybox
      imagePullPolicy: IfNotPresent
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
      command:
      - sleep
      - "360000"
  volumes:
    - name: config-volume
      configMap:
        name: game-config

通过卷挂载后相应文件结构及内容

# cd /etc/config/
/etc/config # ls
game.properties  ui.properties
/etc/config # cat 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
/etc/config # cat ui.properties 
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
/etc/config #

configmap 中 data 下键的内涵

  1. (暂时):data 下的键均对应的是文件名,其下的值为文件内容
    configmap 内容
[root@k8s-master propertirs]# kubectl create configmap game-config-4 --from-literal=special.user=zhenyu --from-literal=special.passwd=yaodidiao
configmap "game-config-4" created
[root@k8s-master propertirs]# kubectl get configmaps game-config-4 -o yaml
apiVersion: v1
data:
  special.passwd: yaodidiao
  special.user: zhenyu
kind: ConfigMap
metadata:
  creationTimestamp: 2017-03-21T03:36:12Z
  name: game-config-4
  namespace: default
  resourceVersion: "3003915"
  selfLink: /api/v1/namespaces/default/configmaps/game-config-4
  uid: 8802f6d2-0de7-11e7-b3d5-fa163ebba51b

configmap env 使用示例:

[root@k8s-master propertirs]# kubectl get configmaps game-config-4 -o yaml
apiVersion: v1
data:
  special.passwd: yaodidiao
  special.user: zhenyu
kind: ConfigMap
metadata:
  creationTimestamp: 2017-03-21T03:36:12Z
  name: game-config-4
  namespace: default
  resourceVersion: "3003915"
  selfLink: /api/v1/namespaces/default/configmaps/game-config-4
  uid: 8802f6d2-0de7-11e7-b3d5-fa163ebba51b
[root@k8s-master propertirs]# cat testEnv.yaml 
apiVersion: v1
kind: Pod
metadata:
  labels:
    name: testenv
    role: master
  name: testenv
spec:
  containers:
    - name: testenv
      image: busybox
      imagePullPolicy: IfNotPresent
      env:
        - name: SPECIAL_USER    ### it's here
          valueFrom:
            configMapKeyRef:
              name: game-config-4
              key: special.user
      command:
      - sleep
      - "360000"
[root@k8s-master propertirs]# kubectl create -f testEnv.yaml 
pod "testenv" created
[root@k8s-master propertirs]# kubectl exec -ti testenv sh
/ # echo $SPECIAL_USER
zhenyu
/ #

configmap 挂载卷的

[root@k8s-master propertirs]# kubectl get configmaps 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
metadata:
  creationTimestamp: 2017-03-21T03:22:34Z
  name: game-config
  namespace: default
  resourceVersion: "3002770"
  selfLink: /api/v1/namespaces/default/configmaps/game-config
  uid: a04f90f0-0de5-11e7-b3d5-fa163ebba51b
[root@k8s-master propertirs]# cat testVolume.yaml 
apiVersion: v1
kind: Pod
metadata:
  labels:
    name: testvolume
    role: master
  name: testvolume
spec:
  containers:
    - name: testvolume
      image: busybox
      imagePullPolicy: IfNotPresent
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
      command:
      - sleep
      - "360000"
  volumes:
    - name: config-volume
      configMap:
        name: game-config
[root@k8s-master propertirs]# kubectl create -f testVolume.yaml 
pod "testvolume" created
[root@k8s-master propertirs]# kubectl exec -ti testvolume sh
/ # cd /etc/config/
/etc/config # ls
game.properties  ui.properties
/etc/config # cat 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
/etc/config # cat ui.properties 
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
/etc/config #

configmap 使用示例

A:Configmap 代替环境变量

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  special.how: very
  special.type: charm
apiVersion: v1
kind: ConfigMap
metadata:
  name: env-config
  namespace: default
data:
  log_level: INFO

pod 中如何使用

  • 用于 pod 中的变量
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: gcr.io/google_containers/busybox
      command: [ "/bin/sh", "-c", "env" ]
      env:
        - name: SPECIAL_LEVEL_KEY #环境变量名
          valueFrom:    #值得来源
            configMapKeyRef: # 定义从configmap中获取
              name: special-config # configmap中对应的configmap名字
              key: special.how # configmap中的key的值
        - name: SPECIAL_TYPE_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: special.type
      envFrom:
        - configMapRef:
            name: env-config
  restartPolicy: Never
  • Kubernetes

    Kubernetes 是 Google 开源的一个容器编排引擎,它支持自动化部署、大规模可伸缩、应用容器化管理。

    108 引用 • 54 回帖 • 1 关注
  • configmap
    1 引用

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...