【问题标题】:Kubernetes Persisent VolumeKubernetes 持久卷
【发布时间】:2020-05-22 16:21:24
【问题描述】:

谁能澄清一下 Kubernetes 中的持久卷?

在下面的示例中,/my-test-project 位于持久卷中。那么,为什么我需要这些挂载,因为从技术上讲,我的整个目录 /my-test-project 是持久的?如果保留整个目录,这些挂载路径和子路径将如何提供帮助。谢谢!

volumeMounts:
    - name: empty-dir-volume
      mountPath: /my-test-project/data-cache
      subPath: data-cache
    - name: empty-dir-volume
      mountPath:  /my-test-project/user-cache
      subPath: user-cache

  volumes:
  - name: empty-dir-volume
    emptyDir: {}

【问题讨论】:

    标签: kubernetes kubernetes-pod


    【解决方案1】:

    您的/my-test-project 整个目录未持久化

    • 主机/my-test-project/data-cache 中的mountPath 或路径在empty-dir-volume 路径data-cache 中持久化

    • mountPath /my-test-project/user-cache 持久化在路径empty-dir-volume 中的user-cache

    这意味着当您在 /my-test-project/data-cache 中创建文件时,它将被持久化在 emtpy-dir-volume 中的 data-cache(subpath) 中。对于用户缓存也是如此。每当您在 /my-test-project/ 中创建文件时,它不会被持久化。假设您创建了/my-test-project/new-dir,现在new-dir 将不会被持久化。

    为了更好地解释,让我们看下面的例子(两个容器共享卷,但在不同的 mounthPath):

    apiVersion: v1
    kind: Pod
    metadata:
      name: share-empty-dir
    spec:
      containers:
      - name: container-1
        image: alpine
        command:
          - "bin/sh"
          - "-c"
          - "sleep 10000"
        volumeMounts:
        - name: empty-dir-volume
          mountPath: /my-test-project/data-cache
          subPath: data-cache-subpath
        - name: empty-dir-volume
          mountPath:  /my-test-project/user-cache
          subPath: user-cache-subpath
      - name: container-2
        image: alpine
        command:
          - "bin/sh"
          - "-c"
          - "sleep 10000"
        volumeMounts:
          - name: empty-dir-volume
            mountPath: /tmp/container-2
      volumes:
      - name: empty-dir-volume
        emptyDir: {}
    

    在容器 1 中:

    • mountPath /my-test-project/user-cache 在路径 user-cache-subpath 中持续存在 empty-dir-volume
    • mountPath /my-test-project/data-cache 在路径empty-dir-volume 中持久化data-cache-subpath

    在容器 2 中:

    • mountPath /tmp/container-2 持久化在路径“”中的empty-dir-volume(表示“/”)

    观察:

    • 触摸/my-test-project/user-cache/a.txt。我们可以在/tmp/container-2/user-cache-subpath/a.txt 的 container-2 中看到这个文件,然后反向操作
    • 触摸/my-test-project/data-cache/b.txt。我们可以在/tmp/container-2/data-cache-subpath/a.txt 的容器 2 中看到这个文件,并且反向会起作用
    • touch /tmp/container-2/new.txt,我们永远不能将 container-1 中的这个文件作为我们在 container-1 中指定子路径的基本路径
    • 为了更好地理解而进行类似的尝试

    注意:为了清楚起见,您使用的是 emptyDir 类型的卷,这意味着每当 pod 被删除时,数据都会丢失。此类型仅用于在容器之间共享数据。

    【讨论】:

    • 感谢您的解释!您的下一行不清楚。
    • 感谢您的解释!您的行 - “您的 /my-test-project 整个目录没有持久化。”不清楚。我正在使用持久卷,而 /my-test-project 在我的持久卷中。那么,我的 /my-test-project 会被正确保存吗?
    • @testbgtestbg 添加了更多详细信息。请参考
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 2019-08-26
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    • 2022-08-10
    相关资源
    最近更新 更多