zoukankan      html  css  js  c++  java
  • Kubernetes之使用ConfigMap配置Pod

      官方参考:https://kubernetes.io/zh/docs/tasks/configure-pod-container/configure-pod-configmap/

      创建ConfigMap  

      可以在 kustomization.yaml 中使用 kubectl create configmap 或 ConfigMap 生成器来创建ConfigMap。注意,从 1.14 版本开始, kubectl 开始支持 kustomization.yaml

      使用kubectl创建ConfigMap

      在目录,文件或文字值中使用kubelet create configmap命令创建configmap

    kubectl create configmap <map-name> <data-source>
    

       其中, <map-name> 是要分配给 ConfigMap 的名称,<data-source> 是要从中提取数据的目录,文件或者文字值。

      数据源对应于 ConfigMap 中的 key-value (键值对)

    •  key=在命令行上题库的文件名或者秘钥
    •  value=在命令行上提供的文件内容或者文字值

      可以使用 kubelet describe或者kubelet get检索有关ConfigMap的信息

      根据目录创建ConfigMap

      你可以使用 kubectl create configmap 从同一目录中的多个文件创建 ConfigMap。

    #创建本地目录
    mkdir -p configure-pod-container/configmap/
    #将样板文件下载到创建的目录
    wget https://kubernetes.io/examples/configmap/game.properties -O configure-pod-container/configmap/game.properties
    wget https://kubernetes.io/examples/configmap/ui.properties -O configure-pod-container/configmap/ui.properties
    #创建configmap
    kubectl create configmap game-config --from-file=configure-pod-container/configmap/
    

       合并了以下两个文件的内容

    # cat configure-pod-container/configmap/game.properties 
    enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.passphrase=UUDDLRLRBABAS
    secret.code.allowed=true
    
    # cat configure-pod-container/configmap/ui.properties 
    color.good=purple
    color.bad=yellow
    allow.textmode=true
    how.nice.to.look=fairlyNice
    

       进入以下ConfigMap中

    kubectl describe configmaps game-config
    

       输出类似以下内容

    Name:         game-config
    Namespace:    default
    Labels:       <none>
    Annotations:  <none>
    
    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
    
    Events:  <none>
    

       

    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
    metadata:
      creationTimestamp: "2020-03-17T08:12:49Z"
      name: game-config
      namespace: default
      resourceVersion: "505452"
      selfLink: /api/v1/namespaces/default/configmaps/game-config
      uid: 1f3868e4-7a6b-4bf3-9317-00911ebc6e91
    

       根据文件创建ConfigMap

      可以使用kubelet create configmap从单个文件或多个文件创建ConfigMap

      例如

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

       将产生以下ConfigMap

    kubectl describe configmaps game-config-2
    

       输出类似以下内容

    Name:         game-config-2
    Namespace:    default
    Labels:       <none>
    Annotations:  <none>
    
    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
    Events:  <none>
    

       可以传入多个 --from-file 参数,从多个数据源创建 ConfigMap

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

       描述上面创建的game-config-2 configmap

    kubectl describe configmaps game-config-2
    

       输出类似以下内容

    Name:         game-config-2
    Namespace:    default
    Labels:       <none>
    Annotations:  <none>
    
    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
    
    Events:  <none>
    

       使用 --from-env-file 选项从环境文件创建 ConfigMap,例如:

    # 环境文件包含环境变量列表。
    # 语法规则:
    #   env 文件中的每一行必须为 VAR = VAL 格式。
    #   以#开头的行(即注释)将被忽略。
    #   空行将被忽略。
    #   引号没有特殊处理(即它们将成为 ConfigMap 值的一部分)。
    
    # 将样本文件下载到 `configure-pod-container/configmap/` 目录
    wget https://kubernetes.io/examples/configmap/game-env-file.properties -O configure-pod-container/configmap/game-env-file.properties
    
    # env文件 `game-env-file.properties` 如下所示
    cat configure-pod-container/configmap/game-env-file.properties
    enemies=aliens
    lives=3
    allowed="true"
    
    # 注释及其上方的空行将被忽略
    
    kubectl create configmap game-config-env-file --from-env-file=configure-pod-container/configmap/game-env-file.properties 
    

       将产生以下ConfigMap

    kubectl get configmap game-config-env-file -o yaml
    

       输出类似以下内容

    apiVersion: v1
    data:
      allowed: '"true"'
      enemies: aliens
      lives: "3"
    kind: ConfigMap
    metadata:
      creationTimestamp: "2020-03-17T08:25:11Z"
      name: game-config-env-file
      namespace: default
      resourceVersion: "507620"
      selfLink: /api/v1/namespaces/default/configmaps/game-config-env-file
      uid: f22b64e7-2232-4c6a-aa85-afbbafb6bcac
    

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

    # 将样本文件下载到 `configure-pod-container/configmap/` 目录
    wget https://k8s.io/examples/configmap/ui-env-file.properties -O configure-pod-container/configmap/ui-env-file.properties
    
    # 创建 configmap
    kubectl create configmap config-multi-env-files 
        --from-env-file=configure-pod-container/configmap/game-env-file.properties 
        --from-env-file=configure-pod-container/configmap/ui-env-file.properties 
    

       将产生以下ConfigMap

    kubectl get configmap config-multi-env-files -o yaml
    

       输出类似以下内容

    apiVersion: v1
    data:
      color: purple
      how: fairlyNice
      textmode: "true"
    kind: ConfigMap
    metadata:
      creationTimestamp: "2020-03-17T08:31:36Z"
      name: config-multi-env-files
      namespace: default
      resourceVersion: "508738"
      selfLink: /api/v1/namespaces/default/configmaps/config-multi-env-files
      uid: d09564b2-b683-455c-8360-423edd3dbbbf
    

       定义从文件创建 ConfigMap时要使用自定义建名

      您可以在使用 --from-file 参数时,在 ConfigMap 的 data 部分中定义除文件名以外的其他键:

    kubectl create configmap game-config-3 --from-file=<my-key-name>=<path-to-file>
    

       <my-key-name> 是您要在 ConfigMap 中使用的建名, <path-to-file> 是您想要键表示数据源文件的位置。

      例如

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

       将产生以下ConfigMap

    apiVersion: v1
    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
    kind: ConfigMap
    metadata:
      creationTimestamp: "2020-03-17T08:36:25Z"
      name: game-config-3
      namespace: default
      resourceVersion: "509581"
      selfLink: /api/v1/namespaces/default/configmaps/game-config-3
      uid: a9ca7b2b-28d1-4fc2-ac13-48e7147fcf87
    

       PS:使用文件创建的configmap默认的建名是文件名,以上自定义了其他建名不指定建名创建做对比

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

     

       根据文字值生成ConfigMap

      您可以将 kubectl create configmap 与 --from-literal 参数一起使用,从命令行定义文字值:

    kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
    

       您可以传入多个键值对。命令行中提供的每对在 ConfigMap 的 data 部分中均表示为单独的条目。

    kubectl get configmaps special-config -o yaml
    

       输出类似以下内容

    apiVersion: v1
    data:
      special.how: very
      special.type: charm
    kind: ConfigMap
    metadata:
      creationTimestamp: "2020-03-17T08:54:43Z"
      name: special-config
      namespace: default
      resourceVersion: "512776"
      selfLink: /api/v1/namespaces/default/configmaps/special-config
      uid: b7f972bf-1eef-4c74-b496-8b90cf3476d2
    

       根据生成器创建ConfigMap

      自 1.14 开始, kubectl 开始支持 kustomization.yaml。 您还可以从生成器创建 ConfigMap,然后将其应用于 Apiserver 创建对象。生成器应在目录内的 kustomization.yaml 中指定。

    # 使用 ConfigMapGenerator 创建 kustomization.yaml 文件
    cat <<EOF >./kustomization.yaml
    configMapGenerator:
    - name: game-config-4
      files:
      - configure-pod-container/configmap/kubectl/game.properties
    EOF
    

       使用 kustomization 目录创建 ConfigMap 对象

    # kubectl apply -k .
    configmap/game-config-4-m9dm2f92bt created
    

       PS:文件kustomization.yaml需要与文件夹configure-pod-container在同一个目录并且该文件夹下面没有其他文件

       可以检查ConfigMap是这样创建的

    # kubectl describe configmaps/game-config-4-m9dm2f92bt
    Name:         game-config-4-m9dm2f92bt
    Namespace:    default
    Labels:       <none>
    Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                    {"apiVersion":"v1","data":{"game.properties":"enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.p...
    
    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
    Events:  <none>
    

       请注意,生成的 ConfigMap 名称具有通过对内容进行散列而附加的后缀,这样可以确保每次修改内容时都会生成新的 ConfigMap。

      定义从文件生成ConfigMap是要使用建名

    # cat kustomization.yaml 
    configMapGenerator:
    - name: game-config-5
      files:
      - game-special-key=configure-pod-container/configmap/kubectl/game.properties
    

       使用 Kustomization 目录创建 ConfigMap 对象

    kubectl apply -k .
    configmap/game-config-5-m67dt67794 created
    

       对比

       从文字生成CofigMap

    #cat kustomization.yaml 
    configMapGenerator:
    - name: special-config-2
      literals:
      - special.how=very
      - special.type=charm
    

       使用 Kustomization 目录创建 ConfigMap 对象。

    kubectl apply -k .
    

       

    kubectl describe configmap special-config-2-c92b5mmcf2
    

      输出类似以下内容

    Name:         special-config-2-c92b5mmcf2
    Namespace:    default
    Labels:       <none>
    Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                    {"apiVersion":"v1","data":{"special.how":"very","special.type":"charm"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"special-co...
    
    Data
    ====
    special.how:
    ----
    very
    special.type:
    ----
    charm
    Events:  <none>
    

       使用 ConfigMap 数据定义容器环境变量

       1.在ConfigMap中将环境变量定义为键值对

    kubectl create configmap special-config --from-literal=special.how=very
    

       查看该键值对

    # kubectl get configmap special-config -o yaml
    apiVersion: v1
    data:
      special.how: very
    kind: ConfigMap
    metadata:
      creationTimestamp: "2020-03-18T01:47:01Z"
      name: special-config
      namespace: default
      resourceVersion: "689548"
      selfLink: /api/v1/namespaces/default/configmaps/special-config
      uid: 89b580f2-714d-4e47-87f6-90d18aa9aa3d
    

       将 ConfigMap 中定义的 special.how 值分配给 Pod 规范中的 SPECIAL_LEVEL_KEY 环境变量。

      下载示例Pod

    wget https://kubernetes.io/examples/pods/pod-single-configmap-env-variable.yaml
    

       

    apiVersion: v1
    kind: Pod
    metadata:
      name: dapi-test-pod
    spec:
      containers:
        - name: test-container
          image: busybox
          command: [ "/bin/sh", "-c", "env" ]
          env:
            # Define the environment variable
            # 定义容器内环境变量变量名为SPECIAL_LEVEL_KEY
            - name: SPECIAL_LEVEL_KEY
              valueFrom:
                configMapKeyRef:
                  # The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
                  # 包含键值映射的configmap为special-config
                  name: special-config
                  # Specify the key associated with the value
                  # 对应的key为special.how该key对应的值为very
                  key: special.how
      restartPolicy: Never
    

       创建Pod

    kubectl apply -f pod-single-configmap-env-variable.yaml
    

       该Pod运行日志输出环境变量,查看日志

    # kubectl logs dapi-test-pod
    KUBERNETES_PORT=tcp://10.0.0.1:443
    KUBERNETES_SERVICE_PORT=443
    MY_SERVICE_PORT_80_TCP=tcp://10.0.0.47:80
    HOSTNAME=dapi-test-pod
    SHLVL=1
    HOME=/root
    MY_SERVICE_SERVICE_PORT_HTTP=80
    MY_SERVICE_SERVICE_HOST=10.0.0.47
    KUBERNETES_PORT_443_TCP_ADDR=10.0.0.1
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    KUBERNETES_PORT_443_TCP_PORT=443
    KUBERNETES_PORT_443_TCP_PROTO=tcp
    MY_SERVICE_SERVICE_PORT=80
    MY_SERVICE_PORT=tcp://10.0.0.47:80
    SPECIAL_LEVEL_KEY=very
    MY_SERVICE_PORT_80_TCP_ADDR=10.0.0.47
    KUBERNETES_SERVICE_PORT_HTTPS=443
    KUBERNETES_PORT_443_TCP=tcp://10.0.0.1:443
    MY_SERVICE_PORT_80_TCP_PORT=80
    KUBERNETES_SERVICE_HOST=10.0.0.1
    PWD=/
    MY_SERVICE_PORT_80_TCP_PROTO=tcp
    

     

       该Pod执行完输出环境变量以后因为重启策略是Never就是完成状态了

       使用多个ConfigMap定义容器变量

      创建ConfigMap

    # cat configmaps.yaml 
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: special-config
      namespace: default
    data:
      special.how: very
    ---
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: env-config
      namespace: default
    data:
      log_level: INFO
    

       创建

    kubectl apply -f configmaps.yaml 
    

       在Pod中定义环境变量

    # cat pod-multiple-configmap-env-variable.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      name: dapi-test-pod
    spec:
      containers:
        - name: test-container
          image: 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
    

       创建Pod

    kubectl apply -f pod-multiple-configmap-env-variable.yaml 
    

       现在,Pod 的输出包含环境变量 SPECIAL_LEVEL_KEY=very 和 LOG_LEVEL=INFO

       将 ConfigMap 中的所有键值对配置为容器环境变量

      注意:Kubernetes v1.16和更高版本提供此功能

      创建一个包含多个键值对的ConfigMap

    # cat configmap-multikeys.yaml 
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: special-config
      namespace: default
    data:
      SPECIAL_LEVEL: very
      SPECIAL_TYPE: charm
    

       创建ConfigMap

    kubectl apply -f configmap-multikeys.yaml 
    

       查看创建的ConfigMap

    # kubectl get configmap special-config -o yaml
    apiVersion: v1
    data:
      SPECIAL_LEVEL: very
      SPECIAL_TYPE: charm
    kind: ConfigMap
    metadata:
      annotations:
        kubectl.kubernetes.io/last-applied-configuration: |
          {"apiVersion":"v1","data":{"SPECIAL_LEVEL":"very","SPECIAL_TYPE":"charm"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"special-config","namespace":"default"}}
      creationTimestamp: "2020-03-18T02:13:48Z"
      name: special-config
      namespace: default
      resourceVersion: "694299"
      selfLink: /api/v1/namespaces/default/configmaps/special-config
      uid: 6df7c234-7ec6-4fad-8c5e-c34aa32f948a
    

      使用 envFrom 将所有 ConfigMap 的数据定义为容器环境变量,ConfigMap 中的键成为 Pod 中的环境变量名称。  

      

    # cat pod-configmap-envFrom.yaml 
    apiVersion: v1
    kind: Pod
    metadata:
      name: dapi-test-pod
    spec:
      containers:
        - name: test-container
          image: busybox
          command: [ "/bin/sh", "-c", "env" ]
          envFrom:
          - configMapRef:
              name: special-config
      restartPolicy: Never
    

      创建Pod

    kubectl apply -f pod-configmap-envFrom.yaml
    

       现在,Pod 的输出包含环境变量 SPECIAL_LEVEL=very 和 SPECIAL_TYPE=charm

       在 Pod 命令中使用 ConfigMap 定义的环境变量

    # cat pod-configmap-env-var-valueFrom.yaml 
    apiVersion: v1
    kind: Pod
    metadata:
      name: dapi-test-pod
    spec:
      containers:
        - name: test-container
          image: busybox
          command: [ "/bin/sh", "-c", "echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)" ]
          env:
            - name: SPECIAL_LEVEL_KEY
              valueFrom:
                configMapKeyRef:
                  name: special-config
                  key: SPECIAL_LEVEL
            - name: SPECIAL_TYPE_KEY
              valueFrom:
                configMapKeyRef:
                  name: special-config
                  key: SPECIAL_TYPE
      restartPolicy: Never
    

       创建Pod

    kubectl apply -f pod-configmap-env-var-valueFrom.yaml
    

       在 test-container 容器中产生以下输出:

    # kubectl logs dapi-test-pod 
    very charm
    

       将ConfigMap数据添加到一个容器中

      当您使用 --from-file 创建 ConfigMap 时,文件名成为存储在 ConfigMap 的 data 部分中的key,文件内容成为key的值。

      本节中的示例引用了一个名为 special-config 的 ConfigMap,如下所示:

    # cat configmap-multikeys.yaml 
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: special-config
      namespace: default
    data:
      SPECIAL_LEVEL: very
      SPECIAL_TYPE: charm
    

       创建ConfigMap

    kubectl apply -f configmap-multikeys.yaml 
    

       使用存储在 ConfigMap 中的数据填充容器

      在 Pod 规范的 volumes 部分下添加 ConfigMap 名称。 这会将 ConfigMap 数据添加到指定为 volumeMounts.mountPath 的目录(在本例中为/etc/config)。 command 引用存储在 ConfigMap 中的 special.level

    # 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:
            # Provide the name of the ConfigMap containing the files you want
            # to add to the container
            name: special-config
      restartPolicy: Never
    

       创建Pod

    kubectl apply -f pod-configmap-volume.yaml
    

       容器运行命令 ls /etc/config/ 产生下面的输出:

    # kubectl logs dapi-test-pod 
    SPECIAL_LEVEL
    SPECIAL_TYPE
    

       注意:

      如果在/etc/config/目录中有一些文件,他们将被删除

      这里ls显示的其实是两个文件名称

      可以修改pod-configmap-volume.yaml增加一个sleep这样启动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/ && sleep 3600" ]
          volumeMounts:
          - name: config-volume
            mountPath: /etc/config
      volumes:
        - name: config-volume
          configMap:
            # Provide the name of the ConfigMap containing the files you want
            # to add to the container
            name: special-config
      restartPolicy: Never
    

       登录Pod查看

    # kubectl exec -it dapi-test-pod sh
    # cd /etc/config/
    # ls -l
    total 0
    lrwxrwxrwx    1 root     root            20 Mar 18 09:09 SPECIAL_LEVEL -> ..data/SPECIAL_LEVEL
    lrwxrwxrwx    1 root     root            19 Mar 18 09:09 SPECIAL_TYPE -> ..data/SPECIAL_TYPE
    #文件内容就是very和charm
    # cat SPECIAL_LEVEL 
    very
    # cat SPECIAL_TYPE 
    charm
    

      将 ConfigMap 数据添加到容器中的特定路径

      使用 path 字段为特定的 ConfigMap 项目指定所需的文件路径。 在这种情况下, SPECIAL_LEVEL 将安装在 /etc/config/keys 目录下的 config-volume 容器中。

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

       创建Pod

    kubectl apply -f pod-configmap-volume-specific-key.yaml
    

       当 pod 运行时,命令 cat /etc/config/keys 产生以下输出

    # kubectl logs dapi-test-pod
    very
    

       为什么输出是very 修改yaml文件注释

    # cat pod-configmap-volume-specific-key.yaml 
    apiVersion: v1
    kind: Pod
    metadata:
      name: dapi-test-pod
    spec:
      containers:
        - name: test-container
          image: busybox
          #增加sleep防止运行完以后Pod处于compled状态
          command: [ "/bin/sh","-c","cat /etc/config/keys && sleep 3600 " ]
          volumeMounts:
          #挂载对应的名称是volumes对应的config-volume
          - name: config-volume
            #挂载的目录是容器内目录/etc/config
            #以keys名挂载以后的文件名称是/etc/config/keys
            mountPath: /etc/config
      volumes:
        - name: config-volume
          configMap:
            name: special-config
            items:
            #取ConfigMap的key为SPECIAL_LEVE的值是very
            - key: SPECIAL_LEVEL
              #在volumeMount是挂载的名称是keys
              path: keys
      restartPolicy: Never
    

       创建Pod

    # kubectl apply -f pod-configmap-volume-specific-key.yaml 
    pod/dapi-test-pod created
    

       登录Pod内部查看

    kubectl exec -it dapi-test-pod sh
    cd /etc/config/
    ls
    keys
    cat keys 
    very
    

      了解ConfigMap和Pod

      ConfigMap API 资源将配置数据存储为键值对。数据可以在 Pod 中使用,也可以提供系统组件(如控制器)的配置。ConfigMap 与 Secrets类似,但是提供了一种使用不包含敏感信息的字符串的方法。用户和系统组件都可以在 ConfigMap 中存储配置数据。

      ConfigMap 应该引用属性文件,而不是替换它们。可以将 ConfigMap 表示为类似于 Linux /etc 目录及其内容的东西。例如,如果您从 ConfigMap 创建Kubernetes Volume,则 ConfigMap 中的每个数据项都由该容器中的单个文件表示。

      ConfigMap 的 data 字段包含配置数据。如下例所示,它可以很简单 – 就像使用 --from-literal – 定义的单个属性一样,也可以很复杂 – 例如使用 --from-file 定义的配置文件或 JSON blob。

    apiVersion: v1
    kind: ConfigMap
    metadata:
      creationTimestamp: 2016-02-18T19:14:38Z
      name: example-config
      namespace: default
    data:
      # example of a simple property defined using --from-literal
      example.property.1: hello
      example.property.2: world
      # example of a complex property defined using --from-file
      example.property.file: |-
        property.1=value-1
        property.2=value-2
        property.3=value-3
    

       限制规定

      在 Pod 规范中引用它之前,必须先创建一个 ConfigMap(除非将 ConfigMap 标记为”可选”)。如果引用的 ConfigMap 不存在,则 Pod 将不会启动。同样,对 ConfigMap 中不存在的键的引用将阻止容器启动。

      如果您使用 envFrom 从 ConfigMap 中定义环境变量,那么将忽略被认为无效的键。可以启动 Pod,但无效名称将记录在事件日志中(InvalidVariableNames)。日志消息列出了每个跳过的键。例如:

    kubectl get events
    

       如果没有key将事件中出现类似以下提示

    15m         Warning   Failed        pod/dapi-test-pod   Error: couldn't find key log_level in ConfigMap default/special-config
    

      ConfigMap 只能由位于相同命令空间中的 Pod 引用。

      Kubelet 不支持将 ConfigMap 用于未在 API 服务器上找到的 Pod。这包括通过 Kubelet 的 --manifest-url 参数,--config 参数或者 Kubelet REST API 创建的容器。

      使用ConfigMap来配置Redis

      目标

      创建一个包含以下内容的kustomization.yaml 文件

    • 一个ConfigMap生成器
    • 一个使用ConfigMap的Pod资源配置

      使用kubectl apply -k ./应用整个路径的配置

      验证配置是否正确

      使用kustomization.yaml Kubernetes版本必须1.14及以上 查看版本信息使用命令

    kubectl version
    

       按照以下步骤,可以使用ConfigMap中的数据类配置Redis缓存

    curl -OL https://k8s.io/examples/pods/config/redis-config
    #文件内容如下
    maxmemory 2mb
    maxmemory-policy allkeys-lru
    
    cat <<EOF >./kustomization.yaml
    configMapGenerator:
    - name: example-redis-config
      files:
      - redis-config
    EOF
    

       将Pod的资源配置添加到kustomization.yaml 文件中

    # cat redis-pod.yaml 
    apiVersion: v1
    kind: Pod
    metadata:
      name: redis
    spec:
      containers:
      - name: redis
        image: redis:5.0.4
        command:
          - redis-server
          - "/redis-master/redis.conf"
        env:
        - name: MASTER
          value: "true"
        ports:
        - containerPort: 6379
        resources:
          limits:
            cpu: "0.1"
        volumeMounts:
        - mountPath: /redis-master-data
          name: data
        - mountPath: /redis-master
          name: config
      volumes:
        - name: data
          emptyDir: {}
        - name: config
          configMap:
            name: example-redis-config
            items:
            - key: redis-config
              path: redis.conf
    
    curl -OL https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/pods/config/redis-pod.yaml
    

       在kustomization.yaml追加

    cat <<EOF >>./kustomization.yaml
    resources:
    - redis-pod.yaml
    EOF
    

       此时完整的kustomization.yaml内容如下

    # cat kustomization.yaml 
    configMapGenerator:
    - name: example-redis-config
      files:
      - redis-config
    resources:
    - redis-pod.yaml
    

       应用整个 kustomization 文件夹以创建 ConfigMap 和 Pod 对象:

      首先应用文件redis-config文件创建ConfigMap,然后使用文件redis-pod.yaml创建Pod

    # kubectl apply -k .
    configmap/example-redis-config-dgh9dg555m created
    pod/redis created
    

       使用以下命令检查创建的对象

    # kubectl get -k .
    NAME                                        DATA   AGE
    configmap/example-redis-config-dgh9dg555m   1      36s
    
    NAME        READY   STATUS    RESTARTS   AGE
    pod/redis   1/1     Running   0          36s
    

       在示例中,配置卷挂载在/redis-master下。它使用path将redis-config的key添加到名为redis.conf的文件中。因此,redis配置的文件路径为/redis-master/redis.conf。 这是镜像将在其中查找 redis master 的配置文件的位置。

      使用kubectl exec进入Pod并运行redis-cli工具来验证配置已正确应用

    # kubectl exec -it redis redis-cli
    127.0.0.1:6379> CONFIG GET maxmemory
    1) "maxmemory"
    2) "2097152"
    127.0.0.1:6379> CONFIG GET maxmemory-policy
    1) "maxmemory-policy"
    2) "allkeys-lru"
    

       PS:redis使用命令CONFIG GET 参数获取对应配置值 这里最大内存设置的是2mb换算成byte就是2097152即2*1024*1024

      删除创建的Pod

    kubectl delete pod redis
    

       不使用kustomization.yaml创建一遍

      删除刚刚创建的ConfigMap和Pod

    kubectl delete pod redis
    kubectl delete configmap example-redis-config-dgh9dg555m
    

       使用文件创建ConfigMap

    #创建ConfigMap名为  example-redis-config
    kubectl create configmap example-redis-config --from-file=redis-config 
    configmap/example-redis-config created
    

       查看刚刚创建的ConfigMap

    #key为文件名redis-config内容及文件内部内容
    kubectl get configmap  example-redis-config -o yaml
    apiVersion: v1
    data:
      redis-config: |
        maxmemory 2mb
        maxmemory-policy allkeys-lru
    kind: ConfigMap
    metadata:
      creationTimestamp: "2020-03-18T06:06:54Z"
      name: example-redis-config
      namespace: default
      resourceVersion: "735256"
      selfLink: /api/v1/namespaces/default/configmaps/example-redis-config
      uid: 35f4baf9-b786-450f-a22c-768fa75a2d08
    

       创建Pod

    # cat redis-pod.yaml 
    apiVersion: v1
    kind: Pod
    metadata:
      name: redis
    spec:
      containers:
      - name: redis
        image: redis:5.0.4
        command:
          - redis-server
          - "/redis-master/redis.conf"
        env:
        - name: MASTER
          value: "true"
        ports:
        - containerPort: 6379
        resources:
          limits:
            cpu: "0.1"
        volumeMounts:
        - mountPath: /redis-master-data
          name: data
        #挂载配置文件从ConfigMap挂载目录为容器目录/redis-master最终挂载的文件为/redis-master/redis.conf
        #内容即文件redis-config内内容
        - mountPath: /redis-master
          name: config
      volumes:
        - name: data
          emptyDir: {}
        - name: config
          configMap:
            name: example-redis-config
            items:
            - key: redis-config
              path: redis.conf
    

       

    # kubectl apply -f redis-pod.yaml 
    pod/redis created
    

       可以使用以上redis-cli验证

      也可以登录Pod查看配置文件内容是否一致

    # kubectl exec -it redis bash
    # cat /redis-master/redis.conf 
    maxmemory 2mb
    maxmemory-policy allkeys-lru
    

       



      

      

     

      

       

      

  • 相关阅读:
    C#消息筛选实现自动锁屏功能
    C#Path目录路径常用操作
    WPFMVVMLight框架学习使用MVVMLight
    C#使用SqlSugar操作数据库导致的问题:托管调试助手“FatalExecutionEngineError”:运行时遇到了 错误。此错误的地址为。。。。
    django—admin 使用simpleui自定义左边菜单栏及去除simple的广告链接
    bcdedit
    Ubuntu中root用户和user用户的相互切换
    ubuntu创建、删除文件及文件夹,强制清空回收站方法
    javascript 内存监测工具
    常用前端开发工具合集
  • 原文地址:https://www.cnblogs.com/minseo/p/12512203.html
Copyright © 2011-2022 走看看