A Persistent Volume Claim (PVC) template in Kubernetes is a specification used within a StatefulSet or a similar resource to dynamically request and manage persistent storage. It defines the characteristics of the storage that the application needs, such as size and access modes.
Key Components of a PVC Template:
-
Access Modes: Specifies how the volume can be accessed. Common modes include:
ReadWriteOnce: The volume can be mounted as read-write by a single node.ReadOnlyMany: The volume can be mounted as read-only by many nodes.ReadWriteMany: The volume can be mounted as read-write by many nodes.
-
Resources: Defines the storage requirements, typically specifying the amount of storage needed (e.g.,
10Gi). -
Storage Class: Optionally specifies the storage class to use for dynamic provisioning of the volume. This can determine the type of storage backend (e.g., SSD, HDD) and its performance characteristics.
Example of a PVC Template in a StatefulSet:
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: my-statefulset
spec:
serviceName: "my-service"
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: my-image
ports:
- containerPort: 80
volumeClaimTemplates:
- metadata:
name: my-pvc
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 10Gi
storageClassName: my-storage-class
In this example, the volumeClaimTemplates section defines a PVC template named my-pvc, which requests 10Gi of storage with ReadWriteOnce access mode. Each pod in the StatefulSet will have its own PVC created based on this template, ensuring that each pod has its own persistent storage.
