ConfigMap
2020-04-09 13:49:47    470    0    0
gua_l


 

ConfigMaps allow you to decouple configuration artifacts from image content to keep containerized applications portable. This page provides a series of usage examples demonstrating how to create ConfigMaps and configure Pods using data stored in ConfigMaps.

ConfigMaps允许您将配置构件与映像内容解耦,以保持容器化应用程序的可移植性。这个页面提供了一系列使用示例,演示如何使用ConfigMaps中存储的数据创建ConfigMaps和配置Pods。

 

用kubectl create configmap创建

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

 

--from-file

--from-file 可以跟路径,可以跟单个文件,可以多次使用跟多个文件

 
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/
kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties
kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties
#show
kubectl get configmaps
kubectl describe configmap game-config-2
kubectl get configmap game-config-2 -o yaml


When you are creating a ConfigMap based on a file, the key in the <data-source> defaults to the basename of the file, and the value defaults to the file content

当您基于文件创建ConfigMap时,<data-source>中的键默认为文件的basename,而值默认为文件内容。

比如game-config 使用的两个文件的文件名game.properties和ui.properties 分别 作为 configmap 里data 的两个key ,而各自的文件内容则默认是key 的value.


root@vm1:~/k8s/flux# 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-04-10T05:27:41Z"
  name: game-config
  namespace: default
  resourceVersion: "680056"
  selfLink: /api/v1/namespaces/default/configmaps/game-config
  uid: f0818a4b-077e-4d98-a7a4-039693c0fcf7​

如果不想默认使用文件名作为key 可以--from-file 时指定自定义名称

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

 

--from-env-file

--from-env-file 多次使用时,只有最后一次使用的文件生效

Caution: When passing --from-env-file multiple times to create a ConfigMap from multiple data sources, only the last env-file is used.

# 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)).

env-file 的规则:

每一行格式是VAR=VAL

由#开头的行将被忽略

空行将被忽略

对引号无特殊处理,引号也会成为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​

 

--from-literal

从字面文字值创建,

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

 

--from-literal=special.how=very

--from-literal=special.type=charm

将在data 下添加:

special.how :very

special.type :charm


从生成器 configMapGenerator 创建

 

configMapGenerator

kustomization.yaml:

file:

file:  并指定key名

kubectl aaply -k .

# Create a kustomization.yaml file with ConfigMapGenerator
cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: game-config-4
  files:
  - configure-pod-container/configmap/game.properties
  #- special-key=configure-pod-container/configmap/game.properties
EOF
Apply the kustomization directory to create the ConfigMap object.
kubectl apply -k .
configmap/game-config-4-m9dm2f92bt created​

注意,生成的ConfigMap名称有一个后缀,它是通过散列内容来添加的。这确保了每次修改内容时都会生成一个新的ConfigMap。



在pod 中使用config map , 在容器的定义下,使用configMapKeyRef , 如下:

在container 中执行命令时使用configmap 的变量

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​​



 

 

 

 

Pre: FluxCD

Next: Flagger

470
Sign in to leave a comment.
No Leanote account? Sign up now.
0 comments
Table of content