This workshop demonstrates the use of PersistentVolumes (PVs) and PersistentVolumeClaims (PVCs) in Kubernetes with ReadWriteOnce (RWO) and ReadWriteMany (RWX) access modes. Minikube's default storage provisioner is used for dynamic volume provisioning.
- Minikube is installed and running:
minikube start
- Enable the default storage provisioner:
minikube addons enable storage-provisioner
pv-rwo.yaml:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-rwo
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
Apply the manifest:
kubectl apply -f pv-rwo.yaml
Verify that the resources were created:
kubectl get pv
kubectl get pvc
pod1-rwo.yaml:
apiVersion: v1
kind: Pod
metadata:
name: pod1-rwo
spec:
containers:
- name: busybox
image: busybox
command: ["sleep", "3600"]
volumeMounts:
- mountPath: "/data"
name: storage
volumes:
- name: storage
persistentVolumeClaim:
claimName: pvc-rwo
Apply the manifest:
kubectl apply -f pod1-rwo.yaml
Verify the Pod is running:
kubectl get pods
pod2-rwo.yaml:
apiVersion: v1
kind: Pod
metadata:
name: pod2-rwo
spec:
containers:
- name: busybox
image: busybox
command: ["sleep", "3600"]
volumeMounts:
- mountPath: "/data"
name: storage
volumes:
- name: storage
persistentVolumeClaim:
claimName: pvc-rwo
Apply the manifest:
kubectl apply -f pod2-rwo.yaml
- Access Pod1:
kubectl exec -it pod1-rwo -- sh
- Write data to the volume:
echo "RWO Test Data" > /data/testfile.txt
- Exit the Pod:
exit
- Access Pod1:
kubectl exec -it pod2-rwo -- sh
- Write data to the volume:
ls /data/testfile.txt
- Exit the Pod:
exit
pv-rwx.yaml:
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-rwx
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
hostPath:
path: /data/rwx
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-rwx
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
volumeName: pv-rwx
Apply the manifest:
kubectl apply -f pv-rwx.yaml
Verify that the resources were created:
kubectl get pv
kubectl get pvc
pod1-rwx.yaml:
apiVersion: v1
kind: Pod
metadata:
name: pod1-rwx
spec:
containers:
- name: busybox
image: busybox
command: ["sleep", "3600"]
volumeMounts:
- mountPath: "/data"
name: shared-storage
volumes:
- name: shared-storage
persistentVolumeClaim:
claimName: pvc-rwx
pod2-rwx.yaml:
apiVersion: v1
kind: Pod
metadata:
name: pod2-rwx
spec:
containers:
- name: busybox
image: busybox
command: ["sleep", "3600"]
volumeMounts:
- mountPath: "/data"
name: shared-storage
volumes:
- name: shared-storage
persistentVolumeClaim:
claimName: pvc-rwx
Apply both manifests:
kubectl apply -f pod1-rwx.yaml
kubectl apply -f pod2-rwx.yaml
Verify that both Pods are running:
kubectl get pods
- Access Pod1:
kubectl exec -it pod1-rwx -- sh
- Write data to the shared volume:
echo "RWX Test Data" > /shared-data/testfile.txt
- Exit Pod1:
exit
- Access Pod2:
kubectl exec -it pod2-rwx -- sh
- Verify the data:
Expected output:
cat /shared-data/testfile.txt
RWX Test Data
To remove all resources:
kubectl delete pod pod1-rwo pod2-rwo pod1-rwx pod2-rwx
kubectl delete pvc pvc-rwo pvc-rwx
kubectl delete pv pv-rwo pv-rwx
- RWO (ReadWriteOnce): Allows one Pod to mount the volume at a time.
- RWX (ReadWriteMany): Allows multiple Pods to share the same volume.
- Minikube’s hostPath simplifies testing PVs and PVCs locally.