How to Use PVC in Pod in Kubernetes: Simple Guide
To use a
PersistentVolumeClaim (PVC) in a Kubernetes Pod, define the PVC first and then reference it in the pod's volumes section. Mount the volume inside the container using volumeMounts to access persistent storage.Syntax
To use a PVC in a pod, you need to specify the volumes section in the pod spec with a reference to the PVC name. Then, inside the container spec, use volumeMounts to mount the volume at a desired path.
Key parts:
- volumes: Links the PVC by name.
- persistentVolumeClaim: References the PVC resource.
- volumeMounts: Mounts the volume inside the container filesystem.
yaml
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: example-container
image: busybox
command: ["sleep", "3600"]
volumeMounts:
- mountPath: "/data"
name: example-pvc-volume
volumes:
- name: example-pvc-volume
persistentVolumeClaim:
claimName: example-pvcExample
This example shows a PVC and a Pod using that PVC to mount storage at /data. The pod runs a simple sleep command to keep running so you can inspect the volume.
yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: example-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: example-container
image: busybox
command: ["sleep", "3600"]
volumeMounts:
- mountPath: "/data"
name: example-pvc-volume
volumes:
- name: example-pvc-volume
persistentVolumeClaim:
claimName: example-pvcOutput
persistentvolumeclaim/example-pvc created
pod/example-pod created
kubectl exec example-pod -- ls /data
# (empty if no files yet)
Common Pitfalls
Common mistakes when using PVCs in pods include:
- Not creating the PVC before the pod, causing pod creation to fail.
- Mismatching
claimNamein the pod volume and the actual PVC name. - Forgetting to mount the volume inside the container with
volumeMounts. - Using incompatible
accessModesfor your storage backend.
Always check PVC status with kubectl get pvc to ensure it is Bound before pod creation.
yaml
apiVersion: v1
kind: Pod
metadata:
name: wrong-pod
spec:
containers:
- name: container
image: busybox
volumeMounts:
- mountPath: "/data"
name: wrong-volume
volumes:
- name: wrong-volume
persistentVolumeClaim:
claimName: non-existent-pvc
# This pod will fail because the PVC does not exist.Quick Reference
| Field | Description |
|---|---|
| persistentVolumeClaim.claimName | Name of the PVC to use in the pod volume |
| volumes.name | Name to reference the volume inside the pod |
| containers.volumeMounts.mountPath | Path inside the container where volume is mounted |
| accessModes | Defines how the volume can be accessed (e.g., ReadWriteOnce) |
| resources.requests.storage | Amount of storage requested by the PVC |
Key Takeaways
Create the PVC before referencing it in a pod to avoid errors.
Reference the PVC in the pod's volumes section using claimName.
Mount the volume inside the container with volumeMounts to access storage.
Check PVC status is Bound before pod creation.
Ensure accessModes and storage size in PVC match your needs.