ConfigMap - Kubernetes(翻译)

Configure a Pod to Use a ConfigMap(使用configmap配置一个Pod)

  1. 系统环境
    #cat /etc/redhat-release
    CentOS Linux release 7.4.1708 (Core)
    #kubeadm version
    kubeadm version: &version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.0",
    #kubectl version
    Client Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.0",
    Server Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.1",
  2. Configure 说明
    ConfigMaps允许你将配置文件、命令行参数或环境变量中读取的配置信息与docker image分离,以保持集装箱化应用程序的便携性。即ConfigMap API给我们提供了向容器中注入配置信息的机制。
  3. Create a ConfigMap(创建configmap)
    #使用命令
    kubectl create configmap <map-name> <data-source> 
    ##可以从目录,文件, literal values(字面值)来创建ConfigMap
    ##<map-name>指的是创建configmap时分配给的名字,<data-source>指的是configmap的数据来源,可以是目录,文件, literal values(字面值)
    #其中,在configmap中数据源要符合键值对的形式,如:
    key=文件名字或者你在命令行中提供的键
    value=文件内容或者是你在命令行中提供的literal values
    #使用kubectl create configmap -h的帮助信息:
    ##Create a new configmap named my-config based on folder bar
    kubectl create configmap my-config --from-file=path/to/bar
    ##Create a new configmap named my-config with specified keys instead of file basenames on disk
    kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt
    ##Create a new configmap named my-config with key1=config1 and key2=config2
    kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2
  4. Create ConfigMaps from directories(从目录创建ConfigMaps)
    #你可以使用kubectl create configmap命令创建一个ConfigMaps从一个目录中的多个文件,如:
    mkdir -p configure-pod-container/configmap/kubectl/
    wget https://k8s.io/docs/tasks/configure-pod-container/configmap/kubectl/game.properties -O configure-pod-container/configmap/kubectl/game.properties
    wget https://k8s.io/docs/tasks/configure-pod-container/configmap/kubectl/ui.properties -O configure-pod-container/configmap/kubectl/ui.properties
    #创建 configmap 
    kubectl create configmap game-config --from-file=configure-pod-container/configmap/kubectl/
    configmap "game-config" created
    #看下configure-pod-container/configmap/kubectl/这个目录下的内容:
    ls configure-pod-container/configmap/kubectl/
    game.properties
    ui.properties
    #查看下创建的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>
  5. Create ConfigMaps from files(使用文件创建ConfigMaps)
    #你可以使用kubectl create configmap去创建ConfigMaps从一个或多个文件,如:
    kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/kubectl/game.properties
    #查看下创建的ConfigMap
    kubectl describe configmaps game-config-2
    kubectl describe configmap 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这个参数可以使用多次,你可以使用两次分别指定上个实例中的那两个配置文件,效果就跟指定整个目录是一样的。
    #如:
    kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/kubectl/game.properties --from-file=configure-pod-container/configmap/kubectl/ui.properties
    #创建configmap
    kubectl describe configmaps game-config-2
    #查看创建的configmap
    kubectl describe configmap 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>
  6. 使用env-file
    使用选项--from-env-file从一个env-file(环境变量文件)去创建ConfigMaps
    关于env-file说明:
    #env-file包含环境变量列表。
    #这些语法规则适用:
    #env-file中的每一行必须采用VAR = VAL格式。
    #以#开头的行(即注释)将被忽略。
    #空行被忽略。
    #没有特殊的引号处理(即它们将成为ConfigMap值的一部分))。
    #如:
    wget https://k8s.io/docs/tasks/configure-pod-container/configmap/kubectl/game-env-file.properties -O configure-pod-container/configmap/kubectl/game-env-file.properties
    cat configure-pod-container/configmap/kubectl/game-env-file.properties
    enemies=aliens
    lives=3
    allowed="true"
    #创建configmap
    kubectl create configmap game-config-env-file \
        --from-env-file=configure-pod-container/configmap/kubectl/game-env-file.properties
    #查看创建的configmap
    kubectl describe configmap game-config-env-file
    Name:         game-config-env-file
    Namespace:    default
    Labels:       <none>
    Annotations:  <none>
    Data
    \====
    allowed:
    \----
    "true"
    enemies:
    \----
    aliens
    lives:
    \----
    3
    Events:  <none>
    #但是需要注意的是:
    当多次使用--from-env-file选项时,仅仅最后一次env-file是有效的,如:
    wget https://k8s.io/docs/tasks/configure-pod-container/configmap/kubectl/ui-env-file.properties -O configure-pod-container/configmap/kubectl/ui-env-file.properties
    kubectl create configmap config-multi-env-files \
        --from-env-file=configure-pod-container/configmap/kubectl/game-env-file.properties \
        --from-env-file=configure-pod-container/configmap/kubectl/ui-env-file.properties
    #查看下ui-env-file.properties文件内容
    cat ui-env-file.properties
    color=purple
    textmode=true
    how=fairlyNice
    #查看创建的configmap
    kubectl describe configmap config-multi-env-files
    Name:         config-multi-env-files
    Namespace:    default
    Labels:       <none>
    Annotations:  <none>
    Data
    \====
    color:
    \----
    purple
    how:
    \----
    fairlyNice
    textmode:
    \----
    true
    Events:  <none>
  7. Define the key to use when creating a ConfigMap from a file(定义从文件创建ConfigMap时要使用的键)
    在使用--from-file参数时,可以定义除ConfigMap的数据部分中使用的文件名以外的其他键:
    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/kubectl/game.properties
    #查看创建的configmap
    kubectl describe configmap game-config-3
    Name:         game-config-3
    Namespace:    default
    Labels:       <none>
    Annotations:  <none>
    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
    Events:  <none>
  8. Create ConfigMaps from literal values(使用字面值创建)
    使用文字值创建,利用—from-literal参数传递配置信息,该参数可以使用多次,格式如下:
    kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
    #查看创建的configmap
    kubectl describe  configmap special-config
    Name:         special-config
    Namespace:    default
    Labels:       <none>
    Annotations:  <none>
    Data
    \====
    special.how:
    \----
    very
    special.type:
    \----
    charm
    Events:  <none>
  9. Define Pod environment variables using ConfigMap data(使用ConfigMap数据定义Pod的环境变量)
    (1)Define a Pod environment variable with data from a single ConfigMap(使用来自单个ConfigMap的数据定义Pod环境变量)
    步骤如下:
    《1》在ConfigMap中定义一个键值对的环境变量
    kubectl create configmap special-config --from-literal=special.how=very 
    《2》将定义在ConfigMap中的special.how值分配给Pod中的SPECIAL_LEVEL_KEY环境变量
    kubectl edit pod dapi-test-pod
    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:
           #Define the environment variable(定义环境变量)
           - name: SPECIAL_LEVEL_KEY
             valueFrom:
               configMapKeyRef:
                 #The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY (configmap中包含想要分配给SPECIAL_LEVEL_KEY的值)
                 name: special-config
                 #Specify the key associated with the value (指定与值关联的键)
                 key: special.how
     restartPolicy: Never
    《3》现在,Pod的输出SPECIAL_LEVEL_KEY=very

    (2)Define Pod environment variables with data from multiple ConfigMaps(使用来自多个ConfigMaps的数据定义Pod环境变量)

    步骤如下:
    《1》首先先创建configmap
    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
    《2》在Pod中定义环境变量
    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
    《3》现在,Pod的输出SPECIAL_LEVEL_KEY=very 和 LOG_LEVEL=info
  10. Configure all key-value pairs in a ConfigMap as Pod environment variables(将ConfigMap中的所有键值对配置为Pod环境变量)
    #注意:运行Kubernetes v1.6及更高版本的用户可以使用此功能。
    步骤如下:
    《1》创建包含多个键值对的ConfigMap。
    apiVersion: v1
    kind: ConfigMap
    metadata:
     name: special-config
     namespace: default
    data:
     SPECIAL_LEVEL: very
     SPECIAL_TYPE: charm
    《2》使用envFrom将所有ConfigMap的数据定义为Pod环境变量。 ConfigMap中的键成为Pod中的环境变量名称。
    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
    《3》现在,Pod的输出SPECIAL_LEVEL=very 和 SPECIAL_TYPE=charm.
  11. Use ConfigMap-defined environment variables in Pod commands(在Pod命令中使用ConfigMap定义的环境变量)
    #说明:
    使用$(VAR_NAME)Kubernetes替换语法在Pod规范的命令部分中使用ConfigMap定义的环境变量。
    使用下面的Pod进行明确说明:
    apiVersion: v1
    kind: Pod
    metadata:
    name: dapi-test-pod
    spec:
    containers:
    - name: test-container
      image: k8s.gcr.io/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
    在test-container容器中会输出:very charm
  12. Add ConfigMap data to a Volume(将ConfigMap数据添加到卷)
    #说明:
    如从文件创建ConfigMaps中所述,使用--from-file创建ConfigMap时,在这个文件中,键就是文件名,键值就是文件内容。
    如:
    apiVersion: v1
    kind: ConfigMap
    metadata:
    name: special-config
    namespace: default
    data:
    special.level: very
    special.type: charm
  13. Populate a Volume with data stored in a ConfigMap(使用存储在ConfigMap中的数据填充卷)
    #说明:
    在Pod规范的volumes部分下添加ConfigMap名称。这会将ConfigMap数据添加到指定为volumeMounts.mountPath的目录(在本例中为/ etc / config)。命令部分引用ConfigMap.p中存储的special.level项。
    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:
        #Provide the name of the ConfigMap containing the files you want
        #to add to the container
        name: special-config
    restartPolicy: Never
    #运行这个Pod之后,命令("ls /etc/config/")会输出如下内容:
    special.level
    special.type
    #注意:如果有文件在/etc/config/ 目录下,它们将会被删除!!
  14. Add ConfigMap data to a specific path in the Volume(将ConfigMap数据添加到卷中的特定路径)
    #说明:
    使用path字段为特定的ConfigMap项指定所需的文件路径。在这种情况下,special.level项将安装在/ etc / config / keys的config-volume卷中。
    apiVersion: v1
    kind: Pod
    metadata:
    name: dapi-test-pod
    spec:
    containers:
    - name: test-container
      image: k8s.gcr.io/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之后,命令 ("cat /etc/config/keys") 会输出如下内容:
    very
  15. Understanding ConfigMaps and Pods(理解ConfigMaps 和 Pods)
    说明:
    ConfigMap API资源用来保存key-value pair配置数据,这个数据可以在pods里使用,或者被用来为像controller一样的系统组件存储配置数据。虽然ConfigMap跟Secrets类似,但是ConfigMap更方便的处理不含敏感信息的字符串。 注意:ConfigMaps不是属性配置文件的替代品。ConfigMaps只是作为多个properties文件的引用。你可以把它理解为Linux系统中的/etc目录,专门用来存储配置文件的目录。下面举个例子,使用ConfigMap配置来创建Kuberntes Volumes,ConfigMap中的每个data项都会成为一个新文件。
    如:
    kind: ConfigMap
    apiVersion: v1
    metadata:
    creationTimestamp: 2016-02-18T19:14:38Z
    name: example-config
    namespace: default
    data:
    example.property.1: hello
    example.property.2: world
    example.property.file: |-
    property.1=value-1
    property.2=value-2
    property.3=value-3

    data一栏包括了配置数据,ConfigMap可以被用来保存单个属性,也可以用来保存一个配置文件。 配置数据可以通过很多种方式在Pods里被使用。ConfigMaps可以被用来:
    (1)设置环境变量的值
    (2)在容器里设置命令行参数
    (3)在数据卷里面创建config文件

转载于:https://blog.51cto.com/wutengfei/2135733

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值