【问题标题】:What is the difference between subPath and mountPath in KubernetesKubernetes 中的 subPath 和 mountPath 有什么区别
【发布时间】:2021-03-31 15:02:29
【问题描述】:

我正在尝试将 volumeMounts 中的文件添加到 .dockerignore,并尝试了解 subPath 和 mountPath 之间的区别。阅读官方文档对我来说不是很清楚。

我应该从我阅读的内容中添加 mountPath 是 pod 中将安装卷的目录。

来自官方文档:“subPath volumeMounts.subPath 属性指定了引用卷内的子路径,而不是其根。” https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath(这部分不清楚)

- mountPath: /root/test.pem
            name: test-private-key
            subPath: test.testing.com.key

在这个例子中,我应该将 test.pem 和 test.testing.com.key 都包含到 dockerignore 中吗?

【问题讨论】:

    标签: kubernetes


    【解决方案1】:

    mountPath 显示引用的卷应安装在容器中的哪个位置。例如,如果您将卷挂载到mountPath: /a/b/c,则该卷将可用于目录/a/b/c 下的容器。

    安装卷将使mountPath 下的所有卷都可用。如果只需要挂载卷的一部分,例如卷中的单个文件,则使用subPath 指定必须挂载的部分。例如,mountPath: /a/b/csubPath: d 将使 d 位于目录 /a/b/c 下的挂载卷中

    【讨论】:

    • 感谢您的回复。所以为了确保我理解你的最后一句话,你是说只有文件 d 是要挂载到 /a/b/c 的吗?所以会是 /a/b/c/d?
    • 是的,subPath,这就是你会得到的。
    • 注意当subPath为文件夹时,文件夹的内容会挂载到mountPath
    【解决方案2】:

    请务必注意,subPath 将删除 mountPath 中的所有文件和文件夹,只保留 subPath 文件/文件夹。

    为了说明这一点:

    案例一:没有子路径

    让我们假设这是您的 volumeMount 定义:

    volumeMounts:
    - name: test-vol
      mountPath: /a/b/c
    

    如果你在容器的挂载卷中输入命令ls /a/b/c,你会得到这个示例结果:

    one.txt two.txt /some_folder
    

    案例 2:使用 subPath:

    如果将subPath: d添加到容器的volumeMounts中:

    volumeMounts:
    - name: test-vol
      mountPath: /a/b/c
      subPath: d
    

    那么d会覆盖/a/b/c的所有内容 因此,现在如果你给出命令ls /a/b/c,结果就是d。 删除原内容one.txt two.txt /bin

    我推荐这个 youtube 链接作为一个真实的例子:link..

    【讨论】:

      猜你喜欢
      • 2019-03-05
      • 2018-05-30
      • 2020-05-18
      • 2015-01-30
      • 2016-04-04
      • 2018-05-12
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      相关资源
      最近更新 更多