【问题标题】:How can I mount files in the same sub path in k8s deployment?如何在 k8s 部署中将文件挂载到同一子路径中?
【发布时间】:2021-10-09 18:23:57
【问题描述】:

我现在在我的部署中定义了这些卷安装,

volumeMounts:
        - name: website-storage
          mountPath: /app/upload
          readOnly: false
          subPath: foo/upload
        - name: website-storage
          mountPath: /app/html
          readOnly: true
          subPath: foo/html/html

现在我想将另一条路径从我的 PVC 挂载到 /app/html/website-content,这就是我尝试的方式,

volumeMounts:
        - name: website-storage
          mountPath: /app/upload
          readOnly: false
          subPath: foo/upload
        - name: website-storage
          mountPath: /app/html
          readOnly: true
          subPath: foo/html/html
        - name: website-storage
          mountPath: /app/html/website-content
          readOnly: true
          subPath: foo/website-content

这不起作用,并在安装过程中出错。是否有可能做到这一点?我必须在安装之前明确创建website-content 文件夹吗?提前谢谢!

【问题讨论】:

    标签: kubernetes volumes persistent-volume-claims


    【解决方案1】:

    问题的原因是在 pod 初始化期间,尝试在 /app/html 位置创建目录 website-content,这将导致错误,因为 /app/html 以只读方式挂载。

    您不能在只读系统中创建文件夹,这意味着您无法挂载卷,因为该文件夹不存在,但如果已经创建了该文件夹,则可以挂载该卷。

    因此,您只需在卷上的foo/html/html 位置创建一个目录website-content,然后再将其附加到容器中。然后,因为它会被挂载到/app/html 位置,所以会有目录/app/html/website-content

    例如,您可以为此使用init container。将此代码添加到您的部署文件中:

    initContainers:
      - name: init-container
        image: busybox
        volumeMounts:
        - name: website-storage
          mountPath: /my-storage
          readOnly: false
        command: ['sh', '-c', 'mkdir -p /my-storage/foo/html/html/website-content']
    

    当 pod 运行时,您使用 kubectl describe pod {pod-name} 检查 pod 上的挂载点:

    Mounts:
    /app/html from website-storage (ro,path="foo/html/html")
    /app/html/website-content from website-storage (ro,path="foo/website-content")
    /app/upload from website-storage (rw,path="foo/upload")
    

    【讨论】:

      【解决方案2】:

      在我看来 app/html 已经挂载并且是只读的,最好使用不同的路径,

      第二个你需要与更多的 pod 共享卷吗?

      【讨论】:

        【解决方案3】:

        您不能将一个卷装入另一个卷。相反,您可以将其挂载到不同的位置,并在容器启动期间创建指向该位置的符号链接。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-21
          • 1970-01-01
          • 1970-01-01
          • 2020-11-29
          相关资源
          最近更新 更多