Middle
What are PV and PVC in the context of Kubernetes?
sobes.tech AI
Answer from AI
Persistent Volume (PV) is a storage resource in a Kubernetes cluster. It abstracts the details of the specific storage implementation from consumers. PV is managed by the cluster administrator and exists independently of the lifecycle of any pod.
Persistent Volume Claim (PVC) is a user's request for storage. The user requests a storage volume with specific characteristics (size, access mode). PVC is bound to a suitable PV. If a suitable PV is not found, dynamic provisioning of a new PV can be performed if the appropriate StorageClass is configured.
Main differences and interactions:
- PV: Represents the actual storage resource.
- PVC: Represents the user's request for storage.
- PVC is bound to PV.
- PV can be statically created by an administrator or dynamically created upon PVC request using StorageClass.
- A Pod references a PVC, not directly a PV, which separates application logic from storage details.
Access modes for PV and PVC:
ReadWriteOnce(RWO): The volume can be mounted for read/write by a single node at a time.ReadOnlyMany(ROX): The volume can be mounted read-only by many nodes simultaneously.ReadWriteMany(RWX): The volume can be mounted for read/write by many nodes simultaneously.
Example YAML for PV and PVC:
# persistent-volume.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
capacity:
storage: 5Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain # What to do with the volume after PVC deletion (Retain, Recycle, Delete)
storageClassName: local-storage # Can be used to bind with StorageClass
local:
path: /mnt/data
nodeAffinity: # Affinity for local storage
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- my-node
# persistent-volume-claim.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: local-storage # Binds to PV or StorageClass
# pod-using-pvc.yaml
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
containers:
- name: app-container
image: nginx
volumeMounts:
- name: app-storage
mountPath: /usr/share/nginx/html
volumes:
- name: app-storage
persistentVolumeClaim:
claimName: my-pvc # Reference to PVC