前言:
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。
k create cm cm-test1 --from-literal=env1=CSDN -n dev --dry-run=client -o yaml >cm-test1.yaml k apply -f cm-test1.yaml
将此configMap定义的变量挂载到pod内,这个pod是tomcat的:
apiVersion: apps/v1 kind: Deployment metadata: name: tomcat-deployment namespace: dev spec: replicas: 1 selector: matchLabels: app: tomcat-pod template: metadata: labels: app: tomcat-pod spec: containers: - name: tomcat image: tomcat:8.5-jre10-slim env: - name: FUCK #在容器内的变量名称 valueFrom: configMapKeyRef: name: cm-test1 #configMap的名称,上面的cm文件定义的名称 key: env1 #要使用的key的值,上面只定义了这一个key
简单的验证:
部署这个tomcat的pod,等待pod启动正常如下:
[root@k8s-master ~]# k get po -A -owide NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES default mysql-5.7.23-5f9bd6468c-h4gqn 1/1 Running 4 3d6h 10.244.169.130 k8s-node2 <none> <none> dev nginx-deployment-b785b4498-s26js 1/1 Running 0 5h2m 10.244.36.73 k8s-node1 <none> <none> dev tomcat-deployment-789b44ffcd-z7bfx 1/1 Running 0 15m 10.244.169.142 k8s-node2 <none> <none>
进入pod,打印变量值,验证无误。
[root@k8s-master ~]# k exec -it tomcat-deployment-789b44ffcd-z7bfx -n dev -- /bin/bash root@tomcat-deployment-789b44ffcd-z7bfx:/usr/local/tomcat# env |grep CSDN FUCK=CSDN root@tomcat-deployment-789b44ffcd-z7bfx:/usr/local/tomcat# echo $FUCK CSDN
(2)命令行方式通过文件生成configMap,使用Volume将ConfigMap作为文件或目录挂载:
例如,现有一个tomcat,将首页index.jsp 更改后挂载到pod内,实现首页的变更
首页文件模板:
[root@k8s-master ~]# cat index.jsp this is a test page!!!!! this is a test page!!!!! this is a test page!!!!! this is a test page!!!!!
生成configMap,这里叫cm-test ,namespace指定的是dev,最后执行此文件。
k create configmap cm-test --from-file=index.jsp -n dev -oyaml --dry-run=client [root@k8s-master ~]# cat cm-test.yaml apiVersion: v1 kind: ConfigMap metadata: name: cm-test namespace: dev data: index.jsp: | this is a test page!!!!! this is a test page!!!!! this is a test page!!!!! this is a test page!!!!! this is a test page!!!!! sfsds this is a test page!!!!! k apply -f cm-test.yaml
pod引用此configMap:
apiVersion: apps/v1 kind: Deployment metadata: name: tomcat-deployment namespace: dev spec: replicas: 1 selector: matchLabels: app: tomcat-pod template: metadata: labels: app: tomcat-pod spec: containers: - name: tomcat image: tomcat:8.5-jre10-slim volumeMounts: - name: conf mountPath: /usr/local/tomcat/webapps/ROOT/index.jsp#由于这个版本的tomcat此目录下有其它文件,为防止被覆盖,因此设置subPath subPath: index.jsp ports: - containerPort: 8080 volumes: - name: conf configMap: name: cm-test items: - key: index.jsp #key不能写错,cm文件里定义的就是这个 path: index.jsp #挂载在容器后叫什么文件名 nodeName: k8s-node2
(3)命令行方式,通过文件夹生成configMap清单文件(test文件夹内有三个文件):
echo hello > test/hello.txt echo world > test/world.txt cat test/index.jsp this is a test page!!!!! this is a test page!!!!! this is a test page!!!!! this is a test page!!!!! k create cm cm-test3 --from-file=test/ --dry-run=client -o yaml > cm-test3.yaml
查看命令生成的文件的内容:
[root@k8s-master ~]# cat cm-test3.yaml apiVersion: v1 data: hello.txt: | hello index.jsp: | this is a test page!!!!! this is a test page!!!!! this is a test page!!!!! this is a test page!!!!! world.txt: | world kind: ConfigMap metadata: creationTimestamp: null name: cm-test3
configMap的调用还是一样的,要么挂载为文件,要么作为环境变量,和它是由文件还是文件夹还是字符串生成没有什么特别的联系,该怎么调用就怎么调用。
例如,集群的coredns使用的configMap:
[root@k8s-master ~]# k get cm -A NAMESPACE NAME DATA AGE kube-system calico-config 4 2d13h kube-system coredns 1 2d21h kube-system extension-apiserver-authentication 6 3d2h