configMap

 ConfigMap功能在Kubernetes1.2版本中引入,许多应用程序会从配置文件、命令行参数或环境变量中读取配置信息。ConfigMap APi给我们提供了向容器中注入配置信息的机制,ConfigMap可以被用来保存单个属性,也可以用来保存整个配置文件或者JSON二进制大对象。

ConfigMap的创建:

使用目录创建:

mkdir configmaptest1
vim 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

vim ui.properties

color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice

kubectl create configmap game-config --from-file=/root/configmap

—from-file

指定在目录下的所有文件都会被用在 ConfigMap 里面创建一个键值对,键的名字就是文件名,值就是文件的内容。

使用文件创建

只要制定一个文件就可以从单个文件中创建ConfigMap

vim onlyfile.properties

ilives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30

kubectl create configmap onlyfile --from-file=/root/configmap/onlyfile.properties

--from-file 这个参数可以使用多次,你可以使用两次分别指定上个实例中的那两个配置文件,效果就跟制定整个目录是一样的。

使用字面值创建

使用文字值创建,利用--from-literal参数传递配置信息,该参数可以使用多次,格式如下

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

kubectl get configmaps special-config -o yaml

Pod中使用ConfigMap

使用ConfigMap来替代环境变量:

special-config :

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

env-config:

pod

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
  - name: test-container
    image: wangyanglinux/myapp:v1
    command: ["/bin/sh","-c","env"]
    env:
    - name: SPECIAL_LEVEL_KEY
      valueFrom:
        configMapKeyRef:
          name: special-config
          key: special.how
    - name: SPECIAL_TYPE_KEY
      valueFrom:
        configMapKeyRef:
          name: special-config
          key: special.type
    envFrom:
    - configMapRef:
        name: env-config
  restartPolicy: Never

使用ConfigMap 设置命令行参数

configmap依旧使用上方:

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod1
spec:
  containers:
  - name: test-container1
    image: wangyanglinux/myapp:v1
    command: ["/bin/sh","-c","echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY) $(log_level)"]
    env:
    - name: SPECIAL_LEVEL_KEY
      valueFrom:
        configMapKeyRef:
          name: special-config
          key: special.how
    - name: SPECIAL_TYPE_KEY
      valueFrom:
        configMapKeyRef:
          name: special-config
          key: special.type
    envFrom:
    - configMapRef:
        name: env-config
  restartPolicy: Never

 通过数据卷插件使用ConfigMap

在数据卷里面使用这个 ConfigMap,有不同的选项。最基本的就是将文件填入数据卷,在这个文件中,键就是文件名,键值就是文件内容

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod2
spec:
  containers:
  - name: test-container2
    image: wangyanglinux/myapp:v1
    command: ["/bin/sh","-c","cat /etc/config/special.how"]
    volumeMounts:
    - name: config-volume   # 使用创建的 volumes
      mountPath: /etc/config
  volumes:
  - name: config-volume  #创建 volumes
    configMap:
      name: special-config
  restartPolicy: Never

ConfigMap的热更新

Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app3
spec:
  selector:
    matchLabels:
      run: myapp3
  replicas: 1
  template:
    metadata:
      labels:
        run: myapp3
    spec:
      containers:
      - name: my-app3
        image: wangyanglinux/myapp:v1
        ports:
        - containerPort: 80
        volumeMounts:
        - name: config-volume
          mountPath: /etc/config
      volumes:
      - name: config-volume
        configMap:
          name: env-config

 

ConfigMap 更新后滚动更新 Pod

更新 ConfigMap 目前并不会触发相关 Pod 的滚动更新,可以通过修改 pod annotations 的方式强制触发滚动更新

kubectl patch deployment my-app3 --patch '{"spec": {"template": {"metadata": {"annotations":{"version/config": "20190411" }}}}}'

 在.spec.template.metadata.annotations中添加version/config,每次通过修改version/config来触发滚动更新.

更新ConfigMap 后:

使用该ConfigMap挂载的Env不会同步更新

使用该ConfigMap挂载的Volume中的数据需要一段时间 才能同步更新

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐