kubernetes - NFS-PV、NFS 上的 hostPath-PV 和部署中的 hostPath 挂载之间的区别

标签 kubernetes nfs persistent-volumes

我有一个 Kubernetes 集群设置(本地),它有一个 NFS 共享(my-nfs.internal.tld)安装到 /exports/backup在每个节点上创建备份。
现在我正在设置我的日志记录堆栈,我想让数据持久化。所以我想我可以从在 NFS 上存储索引开始。
现在我找到了三种不同的方法来实现这一目标:
NFS-PV

apiVersion: v1
kind: PersistentVolume
metadata:
  name: logging-data
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  nfs:
    server: my-nfs.internal.tld
    path: /path/to/exports/backup/logging-data/
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: logging-data-pvc
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: logging-data
  resources:
    requests:
      storage: 10Gi
apiVersion: apps/v1
kind: Deployment
...
spec:
  ...
  template:
    ...
    spec:
      ...
      volumes:
        - name: logging-data-volume
          persistentVolumeClaim:
            claimName: logging-data-pvc
当然,这将要求我的集群访问 NFS(而不是仅访问当前设置的节点)。
主机路径-PV
apiVersion: v1
kind: PersistentVolume
metadata:
  name: logging-data
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /exports/backup/logging-data/
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: logging-data-pvc
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: logging-data
  resources:
    requests:
      storage: 10Gi
apiVersion: apps/v1
kind: Deployment
...
spec:
  ...
  template:
    ...
    spec:
      ...
      volumes:
        - name: logging-data-volume
          persistentVolumeClaim:
            claimName: logging-data-pvc
部署中的 hostPath 挂载
由于 nfs 已安装到我的所有节点,因此我也可以直接在部署中使用主机路径而无需固定任何内容。
apiVersion: apps/v1
kind: Deployment
...
spec:
  ...
  template:
    ...
    spec:
      ...
      volumes:
        - name: logging-data-volume
          hostPath:
            path: /exports/backup/logging-data
            type: DirectoryOrCreate

所以我的问题是:这三者之间真的有什么区别吗?我很确定这三个都有效。我已经测试了第二个和第三个。我还不能测试第一个(至少在这个特定的设置中)。特别是第二个和第三个解决方案似乎与我非常相似。我认为,第二个使在多个集群上重用部署文件变得更容易,因为您可以使用不同类型的持久卷而无需更改 volumes部署的一部分。但除此之外还有什么区别吗?性能可能吗?还是其中之一已被弃用并将很快被删除?
我找到了一个 tutorial值得一提的是,hostPath-PV 仅适用于单节点集群。但我确信它也适用于我的情况。也许评论是关于:“在多节点集群上,部署到不同节点时数据会发生变化。”
从阅读到大量文档和操作方法,我明白,第一个是首选解决方案。我可能也会选择它,因为它是最容易复制到云设置的方法。但我真的不明白为什么这比其他两个更受欢迎。
在此先感谢您对此事的意见!

最佳答案

NFS确实是首选的解决方案:

An nfs volume allows an existing NFS (Network File System) share to be mounted into a Pod. Unlike emptyDir, which is erased when a Pod is removed, the contents of an nfs volume are preserved and the volume is merely unmounted. This means that an NFS volume can be pre-populated with data, and that data can be shared between pods. NFS can be mounted by multiple writers simultaneously.


因此,NFS 之所以有用有两个原因:
  • 数据是持久的。
  • 可以同时从多个 Pod 访问它,并且可以在 Pod 之间共享数据。

  • 请参阅 NFS example更多细节。
    hostPath :

    A hostPath volume mounts a file or directory from the host node's filesystem into your Pod.

    Pods with identical configuration (such as created from a PodTemplate) may behave differently on different nodes due to different files on the nodes

    The files or directories created on the underlying hosts are only writable by root. You either need to run your process as root in a privileged Container or modify the file permissions on the host to be able to write to a hostPath volume

    hostPath不推荐,有以下几个原因:
  • 您不能直接控制 pod 将在哪个节点上运行,因此您不能保证 pod 会实际调度在具有数据量的节点上。
  • 您将集群暴露在安全威胁之下。
  • 如果一个节点出现故障,您需要将 pod 安排在其他节点上,在该节点上您的本地配置卷将不可用。
  • hostPath例如,如果您想将它用于在 DaemonSet 中运行的日志收集器,那就太好了。 .除此之外,最好使用 NFS。

    关于kubernetes - NFS-PV、NFS 上的 hostPath-PV 和部署中的 hostPath 挂载之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65568898/

    相关文章:

    kubernetes - 使用 kubectl 从 gke 应用程序中删除外部 IP 地址

    linux - 更改 NFS 共享的所有者

    python - 如何在 NFS 上进行正确的文件锁定?

    Kubernetes 持久卷 : MountPath directory created but empty

    mysql - 无法为 wordpress 和 mysql 制作具有持久数据工作的 kubernetes 示例

    kubernetes - 寻找从 K8s Yaml 文件生成图表的工具

    kubernetes - 谷歌 Kubernetes 引擎 : How to define one Ingress for multiple namespaces?

    dynamic - Kubernetes Hostpath 外部供应商 - PVC 待定

    linux - 如何使符号链接(symbolic link)与远程挂载一起使用?

    Kubernetes:我可以将同一 PV 的不同子路径挂载到同一容器的不同位置吗?