了解 Kubernetes 中的 PV - PV 如何存储数据?

Understand PV in Kubernetes - How PV store data?

我在学习k8s,对PV的理解有点问题

比如我想部署PostgreSQL 存储数据在/var/lib/postgresql/data 我想将我的本地磁盘用于 PV,所以我创建了它并设置了我的路径:/mnt/ssd/Kubernetes/Postgres

我不明白 PV 和 PVC 将如何存储我的数据,因为我用 PostgreSQL 创建了 pod1,创建了新的 DB 并杀死了这个 pod1。新的 pod2 仍然有我第二次在 pod1 上创建的数据库,但在 /mnt/ssd/Kubernetes/Postgres 的本地磁盘上我没有任何文件,所以

  1. 新的pod2如何知道创建的数据库? PV 如何存储有关我创建的数据库的数据?
  2. 如果 PV 上没有任何数据,为什么需要我的磁盘?

为了深入了解卷、持久卷和声明在 Kubernetes 中的工作原理,我强烈建议阅读有关以下方面的官方文档:

  1. Volumes:

On-disk files in a Container are ephemeral, which presents some problems for non-trivial applications when running in Containers. First, when a Container crashes, kubelet will restart it, but the files will be lost - the Container starts with a clean state. Second, when running Containers together in a Pod it is often necessary to share files between those Containers. The Kubernetes Volume abstraction solves both of these problems.

  1. Persistent Volumes:

A PersistentVolume (PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes. It is a resource in the cluster just like a node is a cluster resource. PVs are volume plugins like Volumes, but have a lifecycle independent of any individual Pod that uses the PV. This API object captures the details of the implementation of the storage, be that NFS, iSCSI, or a cloud-provider-specific storage system.

  1. 一个 PersistentVolumeClaim (PVC):

is a request for storage by a user. It is similar to a Pod. Pods consume node resources and PVCs consume PV resources. Pods can request specific levels of resources (CPU and Memory). Claims can request specific size and access modes (e.g., they can be mounted ReadWriteOnce, ReadOnlyMany or ReadWriteMany, see AccessModes).

在从理论上掌握它们的工作原理后,您可以看到 detailed walkthrough with working examples

此外,您的 PostgreSQL 是一个示例 stateful application. Here 您可以找到一个教程,向您展示如何使用 Persistent Volumes 部署数据库和应用程序。