VolumeMount浅析

2020-03-09 13:53:10 浏览数 (1)

代码语言:javascript复制
     volumeMounts:
        - name: config-volume
          mountPath: /path/dir/file  //代表要挂载到容器的位置
          subPath: items.path   //代表选择要挂载的项,对应items.path的值,如果不指定代表全挂
  volumes:
    - name: config-volume
      configMap:
         name: test-cfg     //被挂载的configmap name
         items:
           - key: key_name   //configmap里面的key的名字
             path: path/file_name或者file_name //代表key的别名,volumeMounts.subpath匹配key的别名,也代表挂载后的路径或文件名

subPathExpr能根据pod名字来挂载,如将/var/log/pods/xxx/ 挂载到pod的/logs下:

代码语言:javascript复制
apiVersion: v1
kind: Pod
metadata:
  name: pod1
spec:
  containers:
  - name: container1
    env:
    - name: POD_NAME
      valueFrom:
        fieldRef:
          apiVersion: v1
          fieldPath: metadata.name
    image: busybox
    command: [ "sh", "-c", "while [ true ]; do echo 'Hello'; sleep 10; done | tee -a /logs/hello.txt" ]
    volumeMounts:
    - name: workdir1
      mountPath: /logs
      subPathExpr: $(POD_NAME)
  restartPolicy: Neve
  volumes:
  - name: workdir1
    hostPath:
      path: /var/log/pods

# ll /var/log/pods/default_pod1_bf63569f-3155-4ab4-808e-b8d72b608431/container1/0.log lrwxrwxrwx 1 root root 165 Mar 9 11:37 /var/log/pods/default_pod1_bf63569f-3155-4ab4-808e-b8d72b608431/container1/0.log -> /var/lib/docker/containers/77bfd2e8b05f3177e42381cda6d521ec3b74c1c454efb0cf3982996505c7dd59/77bfd2e8b05f3177e42381cda6d521ec3b74c1c454efb0cf3982996505c7dd59-json.log

相关增强功能:

https://github.com/kubernetes/enhancements/issues/559

示例configmap

代码语言:javascript复制
apiVersion: v1
data:
  app.properties: |
    property.1 = value-1
    property.2 = value-2
    property.3 = value-3
  cache_host: mysql-k8s
kind: ConfigMap
metadata:
  name: test-cfg

1使用volume将ConfigMap作为文件或目录直接挂载,其中每一个key-value键值对都会生成一个文件,key为文件名,value为内容

代码语言:javascript复制
    volumeMounts:
        - name: config-volume
          mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
         name: test-cfg

代码语言:javascript复制
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

2.另一种方式,只挂载某个key,并支持相对路径,其中一个key生成的文件路径名和文件名相同

代码语言:javascript复制
---
      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
代码语言:javascript复制
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/path/to# pwd
/etc/config/path/to
root@testvolume:/etc/config/path/to# ls
special-key-cache

3.有subPath时且subPath推荐筛选结果为true,mountPath指定到文件名,(只挂载subpath选择的path到mountpath)---

代码语言:javascript复制
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
代码语言:javascript复制
root@testvolume:/etc/config# cat app.properties 
property.1 = value-1
property.2 = value-2
property.3 = value-3

4.有subPath但筛选结果为false, 容器中生成一个空目录/etc/config/app.properties,无文件---

代码语言:javascript复制
     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 
代码语言:javascript复制
root@testvolume:/etc/config# cd app.properties
root@testvolume:/etc/config/app.properties# ls
root@testvolume:/etc/config/app.properties# ls #此目录下为空

5.无 subPath,path相当于重命名---

代码语言:javascript复制
      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 
代码语言:javascript复制
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/path/to# cat special-key-cache 
mysql-k8s

6.有subPath且筛选结果为true,mouthPath指定文件名,可以和subPath不一样

---

代码语言:javascript复制
      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

---

代码语言:javascript复制
root@testvolume:/etc/config# cat app.properties
property.1 = value-1
property.2 = value-2
property.3 = value-3

7.有subPath且筛选结果为true,mouthPath指定文件名,可以和subPath不一样,甚至随意指定为z.txt---

代码语言:javascript复制
      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

---

代码语言:javascript复制
root@testvolume:/etc/config# cat z.txt 
property.1 = value-1
property.2 = value-2
property.3 = value-3

0 人点赞