参考:https://www.cnblogs.com/breezey/p/6582082.html
我们知道,在几乎所有的应用开发中,都会涉及到配置文件的变更,比如说在web的程序中,需要连接数据库,缓存甚至是队列等等。而我们的一个应用程序从写第一行代码开始,要经历开发环境、测试环境、预发布环境只到最终的线上环境。而每一个环境都要定义其独立的各种配置。如果我们不能很好的管理这些配置文件,你的运维工作将顿时变的无比的繁琐。为此业内的一些大公司专门开发了自己的一套配置管理中心,如360的Qcon,百度的disconf等。kubernetes也提供了自己的一套方案,即ConfigMap。kubernetes通过ConfigMap来实现对容器中应用的配置管理。
1.创建configmap
1.1 通过yaml创建
我们先来看第一种,在yaml文件中,配置文件以key-value键值对的形式保存,当然也可以直接放一个完整的配置文件,在下面的示例中,cache_hst、cache_port、cache_prefix即是key-value键值对,而app.properties和my.cnf都是配置文件:
[root@k8s-master k8s-objs]# pwd
/root/k8s-objs [root@k8s-master k8s-objs]# cat configmap-test1.yaml apiVersion: v1 kind: ConfigMap metadata: name: test-cfg namespace: default data: cache_host: mysql-k8s cache_port: "33006" cache_prefix: k8s my.cnf: | [mysqld] log-bin = mysql-bin app.properties: | property.1 = value-1 property.2 = value-2 property.3 = value-3 [root@k8s-master k8s-objs]# [root@k8s-master k8s-objs]# kubectl create -f configmap-test1.yaml configmap/test-cfg created [root@k8s-master k8s-objs]# kubectl get configmaps NAME DATA AGE test-cfg 5 85s [root@k8s-master k8s-objs]# kubectl describe pod test-cfg Error from server (NotFound): pods "test-cfg" not found [root@k8s-master k8s-objs]# kubectl describe configmap test-cfg Name: test-cfg Namespace: default Labels: <none> Annotations: <none> Data ==== app.properties: ---- property.1 = value-1 property.2 = value-2 property.3 = value-3 cache_host: ---- mysql-k8s cache_port: ----
33006 cache_prefix: ---- k8s my.cnf: ---- [mysqld] log-bin = mysql-bin Events: <none> [root@k8s-master k8s-objs]#
1.2 通过命令创建configmap
kubectl create configmap test-config3 --from-literal=db.host=10.5.10.116 --from-literal=db.port='3306'
##直接将文件创建为configmap
##kubectl create configmap test-config --from-file=./configs ##将该目录下的所有配置文件创建为configmap
##kubectl create configmap test-config2 --from-file=./configs/db.conf --from-file=./configs/cache.conf
[root@k8s-master k8s-objs]# kubectl create configmap test-config3 --from-literal=db.host=10.5.10.116 --from-literal=db.port='3306' configmap/test-config3 created [root@k8s-master k8s-objs]# kubectl get configmaps NAME DATA AGE test-cfg 5 6m1s test-config3 2 9s [root@k8s-master k8s-objs]# kubectl describe configmap test-cfg3 Error from server (NotFound): configmaps "test-cfg3" not found [root@k8s-master k8s-objs]# kubectl describe configmap test-config3 Name: test-config3 Namespace: default Labels: <none> Annotations: <none> Data ==== db.host: ----
10.5.10.116 db.port: ----
3306 Events: <none> [root@k8s-master k8s-objs]#
查看属性
[root@k8s-master k8s-objs]# kubectl get configmaps -o yaml apiVersion: v1 items: - apiVersion: v1 data: app.properties: | property.1 = value-1 property.2 = value-2 property.3 = value-3 cache_host: mysql-k8s cache_port: "33006" cache_prefix: k8s my.cnf: | [mysqld] log-bin = mysql-bin kind: ConfigMap metadata: creationTimestamp: "2019-04-11T08:25:13Z" name: test-cfg namespace: default resourceVersion: "976815" selfLink: /api/v1/namespaces/default/configmaps/test-cfg uid: 544cc424-5c33-11e9-a5c3-000c291ae345
###第二个configmap - apiVersion: v1 data: db.host: 10.5.10.116 db.port: "3306" kind: ConfigMap metadata: creationTimestamp: "2019-04-11T08:31:05Z" name: test-config3 namespace: default resourceVersion: "977288" selfLink: /api/v1/namespaces/default/configmaps/test-config3 uid: 261ab5e2-5c34-11e9-a5c3-000c291ae345 kind: List metadata: resourceVersion: "" selfLink: ""
2 使用configmap
2.1 作为环境变量
[root@k8s-master k8s-objs]# cat pod-configmap-testenv.yaml apiVersion: v1 kind: Pod metadata: labels: purpose: test-configmap name: testenv spec: containers: - name: test-configmap image: tomcat:8 imagePullPolicy: IfNotPresent #command: [ "/bin/sh", "-c", "echo $(MY_CACHE_HOST)" ] env: - name: MY_CACHE_HOST valueFrom: configMapKeyRef: name: test-cfg key: cache_host [root@k8s-master k8s-objs]# [root@k8s-master k8s-objs]# kubectl create -f pod-configmap-testenv.yaml pod/testenv created
root@k8s-master k8s-objs]# kubectl get pod
NAME READY STATUS RESTARTS AGE
hello-5cd4456b66-gstq6 1/1 Running 1 8d
hello-5cd4456b66-sb5px 1/1 Running 1 8d
testenv 1/1 Running 0 7s
#查看containerid
[root@k8s-master k8s-objs]# kubectl describe pod testenv
Name: testenv
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: k8s-node1/192.168.111.131
Start Time: Fri, 12 Apr 2019 08:28:27 +0800
Labels: purpose=test-configmap
Annotations: <none>
Status: Running
IP: 10.244.1.69
Containers:
test-configmap:
Container ID: docker://7c76ff1602561c498a537ea0bcd3bb0a244a7ef9ec3cdc34b6ba03577d889a93
Image: tomcat:8
Image ID: docker-pullable://tomcat@sha256:3e3d18321127bb9114f4226f95802d3899aeec4c36df84d0359e5da300e9bc72
Port: <none>
Host Port: <none>
State: Running
Started: Fri, 12 Apr 2019 08:28:33 +0800
Ready: True
Restart Count: 0
Environment:
MY_CACHE_HOST: <set to the key 'cache_host' of config map 'test-cfg'> Optional: false
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-92rjn (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-92rjn:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-92rjn
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m49s default-scheduler Successfully assigned default/testenv to k8s-node1
Normal Pulled 2m44s kubelet, k8s-node1 Container image "tomcat:8" already present on machine
Normal Created 2m44s kubelet, k8s-node1 Created container test-configmap
Normal Started 2m43s kubelet, k8s-node1 Started container test-configmap
[root@k8s-master k8s-objs]#
进入kubernetes中的pod的容器中,
kubectl exec -it testenv -n default -- /bin/bash #退出exit
[root@k8s-master k8s-objs]# kubectl exec -it testenv -n default -- /bin/bash ##下面标红表示新的pod主机名,即已成功进入pod root@testenv:/usr/local/tomcat# root@testenv:/usr/local/tomcat# echo $MY_CACHE_HOST #查看环境变量是否生效 mysql-k8s root@testenv:/usr/local/tomcat# exit
2.2 挂载文件数据卷
2.2.1使用volume将ConfigMap作为文件或目录直接挂载,其中每一个key-value键值对都会生成一个文件,key为文件名,value为内容
查看configmap
[root@k8s-master k8s-objs]# kubectl get configmap NAME DATA AGE test-cfg 5 16h test-config3 2 16h [root@k8s-master k8s-objs]# kubectl get configmap test-cfg -o yaml apiVersion: v1 data: app.properties: | property.1 = value-1 property.2 = value-2 property.3 = value-3 cache_host: mysql-k8s cache_port: "33006" cache_prefix: k8s my.cnf: | [mysqld] log-bin = mysql-bin kind: ConfigMap metadata: creationTimestamp: "2019-04-11T08:25:13Z" name: test-cfg namespace: default resourceVersion: "976815" selfLink: /api/v1/namespaces/default/configmaps/test-cfg uid: 544cc424-5c33-11e9-a5c3-000c291ae345 [root@k8s-master k8s-objs]#
创建pod,并挂载volume
[root@k8s-master k8s-objs]# cat pod-configmap-testvolume.yaml apiVersion: v1 kind: Pod metadata: labels: purpose: test-configmap-volume name: testvolume spec: containers: - name: test-configmap-volume image: tomcat:8 imagePullPolicy: IfNotPresent #command: [ "/bin/sh", "-c", "echo $(MY_CACHE_HOST)" ] volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: name: test-cfg [root@k8s-master k8s-objs]# [root@k8s-master k8s-objs]# kubectl create -f pod-configmap-testvolume.yaml pod/testvolume created [root@k8s-master k8s-objs]# kubectl get pod NAME READY STATUS RESTARTS AGE hello-5cd4456b66-gstq6 1/1 Running 1 8d hello-5cd4456b66-sb5px 1/1 Running 1 8d testvolume 1/1 Running 0 113s [root@k8s-master k8s-objs]# kubectl describe pod testvolume Name: testvolume Namespace: default Priority: 0 PriorityClassName: <none> Node: k8s-node1/192.168.111.131 Start Time: Fri, 12 Apr 2019 08:59:31 +0800 Labels: purpose=test-configmap-volume Annotations: <none> Status: Running IP: 10.244.1.70 Containers: test-configmap-volume: Container ID: docker://16de1a9dec62564d6e7e50a3167581e9be8151c388707e8f48f4f5152a0ed83a
Image: tomcat:8 Image ID: docker-pullable://tomcat@sha256:3e3d18321127bb9114f4226f95802d3899aeec4c36df84d0359e5da300e9bc72
Port: <none> Host Port: <none> State: Running Started: Fri, 12 Apr 2019 08:59:34 +0800 Ready: True Restart Count: 0 Environment: <none> Mounts: /etc/config from config-volume (rw) /var/run/secrets/kubernetes.io/serviceaccount from default-token-92rjn (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: config-volume: Type: ConfigMap (a volume populated by a ConfigMap) Name: test-cfg Optional: false default-token-92rjn: Type: Secret (a volume populated by a Secret) SecretName: default-token-92rjn Optional: false QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 2m45s default-scheduler Successfully assigned default/testvolume to k8s-node1 Normal Pulled 2m42s kubelet, k8s-node1 Container image "tomcat:8" already present on machine Normal Created 2m42s kubelet, k8s-node1 Created container test-configmap-volume Normal Started 2m42s kubelet, k8s-node1 Started container test-configmap-volume [root@k8s-master k8s-objs]#
进入kubernetes的pod,
[root@k8s-master k8s-objs]# kubectl get pod NAME READY STATUS RESTARTS AGE hello-5cd4456b66-gstq6 1/1 Running 1 8d hello-5cd4456b66-sb5px 1/1 Running 1 8d testvolume 1/1 Running 0 3m56s [root@k8s-master k8s-objs]# kubectl exec -it testsvolume /bin/bash Error from server (NotFound): pods "testsvolume" not found [root@k8s-master k8s-objs]# kubectl exec -it testvolume /bin/bash root@testvolume:/usr/local/tomcat# cd /etc/config root@testvolume:/etc/config# ls app.properties cache_host cache_port cache_prefix my.cnf root@testvolume:/etc/config# ll bash: ll: command not found root@testvolume:/etc/config# ls -l ##对照发现,使用volume将ConfigMap作为文件或目录直接挂载,其中每一个key-value键值对都会生成一个文件,key为文件名,value为内容 total 0 lrwxrwxrwx 1 root root 21 Apr 12 00:59 app.properties -> ..data/app.properties lrwxrwxrwx 1 root root 17 Apr 12 00:59 cache_host -> ..data/cache_host lrwxrwxrwx 1 root root 17 Apr 12 00:59 cache_port -> ..data/cache_port lrwxrwxrwx 1 root root 19 Apr 12 00:59 cache_prefix -> ..data/cache_prefix lrwxrwxrwx 1 root root 13 Apr 12 00:59 my.cnf -> ..data/my.cnf root@testvolume:/etc/config# cat app.properties property.1 = value-1 property.2 = value-2 property.3 = value-3 root@testvolume:/etc/config# cat cache_host mysql-k8sroot@testvolume:/etc/config# cat cache_port 33006root@testvolume:/etc/config# cat cache_prefix k8sroot@testvolume:/etc/config# cat my.cnf [mysqld] log-bin = mysql-bin root@testvolume:/usr/local/tomcat# echo $cache_host ##并没有自动生成环境变量 root@testvolume:/usr/local/tomcat# root@testvolume:/etc/config# exit
2.2.2 另一种方式,只挂载某个key,并支持相对路径,其中一个key生成的文件路径名和文件名相同
[root@k8s-master k8s-objs]# cat pod-configmap-testvolume.yaml apiVersion: v1 kind: Pod metadata: labels: purpose: test-configmap-volume name: testvolume spec: containers: - name: test-configmap-volume image: tomcat:8 imagePullPolicy: IfNotPresent #command: [ "/bin/sh", "-c", "echo $(MY_CACHE_HOST)" ] volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: name: test-cfg items: - key: cache_host path: path/to/special-key-cache #path中的最后一级“special-key-cache”为文件名 - key: app.properties path: app.properties [root@k8s-master k8s-objs]# kubectl create -f pod-configmap-testvolume.yaml pod/testvolume created [root@k8s-master k8s-objs]# kubectl get pod NAME READY STATUS RESTARTS AGE hello-5cd4456b66-gstq6 1/1 Running 1 8d hello-5cd4456b66-sb5px 1/1 Running 1 8d testvolume 1/1 Running 0 12s [root@k8s-master k8s-objs]# kubectl describe pod testvolume Name: testvolume Namespace: default Priority: 0 PriorityClassName: <none> Node: k8s-node1/192.168.111.131 Start Time: Fri, 12 Apr 2019 09:19:49 +0800 Labels: purpose=test-configmap-volume Annotations: <none> Status: Running IP: 10.244.1.71 Containers: test-configmap-volume: Container ID: docker://227d2ab39823087193f1830309c73af5408c85f634eb5c7fce9c668533766b76
Image: tomcat:8 Image ID: docker-pullable://tomcat@sha256:3e3d18321127bb9114f4226f95802d3899aeec4c36df84d0359e5da300e9bc72
Port: <none> Host Port: <none> State: Running Started: Fri, 12 Apr 2019 09:19:53 +0800 Ready: True Restart Count: 0 Environment: <none> Mounts: /etc/config from config-volume (rw) /var/run/secrets/kubernetes.io/serviceaccount from default-token-92rjn (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: config-volume: Type: ConfigMap (a volume populated by a ConfigMap) Name: test-cfg Optional: false default-token-92rjn: Type: Secret (a volume populated by a Secret) SecretName: default-token-92rjn Optional: false QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 2m27s default-scheduler Successfully assigned default/testvolume to k8s-node1 Normal Pulled 2m24s kubelet, k8s-node1 Container image "tomcat:8" already present on machine Normal Created 2m23s kubelet, k8s-node1 Created container test-configmap-volume Normal Started 2m23s kubelet, k8s-node1 Started container test-configmap-volume [root@k8s-master k8s-objs]#
进入pod
[root@k8s-master k8s-objs]# kubectl get pod NAME READY STATUS RESTARTS AGE hello-5cd4456b66-gstq6 1/1 Running 1 8d hello-5cd4456b66-sb5px 1/1 Running 1 8d testvolume 1/1 Running 0 3m24s [root@k8s-master k8s-objs]# kubectl exec -it testvolume /bin/bash root@testvolume:/usr/local/tomcat# cd /etc/config root@testvolume:/etc/config# ls -l #在目录/etc/config下生成一个文件(/etc/config/app.properties)和一个带path目录的文件(/etc/config/path/to/special-key-cache) total 0 lrwxrwxrwx 1 root root 21 Apr 12 01:19 app.properties -> ..data/app.properties lrwxrwxrwx 1 root root 11 Apr 12 01:19 path -> ..data/path root@testvolume:/etc/config# ls app.properties app.properties root@testvolume:/etc/config# cat app.properties/app.properties cat: app.properties/app.properties: Not a directory root@testvolume:/etc/config# cd app.properties bash: cd: app.properties: Not a directory root@testvolume:/etc/config# cat app.properties ##说明key:app.properties的path项用文件名相当于没有起任何作用 property.1 = value-1 property.2 = value-2 property.3 = value-3 root@testvolume:/etc/config# pwd
/etc/config root@testvolume:/etc/config# cd path/to/special-key-cache bash: cd: path/to/special-key-cache: Not a directory root@testvolume:/etc/config# cd path root@testvolume:/etc/config/path# cd to root@testvolume:/etc/config/path/to# cd special-key-cache bash: cd: special-key-cache: Not a directory root@testvolume:/etc/config/path/to# pwd
/etc/config/path/to root@testvolume:/etc/config/path/to# ls special-key-cache root@testvolume:/etc/config/path/to# cat special-key-cache mysql-k8sroot@testvolume:/etc/config/path/to# exit
configmap在pod容器中成功生成了文件
3 深度解析mountPath,subPath,key,path的关系和作用
结论:
kubernetes key (pod.spec.volums[0].configMap.items[0].key)用于指定configMap中的哪些条目可用于挂载,如2.2.2
kubernetes path (pod.spec.volums[0].configMap.items[0].path)用于将key重命名,如案例3.3
kubernetes suPath (pod.spec.containers[0].volumeMounts.subPath)决定容器中有无挂载(按名字从key,有path时以path为主,中比对是否存在要的条目)。
kubernetes mountPath (pod.spec.containers[0].volumeMounts.mountPath)决定容器中挂载的结果文件名。没有subPath项时如案例2.2.1,此项仅指定路径。有subPath时且subPath筛选结果为true时如案例3.1,此项指定路径和文件名,此时文件名可随意指定如案例3.4和3.5;有subPath但subPath筛选结果为false时如案例3.2,此项指定路径(最后一级目录名为文件名)
3.1 mountPath结合subPath(也可解决多个configmap挂载同一目录,导致覆盖)作用
有subPath时且subPath推荐筛选结果为true,mountPath指定到文件名
注:subPath只是将volume.items.key中多个key进行筛选,只挂载需要的文件,相当于2.2.2中app.properties保留,其它key不挂载
[root@k8s-master k8s-objs]# cat pod-configmap-testvolume.yaml apiVersion: v1 kind: Pod metadata: labels: purpose: test-configmap-volume name: testvolume spec: containers: - name: test-configmap-volume image: tomcat:8 imagePullPolicy: IfNotPresent #command: [ "/bin/sh", "-c", "echo $(MY_CACHE_HOST)" ] volumeMounts: - name: config-volume mountPath: /etc/config/app.properties #此处配合suPath使用时,app.properties为文件名,即pod容器中只生成了/etc/config目录,目录之下 为文件,只有一个名为app.properties的文件(subPath筛选只挂载app.properties文件) subPath: app.properties volumes: - name: config-volume configMap: name: test-cfg items: - key: cache_host path: path/to/special-key-cache - key: app.properties path: app.properties [root@k8s-master k8s-objs]# kubectl create -f pod-configmap-testvolume.yaml pod/testvolume created [root@k8s-master k8s-objs]# kubectl get pod NAME READY STATUS RESTARTS AGE hello-5cd4456b66-gstq6 1/1 Running 1 8d hello-5cd4456b66-sb5px 1/1 Running 1 8d testvolume 1/1 Running 0 17s [root@k8s-master k8s-objs]# [root@k8s-master k8s-objs]# kubectl describe pod testvolume Name: testvolume Namespace: default Priority: 0 PriorityClassName: <none> Node: k8s-node1/192.168.111.131 Start Time: Fri, 12 Apr 2019 09:59:24 +0800 Labels: purpose=test-configmap-volume Annotations: <none> Status: Running IP: 10.244.1.72 Containers: test-configmap-volume: Container ID: docker://ef581eaaf8bd895bce8d4e77f66b8c704b557dee186bf72e00e1aa3dbb7390f4 Image: tomcat:8 Image ID: docker-pullable://tomcat@sha256:3e3d18321127bb9114f4226f95802d3899aeec4c36df84d0359e5da300e9bc72 Port: <none> Host Port: <none> State: Running Started: Fri, 12 Apr 2019 09:59:27 +0800 Ready: True Restart Count: 0 Environment: <none> Mounts: /etc/config/app.properties from config-volume (rw,path="app.properties") /var/run/secrets/kubernetes.io/serviceaccount from default-token-92rjn (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: config-volume: Type: ConfigMap (a volume populated by a ConfigMap) Name: test-cfg Optional: false default-token-92rjn: Type: Secret (a volume populated by a Secret) SecretName: default-token-92rjn Optional: false QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 2m13s default-scheduler Successfully assigned default/testvolume to k8s-node1 Normal Pulled 2m10s kubelet, k8s-node1 Container image "tomcat:8" already present on machine Normal Created 2m10s kubelet, k8s-node1 Created container test-configmap-volume Normal Started 2m10s kubelet, k8s-node1 Started container test-configmap-volume [root@k8s-master k8s-objs]#
进入pod
[root@k8s-master k8s-objs]# kubectl exec -it testvolume /bin/bash root@testvolume:/usr/local/tomcat# cd /etc/config root@testvolume:/etc/config# ls app.properties root@testvolume:/etc/config# ll bash: ll: command not found root@testvolume:/etc/config# ls app.properties root@testvolume:/etc/config# ls -l total 4 -rw-r--r-- 1 root root 63 Apr 12 01:59 app.properties root@testvolume:/etc/config# cd app.properties bash: cd: app.properties: Not a directory root@testvolume:/etc/config# cat app.properties property.1 = value-1 property.2 = value-2 property.3 = value-3 root@testvolume:/etc/config#
3.2有subPath但筛选结果为false,
容器中生成一个空目录/etc/config/app.properties,无文件
解析。subPath筛选范围优先级为pod.spec.volums[0].configMap.items[0].path>pod.spec.volums[0].configMap.items[0].key>configMap.key,本例中为path,即在path指定的条目【“cache_host”,"app-properties "注意中间是横杠不是点】找是否有subPath项“app.properties”注意中间为点,查找结果为false,所以无文件挂载。容器将“/etc/config/app.properties”当成一个待创建的路径。
[root@k8s-master k8s-objs]# vi pod-configmap-testvolume.yaml apiVersion: v1 kind: Pod metadata: labels: purpose: test-configmap-volume name: testvolume spec: containers: - name: test-configmap-volume image: tomcat:8 imagePullPolicy: IfNotPresent #command: [ "/bin/sh", "-c", "echo $(MY_CACHE_HOST)" ] volumeMounts: - name: config-volume mountPath: /etc/config/app.properties #此时此处app.properties为文件名 subPath: app.properties volumes: - name: config-volume configMap: name: test-cfg items: - key: cache_host path: path/to/special-key-cache - key: app.properties path: app-properties #此处path相当于更改文件名mv app.properties app-properties
[root@k8s-master k8s-objs]# kubectl exec -it testvolume /bin/bash root@testvolume:/usr/local/tomcat# cd /etc/config root@testvolume:/etc/config# ls app.properties root@testvolume:/etc/config# ll #此目录下只有一个子目录 bash: ll: command not found root@testvolume:/etc/config# ls -l total 0 drwxrwxrwx 2 root root 6 Apr 12 02:11 app.properties root@testvolume:/etc/config# cat app.properties cat: app.properties: Is a directory root@testvolume:/etc/config# cd app.properties root@testvolume:/etc/config/app.properties# ls root@testvolume:/etc/config/app.properties# ls #此目录下为空 root@testvolume:/etc/config/app.properties#
3.3无 subPath,path相当于重命名
[root@k8s-master k8s-objs]# cat pod-configmap-testvolume.yaml apiVersion: v1 kind: Pod metadata: labels: purpose: test-configmap-volume name: testvolume spec: containers: - name: test-configmap-volume image: tomcat:8 imagePullPolicy: IfNotPresent #command: [ "/bin/sh", "-c", "echo $(MY_CACHE_HOST)" ] volumeMounts: - name: config-volume mountPath: /etc/config/app.properties ##此处app.properties为目录 # subPath: app.properties volumes: - name: config-volume configMap: name: test-cfg items: - key: cache_host path: path/to/special-key-cache - key: app.properties path: app-properties #此处path相当于更改文件名mv app.properties app-properties [root@k8s-master k8s-objs]# [root@k8s-master k8s-objs]# kubectl exec -it testvolume /bin/bash root@testvolume:/usr/local/tomcat# ls BUILDING.txt CONTRIBUTING.md LICENSE NOTICE README.md RELEASE-NOTES RUNNING.txt bin conf include lib logs native-jni-lib temp webapps work root@testvolume:/usr/local/tomcat# cd /etc/config root@testvolume:/etc/config# ls app.properties root@testvolume:/etc/config# ls -l total 0 drwxrwxrwx 3 root root 93 Apr 12 02:20 app.properties root@testvolume:/etc/config# cd app.properties root@testvolume:/etc/config/app.properties# ls app-properties path root@testvolume:/etc/config/app.properties# ls -l total 0 lrwxrwxrwx 1 root root 21 Apr 12 02:20 app-properties -> ..data/app-properties lrwxrwxrwx 1 root root 11 Apr 12 02:20 path -> ..data/path root@testvolume:/etc/config/app.properties# cat app-properties property.1 = value-1 property.2 = value-2 property.3 = value-3 root@testvolume:/etc/config/app.properties# cat path cat: path: Is a directory root@testvolume:/etc/config/app.properties# cd path root@testvolume:/etc/config/app.properties/path# ls to root@testvolume:/etc/config/app.properties/path# cd to root@testvolume:/etc/config/app.properties/path/to# ls -l total 4 -rw-r--r-- 1 root root 9 Apr 12 02:20 special-key-cache root@testvolume:/etc/config/app.properties/path/to# cat special-key-cache mysql-k8sroot@testvolume:/etc/config/app.properties/path/to#
3.4有subPath且筛选结果为true,mouthPath指定文件名,可以和subPath不一样
[root@k8s-master k8s-objs]# vi pod-configmap-testvolume.yaml apiVersion: v1 kind: Pod metadata: labels: purpose: test-configmap-volume name: testvolume spec: containers: - name: test-configmap-volume image: tomcat:8 imagePullPolicy: IfNotPresent #command: [ "/bin/sh", "-c", "echo $(MY_CACHE_HOST)" ] volumeMounts: - name: config-volume mountPath: /etc/config/app.properties #此处app.properties为文件名 subPath: app-properties #改为与mountPath不一样的文件名时,subPath相当于通过volume筛选configMap中的key(此处为因volumes.configMap.items.path对key进行了重命名)中的条目,即存在subPath时,subPath决定有无,mountPath决定文件名 volumes: - name: config-volume configMap: name: test-cfg items: - key: cache_host path: path/to/special-key-cache - key: app.properties path: app-properties [root@k8s-master k8s-objs]# kubectl exec -it testvolume /bin/bash root@testvolume:/usr/local/tomcat# cd /etc/config root@testvolume:/etc/config# ls app.properties root@testvolume:/etc/config# ls -l total 4 -rw-r--r-- 1 root root 63 Apr 12 02:43 app.properties root@testvolume:/etc/config# cat app.properties property.1 = value-1 property.2 = value-2 property.3 = value-3 root@testvolume:/etc/config# pwd /etc/config root@testvolume:/etc/config# ls app.properties root@testvolume:/etc/config# exit
3.5有subPath且筛选结果为true,mouthPath指定文件名,可以和subPath不一样,甚至随意指定为z.txt
[root@k8s-master k8s-objs]# vi pod-configmap-testvolume.yaml apiVersion: v1 kind: Pod metadata: labels: purpose: test-configmap-volume name: testvolume spec: containers: - name: test-configmap-volume image: tomcat:8 imagePullPolicy: IfNotPresent #command: [ "/bin/sh", "-c", "echo $(MY_CACHE_HOST)" ] volumeMounts: - name: config-volume mountPath: /etc/config/z.txt #subPath决定有无,mountPath决定文件名为z.txt subPath: app-properties volumes: - name: config-volume configMap: name: test-cfg items: - key: cache_host path: path/to/special-key-cache - key: app.properties path: app-properties [root@k8s-master k8s-objs]# kubectl exec -it testvolume /bin/bash root@testvolume:/usr/local/tomcat# cd /etc/config root@testvolume:/etc/config# ls z.txt root@testvolume:/etc/config# pwd /etc/config root@testvolume:/etc/config# cat z.txt property.1 = value-1 property.2 = value-2 property.3 = value-3 root@testvolume:/etc/config#