当前位置:   article > 正文

k8s之ConfigMap详细理解及使用

configmap

一、ConfigMap介绍

ConfigMap是一种API对象,用来将非加密数据保存到键值对中。可以用作环境变量、命令行参数或者存储卷中的配置文件。

ConfigMap可以将环境变量配置信息和容器镜像解耦,便于应用配置的修改。如果需要存储加密信息时可以使用Secret对象。

二、ConfigMap创建

1.通过命令行创建configmap

 可以使用 kubectl create configmap 从文件、目录或者 key-value 字符串创建等创建 ConfigMap

 (1)通过文件创建configmap

  1. $ echo hello > test1.txt
  2. $ ehco world > test2.txt
  3. $ kubectl create configmap my-config --from-file=key1=test1.txt --from-file=key2=test2.txt
  4. $ kubectl describe configmap my-config

看到该configmap中有两个键值对,key1:hello 和 key2:world

(2)通过文件夹创建configmap

  1. $ mkdir config
  2. $ echo hello > config/test1
  3. $ echo world > config/test2

#根据文件夹创建configmap

$ kubectl create configmap dir-config --from-file=config/

$ kubectl describe configmap dir-config

看到该configmap资源中有两个键值对,test1:hello和test2:world,key为文件名,value为文件内容

(3)通过键值对创建configmap

$ kubectl create configmap literal-config --from-literal=key1=hello --from-literal=key2=world

$ kubectl describe configmap literal-config

2.通过yaml文件创建

  1. #config.yaml
  2. apiVersion: v1
  3. kind: ConfigMap
  4. metadata:
  5. name: my-config
  6. data:
  7. key1: hello
  8. key2: world

$ kubectl create -f config.yaml

$ kubectl describe configmap my-config

三、ConfigMap的使用

Pod的使用方式:

1. 将ConfigMap中的数据设置为容器的环境变量

2. 将ConfigMap中的数据设置为命令行参数

3. 使用Volume将ConfigMap作为文件或目录挂载

4. 编写代码在 Pod 中运行,使用 Kubernetes API 来读取 ConfigMap

1.配置到容器的环境变量

  1. # test-pod-configmap.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. name: test-pod-configmap
  6. spec:
  7. containers:
  8. - name: test-busybox
  9. image: busybox
  10. imagePullPolicy: IfNotPresent
  11. args:
  12. - sleep
  13. - "86400"
  14. env:
  15. - name: KEY1
  16. valueFrom:
  17. configMapKeyRef:
  18. name: my-config
  19. key: key1
  20. - name: KEY2
  21. valueFrom:
  22. configMapKeyRef:
  23. name: my-config
  24. key: key2

#创建pod

$ kubectl create -f test-pod-configmap.yaml

2. 设置为命令行参数

  1. # test-pod-configmap-cmd
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. name: test-pod-configmap-cmd
  6. spec:
  7. containers:
  8. - name: test-busybox
  9. image: busybox
  10. imagePullPolicy: IfNotPresent
  11. command: [ "/bin/sh","-c","echo $(KEY1) $(KEY2)"]
  12. env:
  13. - name: KEY1
  14. valueFrom:
  15. configMapKeyRef:
  16. name: my-config
  17. key: key1
  18. - name: KEY2
  19. valueFrom:
  20. configMapKeyRef:
  21. name: my-config
  22. key: key2
  23. restartPolicy: Never

# 创建pod,该pod成功启动后会输出环境变量KEY1和KEY2的值

$ kubectl create -f test-pod-configmap-cmd.yaml              

#查看pod的日志

$ kubectl logs test-pod-configmap-cmd

#进入到容器中查看环境变量

$ kubectl exec -it test-pod-configmap -- /bin/sh

3.将configmap挂载到容器中

  1. # test-pod-projected-configmap-volume.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. name: test-pod-projected-configmap-volume
  6. spec:
  7. containers:
  8. - name: test-pod-busybox
  9. image: busybox
  10. imagePullPolicy: IfNotPresent
  11. args:
  12. - sleep
  13. - "86400"
  14. volumeMounts:
  15. - name: config-volume
  16. mountPath: "/projected-volume"
  17. readOnly: true
  18. volumes:
  19. - name: config-volume
  20. projected:
  21. sources:
  22. - configMap:
  23. name: my-config

#创建pod

kubectl create -f test-pod-projected-configmap-volume.yaml

#进入容器

$ kubectl exec -it test-pod-projected-configmap-volume -- /bin/sh

#查看挂在到容器中的文件内容

通过volume挂载和环境变量的区别

通过Volume挂载到容器内部时,当该configmap的值发生变化时,容器内部具备自动更新的能力,但是通过环境变量设置到容器内部该值不具备自动更新的能力。

注意:

ConfigMap必须在Pod使用它之前创建

使用envFrom时,将会自动忽略无效的键

Pod只能使用同一个命名空间的ConfigMap

感谢:

https://kubernetes.io/zh/docs/concepts/configuration/configmap/

k8s -- ConfigMap - 简书

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/364837
推荐阅读
相关标签
  

闽ICP备14008679号