參考:http://www.javashuo.com/article/p-mzvdomkb-ex.htmlhtml
咱們知道,在幾乎全部的應用開發中,都會涉及到配置文件的變動,好比說在web的程序中,須要鏈接數據庫,緩存甚至是隊列等等。而咱們的一個應用程序從寫第一行代碼開始,要經歷開發環境、測試環境、預發佈環境只到最終的線上環境。而每個環境都要定義其獨立的各類配置。若是咱們不能很好的管理這些配置文件,你的運維工做將頓時變的無比的繁瑣。爲此業內的一些大公司專門開發了本身的一套配置管理中心,如360的Qcon,百度的disconf等。kubernetes也提供了本身的一套方案,即ConfigMap。kubernetes經過ConfigMap來實現對容器中應用的配置管理。node
咱們先來看第一種,在yaml文件中,配置文件以key-value鍵值對的形式保存,固然也能夠直接放一個完整的配置文件,在下面的示例中,cache_hst、cache_port、cache_prefix便是key-value鍵值對,而app.properties和my.cnf都是配置文件:mysql
[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]#
kubectl create configmap test-config3 --from-literal=db.host=10.5.10.116 --from-literal=db.port='3306'web
##直接將文件建立爲configmapsql
##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]#
查看屬性docker
[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: ""
[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
api
[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
查看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,並掛載volumetomcat
[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
[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容器中成功生成了文件
結論:
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,此項指定路徑(最後一級目錄名爲文件名)
有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#
容器中生成一個空目錄/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#
[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#
[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
[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#