Use kubernetes of ConfigMap manage Pod profile

Brief introduction

ConfigMaps image with the container can be decoupled from the configuration file, portability of the application container. How to provide a series of exemplary article describes a method to create ConfigMaps, in the stored ConfigMapsconfiguration data in the Pod.

NOTE: This document is to be understood with reference to their official documents, and. If misleading, please criticism.

Create a ConfigMap

We may use kubectl create configmapor kustomization.yamlin ConfigMapcreating a builder ConfigMap. Kubernetes 1.14 version from the start, kubectl started to support the use of kustomization.yamlcreation ConfigMap.

Create a ConfigMap use kubectl create configmap

Use kubectl create configmapcreate configmaps from the directory, file, or literal.

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

<map-name>That configuration ConfigMapname, <data-source>the directory data read from the directory, file, or literal.

ConfigMap is data-sourcesupposed to be a key-value pair:

  • key = key command or file name provided in row
  • value = provide literal or the contents of the file on the command line provided

Use kubectl describeor kubectl getto view ConfigMapinformation.

Create a directory from ConfigMaps

It can be used kubectl create configmapto create multiple files from a directory under the sameConfigMap

# Create the local directory
mkdir -p configure-pod-container/configmap/

# Download the sample files into `configure-pod-container/configmap/` directory
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

# Create the configmap
kubectl create configmap game-config --from-file=configure-pod-container/configmap/

configure-pod-container/configmap/It contains even a file directory

game.properties
ui.properties

Check the configuration of ConfigMapcontent:

kubectl describe configmaps game-config

As can be seen from the results: ConfigMapThe datablock contains the configure-pod-container/configmap/directory game.propertiesand ui.propertiesall the contents of the two files.

Name:         game-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
game-env-file.properties:
----
enemies=aliens
lives=3
allowed="true"

# This comment and the empty line above it are ignored

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>

To yamloutput format is as follows:

# kubectl get configmaps game-config -o yaml
apiVersion: v1
data:
  game-env-file.properties: |
    enemies=aliens
    lives=3
    allowed="true"

    # This comment and the empty line above it are ignored
  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-01-17T07:18:08Z"
  name: game-config
  namespace: default
  resourceVersion: "10100181"
  selfLink: /api/v1/namespaces/default/configmaps/game-config
  uid: a447f523-ddf4-48f5-a0eb-6f24c732fee5

Creating ConfigMaps from a file

Use kubectl create configmapcreated from a separate file or multiple files ConfigMap.

E.g:

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

View Content:

# kubectl describe configmaps game-config-2

Output is as follows

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>

It can also be used multiple times --from-fileto create multiple data sources from parametersConfigMap

E.g:

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

Use --from-env-fileoption from the env-filecreation ConfigMap in:

E.g:

# Env-files contain a list of environment variables.
# These syntax rules apply:
#   Each line in an env file has to be in VAR=VAL format.
#   Lines beginning with # (i.e. comments) are ignored.
#   Blank lines are ignored.
#   There is no special handling of quotation marks (i.e. they will be part of the ConfigMap value)).

# Download the sample files into `configure-pod-container/configmap/` directory
wget https://kubernetes.io/examples/configmap/game-env-file.properties -O configure-pod-container/configmap/game-env-file.properties

# The env-file `game-env-file.properties` looks like below
cat configure-pod-container/configmap/game-env-file.properties
enemies=aliens
lives=3
allowed="true"

# This comment and the empty line above it are ignored

Creating ConfigMap

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

View content

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

Output follows

apiVersion: v1
data:
  allowed: '"true"'
  enemies: aliens
  lives: "3"
kind: ConfigMap
metadata:
  creationTimestamp: "2020-01-17T07:32:05Z"
  name: game-config-env-file
  namespace: default
  resourceVersion: "10103588"
  selfLink: /api/v1/namespaces/default/configmaps/game-config-env-file
  uid: 0ed90509-5577-4225-a1ad-826426069da3

Note: When multiple use by --from-env-filecreated from multiple data sources ConfigMap, only the last one env-filetakes effect.

Demonstrate multiple uses --from-env-fileexamples
such as:

# Download the sample files into `configure-pod-container/configmap/` directory
wget https://k8s.io/examples/configmap/ui-env-file.properties -O configure-pod-container/configmap/ui-env-file.properties

# Create the 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
# kubectl get configmap config-multi-env-files -o yaml

Output follows, it can be seen from the results of the last valid configuration is --from-env-filedesignatedui-env-file.properties

apiVersion: v1
data:
  color: purple
  how: fairlyNice
  textmode: "true"
kind: ConfigMap
metadata:
  creationTimestamp: "2020-01-17T07:45:01Z"
  name: config-multi-env-files
  namespace: default
  resourceVersion: "10106742"
  selfLink: /api/v1/namespaces/default/configmaps/config-multi-env-files
  uid: 583873dd-12e6-408a-9373-b9cfeb092d5a

Key definitions from a file you want to use to create ConfigMap

Using --from-filethe data file to a data source parameters of a given key, in ConfigMapthe datacan see the key block.

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

Here the <my-key-name>name of the key, that you want to use in the COnfigMap, <path-to-file>the data contents of the local data source key to be expressed.
E.g:

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

View content

# kubectl get configmaps game-config-3 -o yaml

The output results are as follows, in dataa name for the next block game-special-keyof key, keythe value is the --from-file=game-special-key=configure-pod-container/configmap/game.propertiescontent file.

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-01-17T07:59:47Z"
  name: game-config-3
  namespace: default
  resourceVersion: "10110353"
  selfLink: /api/v1/namespaces/default/configmaps/game-config-3
  uid: e1569842-1438-46c3-91da-30cd989c17da

ConfigMap created from literals

Use with --from-literalparameters kubectl create configmapto create a ConfigMap literal command line in.

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

You can use multiple key-valuekey-value pairs. Each key on the command line provided in ConfigMapthe datalower block are independent.

View content

# kubectl get configmaps special-config -o yaml

Output follows

apiVersion: v1
data:
  special.how: very
  special.type: charm
kind: ConfigMap
metadata:
  creationTimestamp: "2020-01-17T08:09:09Z"
  name: special-config
  namespace: default
  resourceVersion: "10112639"
  selfLink: /api/v1/namespaces/default/configmaps/special-config
  uid: 57306de5-bd39-4d98-84fe-12357312551b

Creating ConfigMap from the generator

kubeletFrom the beginning supports the use of version 1.14 kustomization.yaml. It can be created from the generator ConfigMap, and then used to create an object on Apiserver thereof.

# Create a kustomization.yaml file with ConfigMapGenerator
cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: game-config-4
  files:
  - configure-pod-container/configmap/game.properties
EOF

Application Kustomization directory is created ConfigMap object.

kubectl apply -k .

Check whether to create success

# kubectl get configmap
NAME                       DATA   AGE
...
game-config-4-m9dm2f92bt   1      48s
...
# 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\nlives=3\nenemies.cheat=true\nenemies.cheat.level=noGoodRotten\nsecret.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>

Note that the generated ConfigMap by having the name of the content hash and after an additional suffix. This ensures that will generate a new ConfigMap each time content changes.

Key definitions used when the file is generated from ConfigMap

You can define a Key, rather than to the ConfigMapfile name to use builder. For example, to use game-special-keythe file configure-pod-container/configmap/game.propertiesgeneratedConfigMap

# Create a kustomization.yaml file with ConfigMapGenerator
cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: game-config-5
  files:
  - game-special-key=configure-pod-container/configmap/game.properties
EOF

Application Kustomization directory is created ConfigMap object.

kubectl apply -k .

Generating from the literal ConfigMap

For the custom type=charmand special.how=verycreate literals ConfigMap, in kustomization.yamlthe definition of the generator is as follows:

# Create a kustomization.yaml file with ConfigMapGenerator
cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: special-config-2
  literals:
  - special.how=very
  - special.type=charm
EOF

create

kubectl apply -k .

Use environment variables data defining the container ConfigMap

From a single ConfigMapof the data defining the container environment variables:

1, ConfigMapto key-valuethe key-value pairs define environment variables

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

2, the ConfigMapdefined special.howvalue is assigned to the Pod specin the SPECIAL_LEVEL_KEYenvironment variables.

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
              name: special-config
              # Specify the key associated with the value
              key: special.how
  restartPolicy: Never

Create a Pod

kubectl create -f /root/k8s-example/pods/pod-single-configmap-env-variable.yaml

Pod's output include environment variablesSPECIAL_LEVEL_KEY=very

From a plurality of ConfigMapscontainer defines data variables

Creating ConfigMaps:

kubectl create -f /root/k8s-example/configmap/configmaps.yaml

Pod in a specdefined environment variables

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

Creating Pod:

kubectl create -f /root/k8s-example/pods/pod-multiple-configmap-env-variable.yaml

Pod environment variable output comprises two SPECIAL_LEVEL_KEY=veryand LOG_LEVEL=INFO.

Configure all key-value pair as a key in the container environment ConfigMap variable

Create a multiple key-value pairs ConfigMap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  SPECIAL_LEVEL: very
  SPECIAL_TYPE: charm
kubectl create -f /root/k8s-example/configmap/configmap-multikeys.yaml

Use envFromall the environmental variables as a container in the data definition ConfigMap. ConfigMapThe Keybecame Pod environment variables.

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

Create a Pod

kubectl create -f https://kubernetes.io/examples/pods/pod-configmap-envFrom.yaml

Pod output include environment variables SPECIAL_LEVEL=very, andSPECIAL_TYPE=charm

ConfigMap variables defined in command Pod

In the Pod specin the commandblock may be used $(VAR_NAME)the way, introducing ConfigMapenvironment variables are defined

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-containerOutput container as follows:

very charm

Volumes added ConfigMap data

Creating ConfigMap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  SPECIAL_LEVEL: very
  SPECIAL_TYPE: charm
# kubectl create -f /root/k8s-example/configmap/configmap-multikeys.yaml

Pod in the specspecification volumeto add the name ConfigMap lower block. This configuration adds data to the specified ConfigMap the volumeMounts.mountPathdirectory, i.e., the container below /etc/configthe directory. commandBlock lists volumeMounts.mountPathall files in the directory.

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

Creating Pod:

# kubectl apply -f pod-configmap-volume.yaml

View container log output

# kubectl logs dapi-test-pod

Output as follows:

SPECIAL_LEVEL
SPECIAL_TYPE

Note: If you have files in the / etc / config directory will be deleted.

Volume ConfigMap added to the specified data path

Use pathfield configuration in a particular item ConfigMap to the specified path. For example, SPECIAL_LEVELthe content item will be mounted at config-volumethe specified /etc/config/keysvolume.

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

Create a Pod

# kubectl apply -f /root/k8s-example/pods/pod-configmap-volume-specific-key.yaml

View log output

# kubectl logs dapi-test-pod
very

Note: all previous files in / etc / config / directory will be deleted

Delete Resource

# kubectl delete configmaps game-config
# kubectl delete configmaps game-config-2
# kubectl delete configmaps config-multi-env-files
# kubectl delete configmaps game-config-3
...

Pod and understanding ConfigMap

API resources ConfigMap in key-value pairs stored configuration data. Data may be used in the Pod may be configured to provide system components. For example controllers. Secrets ConfigMap and similar, but there is provided a method for the string does not contain sensitive information processing. Users and system components in the configuration data can be stored in ConfigMap.

Note: ConfigMap should reference the properties file, rather than replacing them. ConfigMap can be expressed as similar to the Linux / etc directory and its contents thing. For example, if the volume created Kubernetes ConfigMap, then each data item ConfigMap by a single file in the volume is represented.

ConfigMap the datafield contains configuration data. The following example, it can be very simple, for example using --from-literala single attribute definition embodiment, can also be very complex, such as the use --from-filedefined profile

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

ConfigMap restrictions

  • You must create ConfigMap before Pod is created (unless ConfigMap is optional set. If the referenced ConfigMap does not exist, Pod does not start. Also, do not exist for ConfigMap keyreferences also prevent Pod start.

  • If you use a envFromdefined environment variables from ConfigMap, the key will be considered invalid ignored. You can start Pod, but invalid names will be recorded in the event log (InvalidVariableNames) in. Lists each log message keys skipped, the following command can be used to view

kubectl get events

Similar output is as follows:

LASTSEEN FIRSTSEEN COUNT NAME          KIND  SUBOBJECT  TYPE      REASON                            SOURCE                MESSAGE
   0s       0s        1     dapi-test-pod Pod              Warning   InvalidEnvironmentVariableNames   {kubelet, 127.0.0.1}  Keys [1badkey, 2alsobad] from the EnvFrom configMap default/myconfig were skipped since they are considered invalid environment variable names.
  • ConfigMap reside in a particular namespace. ConfigMap only be referenced by name in the same space Pod
  • You can not configure ConfigMaps to the static Pod, Kubelet not supported.

Guess you like

Origin www.cnblogs.com/mcsiberiawolf/p/12227855.html