Kubernetes存储之ConfigMap

Posted Yuan_sr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Kubernetes存储之ConfigMap相关的知识,希望对你有一定的参考价值。

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

ConfigMap的创建

1.使用目录创建

$ ls docs/user-guide/configmap/kubectl/
game.properties
ui.properties

$ cat docs/user-guide/configmap/kubectl/game.properties
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
enemies.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30

$ cat docs/user-guide/configmap/kubectl/ui.properties
color.good=purple
color.bad=yellow
aoolw.textmode=true
how.nice.to.look=fairlyNice

$ kubectl create configmap game-config --from-file=docs/user-guide/configmap/kubectl

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

2.使用文件创建

$ kubectl create configmap game-config --from-file=docs/user-guide/configmap/kubectl/game.properties
$ kubectl get configmaps game-config -o yaml

3.使用字面值创建

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

$ kubectl create configmap special-config --from-literal=special.how --from-literal=special.type=charm
$ kubectl get configmaps special-config -o yaml

Pod中使用ConfigMap

  1. 使用ConfigMap来替代环境变量
apiVersion: v1
kind: ConfigMap
metadata:
  name :special-config
  namespace: default
data:
  special.how: very
  special.type.charm
apiVersion: v1
kind: ConfigMap
metadata:
  name: env-config
  namespace: default
data:
  log_level: INFO
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: myapp:v1
      command: ["/bin/sh","-c","env"]
      env:
        - name: SPECIAL_LEVEL_KEY
          calueFrom:
            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
  1. 用ConfigMap设置命令行参数
apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  special.how: very
  special.type: charm
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: myapp:v1
      command: ["/bin/sh", "-c", "echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)"]
      env:
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: special.how
        - name: SPECIAL_TYPE_KEY
          calueFrom:
            configMapKeyRef:
              name: special-config
              key: special.type
  restartPolicy: Never
  1. 通过数据卷插件使用ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: dafault
data:
  special.how: very
  special.type: charm

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

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: myapp:v1
      command: ["/bin/sh","-c","cat /etc/config/special.how"]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: special-config
  restartPolicy: Never

ConfigMap的热更新

apiVersion: v1
kind: ConfigMap
metadata:
  name: log-config
  namespace: default
data:
  log_level: INFO
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-nginx
spec:
  replicas: 1
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: myapp:v1
        ports:
        - containerPort: 80
        volumeMounts:
        - name: config-volume
        mountPath: /etc/config
      volumes:
        - name: config-volume
          configMap:
            name: log-config
$ kubectl exec \'kubectl get pods -l run-my-nginx -o=name | cut -d "/" -f2\' --it -- cat /etc/config/log_level
INFO

修改ConfigMap

$ kubectl edit configmap log-config

修改log_level 的值为DEBUG 等待大约10秒,再次查看环境变量的值

$ kubectl exec \'kubectl get pods -l run-my-nginx -o=name | cut -d "/" -f2\' --it -- cat /etc/config/log_level
DEBUG

注意: ConfigMap如果以ENV的方式挂载至容器,修改ConfigMap并不会实现热更新

ConfigMap更新后滚动更新Pod

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

$ kubectl patch deployment my-nginx --patch \'{"spec": {"template": {"metadata": {"annotations":{"version/config": "20210516"}}}}}\'

更新ConfigMap后:

  • 使用该ConfigMap挂载的ENV不会同步更新
  • 使用该ConfigMap挂载的Volume中的数据需要一段时间(10s)才能同步更新

以上是关于Kubernetes存储之ConfigMap的主要内容,如果未能解决你的问题,请参考以下文章

Kubernetes之ConfigMap和Secret

Kubernetes之ConfigMap配置

Kubernetes 数据持久化之Configmap

Linux学习-Kubernetes之Secret和ConfigMap

Linux学习-Kubernetes之Secret和ConfigMap

Kubernetes集群Configmap配置存储资源(三十六)