Persistent Volume and Persistent Volume Claim
Reserving a Persistent Volume
Tips and Tricks
For simulated Practice problems visit KillerCoda.
-
- storageClassName: ""
- capacity.storage: 1Gi
- volumeModes: FileSystem
- accessModes: ReadWriteMany
- hostPath: /data
Solution
# create pv.yaml apiVersion: v1 kind: PersistentVolume metadata: name: pv-1808 spec: capacity: storage: 1Gi volumeMode: Filesystem accessModes: - ReadWriteMany persistentVolumeReclaimPolicy: Recycle storageClassName: "" hostPath: path: /data # create persistent volume k create -f pv.yaml
-
Create a persistent Volume claim name
pvc-1808in namespacens-1808, binding topv-1808with the following details:- storageClassName: ""
- capacity.storage: 1Gi
- volumeModes: FileSystem
- accessModes: ReadWriteMany
Solution
# check for the ns-1808 namespace k get ns # create if it does not exist k create ns ns-1808 # create pvc.yaml apiVersion: v1 kind: PersistentVolumeClaim metadata: name: pvc-1808 namespace: ns-1808 spec: accessModes: - ReadWriteMany volumeMode: Filesystem resources: requests: storage: 1Gi storageClassName: "" volumeName: pv-1808 # create persistent volume k create -f pvc.yaml # check pvc status to be binded k get pvc -n ns-1808
-
Run pod
pd-1808with imagenginx:alpinein namespacens-1808mounting pvcpvc-1808as volume namedmydataat mount path/tedi, run a commandecho "It feels awsome to prepare for CKAD" > /tedi/author.txt.Solution
export dr="--dry-run=client -o yaml" # generate pod.yaml k run pd-1808 --image=nginx:alpine $dr > pod.yaml # update pod.yaml apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: pd-1808 name: pd-1808 namespace: ns-1808 spec: volumes: - name: mydata persistentVolumeClaim: claimName: pvc-1808 containers: - image: nginx:alpine name: pd-1808 command: ["sh","-c","echo 'It feels awsome to prepare for CKAD' > /tedi/author.txt"] volumeMounts: - name: mydata mountPath: /tedi dnsPolicy: ClusterFirst restartPolicy: Always # now to verify that our text is persisted on the host storage # check node on which pod is scheduled k describe po pd-1808 -n ns-1808 | grep -i node # ssh to that node ssh <node-name> # check file contents cat /data/author.txt
-
Create a persistent Volume name
lv-volwith storage capacity1Gi, mounting at/inventoryand accessModesRWO, bind it to a claim namelv-vol-claimand mount it on abusyboxpod namebusyrunning commandsleep 3600in namespacedefault.Solution
# create pv.yaml apiVersion: v1 kind: PersistentVolume metadata: name: lv-vol namespace: default spec: capacity: storage: 1Gi volumeMode: Filesystem accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Recycle hostPath: path: /inventory # create persistent volume claim k create -f pv.yaml # create pvc.yaml apiVersion: v1 kind: PersistentVolumeClaim metadata: name: lv-vol-claim namespace: default spec: accessModes: - ReadWriteOnce volumeMode: Filesystem resources: requests: storage: 1Gi volumeName: lv-vol # create persistent volume claim k create -f pvc.yaml # mount it to a pod apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: busy name: busy namespace: default spec: volumes: - name: data persistentVolumeClaim: claimName: lv-vol-claim containers: - image: busybox name: busy resources: {} command: ["sh","-c","sleep 3600"] volumeMounts: - name: data mountPath: /log dnsPolicy: ClusterFirst restartPolicy: Always
-
create a
rarepod with imagenginx:alpinemounting an emptyDir volume calledpdataat/usr/share/data.Solution
export do="--dry-run=client -o yaml" # generate pod.yaml k run rare --image=nginx:alpine $dr > pod.yaml # update pod.yaml apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: rare name: rare spec: volumes: - name: pdata emptyDir: {} containers: - image: nginx:alpine name: rare volumeMounts: - name: pdata mountPath: /usr/share/data dnsPolicy: ClusterFirst restartPolicy: Always # create the pod k create -f pod.yaml