I learning k8s and I have a little problem to understand PV.
For example, I want to deploy PostgreSQL which store data in /var/lib/postgresql/data I want to use my local disk for PV so I created that and set my path: /mnt/ssd/Kubernetes/Postgres
I do not understand how PV and PVC will store my data, because I created pod1 with PostgreSQL, created new DB and kill this pod1. New pod2 still have database which I created few second time ago on pod1 but on my local disk in /mnt/ssd/Kubernetes/Postgres I do not have any files, so 1. How new pod2 know about created database? How PV store data about my created database? 2. Why does PV need my disk if it doesn't hold any data on it?
To get a strong understanding of how volumes, persistent volumes and claims work in Kubernetes I strongly suggest going through the official documentation regarding:
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.
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 likeVolumes
, 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.
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
orReadWriteMany
, see AccessModes).
After getting a solid theoretical grip of how they work, you can see the detailed walkthrough with working examples.
Also, your PostgreSQL is an example of a stateful application. Here you can find a tutorial showing you how to deploy a database and application with Persistent Volumes.