Introduction to Persistent Volume Claims (PVCs)
Persistent Volume Claims (PVCs) in Kubernetes are used to manage storage resources. They act as a request for storage and abstract the underlying implementation details like NFS, AWS EBS, etc.
Creating and Using PVCs
To create a PVC, you’ll need a YAML file that describes its specifications.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
Use kubectl
to create it:
kubectl apply -f my-pvc.yaml
To use the PVC in a Pod:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: nginx
volumeMounts:
- mountPath: "/data"
name: my-volume
volumes:
- name: my-volume
persistentVolumeClaim:
claimName: my-pvc
Binding and Storage Class Considerations
Once a PVC is created, Kubernetes will try to find a suitable Persistent Volume (PV) to bind it to. The binding is governed by StorageClass
.
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast-storage
provisioner: kubernetes.io/aws-ebs
parameters:
type: gp2
PVC Lifecycle
- Provisioning: Created by user
- Binding: Binds to a suitable PV
- Using: Used by a Pod
- Releasing: Unbound from the Pod
- Reclaiming: PV is either recycled, deleted, or retained based on reclaim policy
Best Practices
- Clearly Define Resources: Always specify storage size and access modes.
- Use Labels: Make it easier to manage PVCs with labels.
- Reuse PVCs: If possible, design applications so PVCs can be reused.
- Choose the Right Storage Class: Ensure you choose a storage class that meets your performance and budget requirements.
Exercise: Setting up and Using a PVC
Setup
- Create a StorageClass:
kubectl apply -f storage-class.yaml
- Create a PVC:
kubectl apply -f my-pvc.yaml
Steps
- Check PVC status:
kubectl get pvc my-pvc
- Create a Pod using the PVC:
kubectl apply -f my-pod.yaml
- Verify Pod and PVC binding:
kubectl describe pod my-pod
Troubleshooting
- PVC not Bound: Check events in the PVC description (
kubectl describe pvc my-pvc
) - Pod Errors: Look at Pod logs (
kubectl logs my-pod
)
Solution
If everything is set up correctly, your Pod should be running, and the PVC should be bound to a PV.