Sobes.tech
Middle

Mis on PV ja PVC Kubernetes kontekstis?

sobes.tech AI

Vastus AI-lt

Persistent Volume (PV) — this 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 any pod lifecycle.

Persistent Volume Claim (PVC) — this is a user's request for storage. The user requests a storage volume with certain characteristics (size, access mode). PVC is linked to an appropriate PV. If no suitable PV is found, a new PV can be dynamically provisioned if the corresponding StorageClass is configured.

Main differences and interactions:

  • PV: Represents the actual storage resource.
  • PVC: Represents the user's request for storage.
  • PVC is linked to PV.
  • PV can be statically created by the administrator or dynamically provisioned based on PVC request using StorageClass.
  • The pod references the PVC, not directly the PV, allowing separation of 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 link 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 # Links 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