Click here to Skip to main content
16,004,505 members
Articles / Containers / Kubernetes

Persistent Volumes and Persistent Volume Claims

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
18 Aug 2024CPOL2 min read 1.4K   1  
Kubernetes provides mechanisms for managing persistent data, with Persistent Volumes (PVs) and Persistent Volume Claims (PVCs) playing a crucial role. In this article, we will delve into the definition and usage of PVs and PVCs for managing storage in Kubernetes.

1. Persistent Volumes (PVs)

A Persistent Volume (PV) is a cluster-level resource in Kubernetes representing a piece of storage that has been provisioned for use by one or more Pods. PVs are not tied to the lifecycle of individual Pods, ensuring data persistence even if Pods are rescheduled or deleted.

Key characteristics of Persistent Volumes:

  • Independent of Pods: This means that even if you delete a Pod that was using a PV, the data in the PV will remain intact. This is crucial for applications that require long-term data storage, such as databases or data analysis tools.
  • Persistent Storage: PVs can be backed by different storage systems, giving you the flexibility to choose the best storage solution for your specific needs. For example, you might use SSDs for high-performance applications and HDDs for large-scale data storage.
  • Lifecycle Management: These policies allow you to control how PVs are managed over time. For example, you can set a retention policy to automatically delete data after a certain period, or you can set a reclaim policy to automatically release the PV when it is no longer in use.

Create a Persistent Volume

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  nfs:
    path: /path/to/nfs
    server: nfs-server.example.com
  storageClassName: manual
  accessModes:
    - ReadWriteMany

2. Persistent Volume Claims (PVCs)

A Persistent Volume Claim (PVC) is a Kubernetes object that represents a request for storage. It's like a user or application submitting a ticket to a storage administrator, specifying the amount of storage needed, how it should be accessed (read-only, read-write, etc.), and what type of storage is preferred (e.g., SSD, HDD).

Think of it as a contract between the application and the underlying storage system. The PVC defines the application's storage needs, and Kubernetes is responsible for matching the PVC to an available Persistent Volume (PV) that meets the specified requirements.

Key Characteristics of Persistent Volume Claims:

  • Size and Access Modes: PVCs can specify a certain amount of storage and access modes (ReadWriteOnce, ReadOnlyMany, ReadWriteMany).
  • Automatic Provisioning: Kubernetes will automatically provision PVCs with suitable PVs based on the PVC's requests and the PV's attributes.
  • Abstraction: PVCs allow applications to request storage without knowing the details of how the storage is provided.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: manual

3. Using Persistent Volumes and Persistent Volume Claims

Step 1: Create a Persistent Volume

First, you need to create a Persistent Volume (PV) to provide storage for PVCs. You can configure the PV to use various storage types such as NFS, AWS EBS, GCE PD, etc.

Step 2: Create a Persistent Volume Claim

After creating the PV, you create a Persistent Volume Claim (PVC) to request a portion of the storage from the PV. The PVC will request a specific amount of storage and will automatically be bound to a PV if the requests match the PV's characteristics.

Step 3: Use the PVC in a Pod

Once the PVC is created and bound to a PV, you can use the PVC in Pods to store data. The PVC will be mounted into the Pod as a volume.

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: my-container
      image: my-image
      volumeMounts:
        - mountPath: /data
          name: my-volume
  volumes:
    - name: my-volume
      persistentVolumeClaim:
        claimName: my-pvc

Read more at: Persistent Volumes and Persistent Volume Claims

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior)
Vietnam Vietnam
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --