前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >VolumeMount浅析

VolumeMount浅析

原创
作者头像
用户7020774
修改2020-03-09 13:53:10
8.5K0
修改2020-03-09 13:53:10
举报
文章被收录于专栏:风云笔记风云笔记
     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下:

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

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为内容

    volumeMounts:
        - name: config-volume
          mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
         name: test-cfg

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生成的文件路径名和文件名相同

---
      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@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)---

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@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,无文件---

     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@testvolume:/etc/config# cd app.properties
root@testvolume:/etc/config/app.properties# ls
root@testvolume:/etc/config/app.properties# ls #此目录下为空

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

      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@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不一样

---

      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@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---

      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@testvolume:/etc/config# cat z.txt 
property.1 = value-1
property.2 = value-2
property.3 = value-3

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档