关于kubernetes:Kubernetes存储之ConfigMap

39次阅读

共计 3565 个字符,预计需要花费 9 分钟才能阅读完成。

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)能力同步更新

正文完
 0