• Kubernetes概念详解之:subPath字段的使用原理及案例分析


    Kubernetes概念详解之:subPath字段的使用原理及案例分析

    有时,在单个 Pod 中共享卷以供多方使用是很有用的。volumeMounts.subPath 属性可用于指定所引用的卷内的子路径,而不是其根路径。

    这句话理解了,基本就懂subPath怎么用了,比如我们要替换nginx.cnf, 挂载的ConfigMap是一个文件夹,如果没有subPath,那/etc/nginx/nginx.cnf将变成一个文件夹,subPath是用来指定卷内子路径的!

    subPath的使用有两种情况:

    1. 作为volumes使用时,subPath代表存储卷的子路径

    2. 作为configmap/secret使用时,subPath代表configmap/secrect的子路径

    1. 作为volumes使用时

    yaml文件

    apiVersion: v1
    kind: Pod
    metadata:
      name: testpod0
    spec:
      containers:
      - name: testc
        image: busybox
        command: ["/bin/sleep","10000"]
        volumeMounts:
          - name: data
            mountPath: /opt/data    # 挂载的路径
            subPath: data           # volume的子路径
            mountPath: /opt/model
            subPath: model
      volumes:
        - name: data
          hostPath:
            path: /opt/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    上述情况下,数据卷使用了hostPath的类型,并定义path为宿主机的/opt/路径。在容器的volumeMounts挂载配置上,使用了subPath配置来指定将数据卷的/opt/data子目录和/opt/model子目录,分别挂载到容器的/opt/data目录和/opt/model目录。

    使用数据卷内子路径的好处就是可以在容器中灵活地选择需要挂载的数据卷的部分内容,避免将整个数据卷都挂载到容器中造成资源浪费,并且可以方便地控制容器中的文件路径和内容。

    2. 作为configmap/secrect使用时

    以文件形式挂载configMap以及挂载secret和挂载普通文件同样道理,可以把 configMap和secret当做卷的根目录,configMap和secret中的key作为 subpath 指定的文件名。此时若是 configMap和secret中key名称为subpath的值,以及和volumeMounts.mountPath文件名保持一致,那么容器中看到挂载的文件就是configMap和secret的key。

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: config-test
    data:
      config.ini: "hello"
      config.conf: "nihao"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    单独挂载一个key为文件

    apiVersion: v1
    kind: Pod
    metadata:
      name: testpod
    spec:
      containers:
      - name: testc
        image: busybox
        command: ["/bin/sleep","10000"]
        volumeMounts:
          - name: config-test
            mountPath: /etc/config.ini   # 最终在容器中的文件名
            subPath: config.ini  #要挂载的confmap中的key的名称
      volumes:
        - name: config-test
          configMap:
            name: config-test
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    上述yaml配置文件中,volumes属性将configMap作为数据卷,并在容器中设置了mountPath: /etc/config.inisubPath: config.ini。其中subPath指定了容器挂载点要获取的configMap键值,mountPath指明了该键值的挂载路径。

    上述这种将subPath获取的key值与挂载路径的文件名写成一致的书写方式,是写subPath子路径的一种比较标准的写法。当然,这个挂载的文件名和subPath的key值可以写成不同的。

    挂载多个key为文件:

    apiVersion: v1
    kind: Pod
    metadata:
      name: testpod2
    spec:
      containers:
      - name: testc
        image: busybox
        command: ["/bin/sleep","10000"]
        volumeMounts:
          - name: config-test
            mountPath: /etc/config.ini   # 最终在容器中的文件名
            subPath: config.ini  #要挂载的confmap中的key的名称
            mountPath: /etc/config.conf   # 最终在容器中的文件名
            subPath: config.conf  #要挂载的confmap中的key的名称
      volumes:
        - name: config-test
          configMap:
            name: config-test
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    多个container挂载不同的key:

    apiVersion: v1
    kind: Pod
    metadata:
      name: testpod1
    spec:
      containers:
      - name: testc
        imagePullPolicy: Never
        image: busybox
        command: ["/bin/sleep","10000"]
        volumeMounts:
          - name: config-test
            mountPath: /etc/config/config.ini
            subPath: config.ini
      - name: testc1
        imagePullPolicy: Never
        image: busybox
        command: ["/bin/sleep","10000"]
        volumeMounts:
          - name: config-test
            mountPath: /etc/config/config.conf
            subPath: config.conf
      volumes:
        - name: config-test
          configMap:
            name: config-test
            items:
            - key: config.ini
              path: config.ini
            - key: config.conf
              path: config.conf
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
  • 相关阅读:
    Java开发学习---Maven私服(二)本地仓库访问私服配置与私服资源上传下载
    壳聚糖-卵清蛋白|Chitosan-Ovalbumin|卵清蛋白-PEG-壳聚糖
    干货 | 移动端App自动化之App控件定位
    CSS3中animation实现流光按钮效果
    Python闯LeetCode--第2题:两数相加
    Oracle 11g+PLSQL Developer安装及环境配置
    JavaScript 操作浏览器和HTML文档/JavaScript 操作对象
    【PAT(甲级)】1048 Find Coins(测试点2,3,4)
    [当前就业]2023年8月25日-计算机视觉就业现状分析
    Fast R-CNN
  • 原文地址:https://blog.csdn.net/Urbanears/article/details/134098026