当前位置:   article > 正文

云原生|kubernetes|关于configMap的一些学习_configmap etcd

configmap etcd

前言:

configMap顾名思义--配置文件集合。主要作用是:

configmap是k8s中的应用配置管理方案,在configmap中,各个配置项都是以key-value的方式存在的,value的数据可以是一个配置文件的内容,这些配置项被保存在k8s使用的持久化存储etcd中。

这样就形成了一个k8s中的配置中心,可以独立的对configmap中的数据进行修改,然后将configmap挂载到pod中进行使用,可以以env的方式,也可以以配置文件的方式在pod中进行引用。这样配置和pod就实现了解耦,都是k8s中独立的资源对象了。




configMap的引用形式

configMap没有什么特别的类型就一种,主要作用是:

  • 将ConfigMap中的数据设置为环境变量
  • 将ConfigMap中的数据设置为命令行参数
  • 使用Volume将ConfigMap作为文件或目录挂载

(1)

将configMap中的数据设置为环境变量

命令行生成configMap文件cm-test1.yaml,其中定义了变量env1(字符串形式),它的值是CSDN。

  1. k create cm cm-test1 --from-literal=env1=CSDN -n dev --dry-run=client -o yaml >cm-test1.yaml
  2. k apply -f cm-test1.yaml

 将此configMap定义的变量挂载到pod内,这个pod是tomcat的:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: tomcat-deployment
  5. namespace: dev
  6. spec:
  7. replicas: 1
  8. selector:
  9. matchLabels:
  10. app: tomcat-pod
  11. template:
  12. metadata:
  13. labels:
  14. app: tomcat-pod
  15. spec:
  16. containers:
  17. - name: tomcat
  18. image: tomcat:8.5-jre10-slim
  19. env:
  20. - name: FUCK #在容器内的变量名称
  21. valueFrom:
  22. configMapKeyRef:
  23. name: cm-test1 #configMap的名称,上面的cm文件定义的名称
  24. key: env1 #要使用的key的值,上面只定义了这一个key

简单的验证:

部署这个tomcat的pod,等待pod启动正常如下:


  1. [root@k8s-master ~]# k get po -A -owide
  2. NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  3. default mysql-5.7.23-5f9bd6468c-h4gqn 1/1 Running 4 3d6h 10.244.169.130 k8s-node2 <none> <none>
  4. dev nginx-deployment-b785b4498-s26js 1/1 Running 0 5h2m 10.244.36.73 k8s-node1 <none> <none>
  5. dev tomcat-deployment-789b44ffcd-z7bfx 1/1 Running 0 15m 10.244.169.142 k8s-node2 <none> <none>

 进入pod,打印变量值,验证无误。

  1. [root@k8s-master ~]# k exec -it tomcat-deployment-789b44ffcd-z7bfx -n dev -- /bin/bash
  2. root@tomcat-deployment-789b44ffcd-z7bfx:/usr/local/tomcat# env |grep CSDN
  3. FUCK=CSDN
  4. root@tomcat-deployment-789b44ffcd-z7bfx:/usr/local/tomcat# echo $FUCK
  5. CSDN

(2)

命令行方式通过文件生成configMap,使用Volume将ConfigMap作为文件或目录挂载:

例如,现有一个tomcat,将首页index.jsp 更改后挂载到pod内,实现首页的变更

首页文件模板:

  1. [root@k8s-master ~]# cat index.jsp
  2. this is a test page!!!!!
  3. this is a test page!!!!!
  4. this is a test page!!!!!
  5. this is a test page!!!!!

生成configMap,这里叫cm-test ,namespace指定的是dev,最后执行此文件。

  1. k create configmap cm-test --from-file=index.jsp -n dev -oyaml --dry-run=client
  2. [root@k8s-master ~]# cat cm-test.yaml
  3. apiVersion: v1
  4. kind: ConfigMap
  5. metadata:
  6. name: cm-test
  7. namespace: dev
  8. data:
  9. index.jsp: |
  10. this is a test page!!!!!
  11. this is a test page!!!!!
  12. this is a test page!!!!!
  13. this is a test page!!!!!
  14. this is a test page!!!!!
  15. sfsds this is a test page!!!!!
  16. k apply -f cm-test.yaml

 

pod引用此configMap:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: tomcat-deployment
  5. namespace: dev
  6. spec:
  7. replicas: 1
  8. selector:
  9. matchLabels:
  10. app: tomcat-pod
  11. template:
  12. metadata:
  13. labels:
  14. app: tomcat-pod
  15. spec:
  16. containers:
  17. - name: tomcat
  18. image: tomcat:8.5-jre10-slim
  19. volumeMounts:
  20. - name: conf
  21. mountPath: /usr/local/tomcat/webapps/ROOT/index.jsp#由于这个版本的tomcat此目录下有其它文件,为防止被覆盖,因此设置subPath
  22. subPath: index.jsp
  23. ports:
  24. - containerPort: 8080
  25. volumes:
  26. - name: conf
  27. configMap:
  28. name: cm-test
  29. items:
  30. - key: index.jsp #key不能写错,cm文件里定义的就是这个
  31. path: index.jsp #挂载在容器后叫什么文件名
  32. nodeName: k8s-node2

 (3)

命令行方式,通过文件夹生成configMap清单文件(test文件夹内有三个文件):


  1. echo hello > test/hello.txt
  2. echo world > test/world.txt
  3. cat test/index.jsp
  4. this is a test page!!!!!
  5. this is a test page!!!!!
  6. this is a test page!!!!!
  7. this is a test page!!!!!
  8. k create cm cm-test3 --from-file=test/ --dry-run=client -o yaml > cm-test3.yaml

 查看命令生成的文件的内容:

  1. [root@k8s-master ~]# cat cm-test3.yaml
  2. apiVersion: v1
  3. data:
  4. hello.txt: |
  5. hello
  6. index.jsp: |
  7. this is a test page!!!!!
  8. this is a test page!!!!!
  9. this is a test page!!!!!
  10. this is a test page!!!!!
  11. world.txt: |
  12. world
  13. kind: ConfigMap
  14. metadata:
  15. creationTimestamp: null
  16. name: cm-test3

configMap的调用还是一样的,要么挂载为文件,要么作为环境变量,和它是由文件还是文件夹还是字符串生成没有什么特别的联系,该怎么调用就怎么调用。 

 

例如,集群的coredns使用的configMap:

  1. [root@k8s-master ~]# k get cm -A
  2. NAMESPACE NAME DATA AGE
  3. kube-system calico-config 4 2d13h
  4. kube-system coredns 1 2d21h
  5. kube-system extension-apiserver-authentication 6 3d2h

coredns的configMap的详细内容: 

  1. [root@k8s-master ~]# cat coredns/coredns-cm.yaml
  2. apiVersion: v1
  3. kind: ConfigMap
  4. metadata:
  5. name: coredns
  6. namespace: kube-system
  7. data:
  8. Corefile: | #这个就是key值了 Corefile
  9. .:53 {
  10. errors
  11. log
  12. health
  13. kubernetes cluster.local 10.254.0.0/18
  14. forward . /etc/resolv.conf
  15. cache 30
  16. }
conredns调用它的configMap:
  1. #无关部分省略了
  2. containers:
  3. - name: coredns
  4. image: registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.7.0
  5. imagePullPolicy: IfNotPresent
  6. args: [ "-conf", "/etc/coredns/Corefile" ]
  7. volumeMounts:
  8. - name: config-volume
  9. mountPath: /etc/coredns
  10. ports:
  11. - containerPort: 53
  12. name: dns
  13. protocol: UDP
  14. - containerPort: 53
  15. name: dns-tcp
  16. protocol: TCP
  17. #一些存活探针什么的也省略了
  18. volumes:
  19. - name: config-volume
  20. configMap:
  21. name: coredns
  22. items:
  23. - key: Corefile #coredns-cm.yaml文件里定义的
  24. path: Corefile #挂载的文件名称



总结:

configMap和secret是比较类似的,作用基本相同,都是对于kubernetes集群内的配置文件解耦,调用方法也基本类似,都可以通过volume挂载方式直接挂到pod相关的容器内部,也可以作为系统环境变量注入到pod相关的容器内。都可以被多个pod同时调用,比如,Apod调用了名称为B的configMap的某个变量C,Dpod也可以调用BconfigMap的变量C,Epod当然也可以,以此类推。

只是挂载为文件的时候需要注意一点,如果挂载目标路径有文件,那么,挂载文件的时候将会覆盖,如果不想覆盖,比如,挂载到pod的容器的/etc目录下,这个时候肯定不希望覆盖了,如果覆盖容器可能都启动不了,就这个subPath的情况我专门做一下解释:

文件夹+文件的情形:

此时的容器内将会有 /etc/index.jsp 这个文件夹,此文件夹下有index.html 这个文件,也就是最终容器内有/etc/index.jsp/index.html这个文件。

  1. volumeMounts:
  2. - name: conf
  3. mountPath: /etc/index.jsp
  4. # subPath: index.html
  5. ports:
  6. - containerPort: 8080
  7. volumes:
  8. - name: conf
  9. configMap:
  10. name: cm-test
  11. items:
  12. - key: index.jsp
  13. path: index.html #挂载在容器后叫什么文件名

 覆盖的情形:

此时的pod启动不了,启动失败,因为etc目录被覆盖了,/etc/目录下就只有一个index.html 文件了。

  1. volumeMounts:
  2. - name: conf
  3. mountPath: /etc/
  4. # subPath: index.html
  5. ports:
  6. - containerPort: 8080
  7. volumes:
  8. - name: conf
  9. configMap:
  10. name: cm-test
  11. items:
  12. - key: index.jsp
  13. path: index.html #挂载在容器后叫什么文件名

正确的subPath挂载情形:

此时是挂载的文件,没有任何目录,文件名称是index.jsp,注意,这里使用了subPath,

 

  1. volumeMounts:
  2. - name: conf
  3. mountPath: /etc/index.jsp
  4. subPath: index.html
  5. ports:
  6. - containerPort: 8080
  7. volumes:
  8. - name: conf
  9. configMap:
  10. name: cm-test
  11. items:
  12. - key: index.jsp
  13. path: index.html #挂载在容器后叫什么文件名
  1. root@tomcat-deployment-d74966946-f8kpm:/etc# ls -al index.jsp
  2. -rw-r--r-- 1 root root 156 Oct 12 12:55 index.jsp

 

不能正确挂载configMap的情形:

subPath和path修改的不一样了,此时没有覆盖,但只有/etc/index.jsp文件夹,cm的内容是完全找不到的。 

  1. volumeMounts:
  2. - name: conf
  3. mountPath: /etc/index.jsp
  4. subPath: index.html
  5. ports:
  6. - containerPort: 8080
  7. volumes:
  8. - name: conf
  9. configMap:
  10. name: cm-test
  11. items:
  12. - key: index.jsp
  13. path: index.jsp #挂载在容器后叫什么文件名
  1. root@tomcat-deployment-6dc7fc8cbd-v6wcp:/etc# ls -al index.jsp/
  2. total 0
  3. drwxrwxrwx 2 root root 6 Oct 12 13:00 .
  4. drwxr-xr-x 1 root root 23 Oct 12 13:00 ..

强烈推荐的做法:

是一直使用subPath并且subPath和path保持一致(注意了,注意了,这种subPath是推荐使用的,也应该一直使用的方法哦):

  1. volumeMounts:
  2. - name: conf
  3. mountPath: /etc/index.jsp
  4. subPath: index.jsp
  5. ports:
  6. - containerPort: 8080
  7. volumes:
  8. - name: conf
  9. configMap:
  10. name: cm-test
  11. items:
  12. - key: index.jsp
  13. path: index.jsp #挂载在容器后叫什么文件名

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

闽ICP备14008679号