– Understand storage classes, persistent volumes.
– Understand volume mode, access modes and reclaim policies for volumes.
– Understand persistent volume claims primitive.
– Know how to configure applications with persistent storage.
If you are interested to learn Volume then you may go through the below videos at the end of this page. However, if you’re interested only for CKA exam then follow me.
In exam, they may ask to you to create PV, after that create PVC and bound this PVC with PV that you created earlier. Further, they may ask to create pod and then mount this PVC.
Let’s take an example of some questions.
Question . Create a new PersistentVolume named tata-pv. It should have a capacity of 2Gi, accessMode ReadWriteOnce, hostPath /srv/app-config-var.
Next create a new PersistentVolumeClaim in Namespace project-tiger
named tata-pvc . It should request 2Gi storage, accessMode ReadWriteOnce
and should not define a storageClassName. The PVC should bound to the
PV correctly.
Finally create a new pod tata in Namespace project-tiger which mounts
that volume at /tmp/tata-data. The Pods should be of image
httpd:2.4.41-alpine.
Solution: In exam, namespace “project-tige” may already created.
To complete this question, we need to create PV, PVC and POD.
1. Create PV
name = tata-pv,
Capacity = 2Gi,
accessMode = ReadWriteOnce,
hostPath = /srv/app-config-var ,
Remember: PV is not namespaced object.
2. Create PVC
name = tata-pvc,
Capacity = 2Gi,
accessMode = ReadWriteOnce,
Namespace = project-tiger,
should not define a storageClassName.
3. Create POD
name = tata,
namespace = project-tiger
volume = /tmp/tata-data
image = httpd:2.4.41-alpine
1. let’s create PV first. For this Open kubernetes.io Web page => Click on “Documentation” => Search “pv hostPath”
Copy content like below from the website.
Copy the below content |
Remove the lines |
Our final file |
apiVersion: v1 kind: PersistentVolume metadata: name: task-pv-volume labels: type: local spec: storageClassName: manual capacity: storage: 10Gi accessModes: – ReadWriteOnce hostPath: path: “/mnt/data”
|
apiVersion: v1 kind: PersistentVolume metadata: name: tata-pv labels: type: local spec: storageClassName: manual capacity: storage: 2Gi accessModes: – ReadWriteOnce hostPath: path: “/srv/app-config-var”
|
apiVersion: v1 kind: PersistentVolume metadata: name: tata-pv spec: capacity: storage: 2Gi accessModes: – ReadWriteOnce hostPath: path: “/srv/app-config-var”
|
Now, we have all the values. Let’s create one yaml file for PV.
cat <<EOF>> question7-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: tata-pv
spec:
capacity:
storage: 2Gi
accessModes:
– ReadWriteOnce
hostPath:
path: “/srv/app-config-var”
EOF
2. After the PV creation, we need to create PVC. In order to create PVC:
kubernetes.io => Documentation => Search “pvc accessModes”
Open the first link.
Copy the below content |
Remove the lines |
Our final file |
|
|
|
Now, we have all the values. Let’s create one yaml file for PVC.
kind: PersistentVolumeClaim
metadata:
name: tata-pvc
namespace: project-tiger
spec:
accessModes:
– ReadWriteOnce
resources:
requests:
storage: 2Gi
3. For creating a POD,
name=tata, namespace=project-tiger, volume=/tmp/tata-data, image=httpd:2.4.41-alpine
kubernetes.io => Documentation => Search “pod pvc”
Copy the below content |
Remove the lines |
Our final file |
|
|
|
apiVersion: v1
kind: Pod
metadata:
name: tata
namespace: project-tiger
spec:
volumes:
– name: data
persistentVolumeClaim:
claimName: tata-pvc
containers:
– name: task-pv-container
image: httpd:2.4.41-alpine
volumeMounts:
– mountPath: “/tmp/tata-data”
name: data
EOF
kubectl apply -f question7-pv.yaml
kubectl apply -f question7-pvc.yaml
kubectl apply -f question7-pod.yaml
Post checks: How to check if our volume is mounted on this POD?
NAME READY STATUS RESTARTS AGE
tata 1/1 Running 0 50s
[root@master1 ~]# kubectl -n project-tiger exec -it tata — /bin/bash
bash-5.0# df -h
Filesystem Size Used Available Use% Mounted on
overlay 14.0G 8.3G 5.6G 60% /
tmpfs 64.0M 0 64.0M 0% /dev
/dev/sda2 14.0G 8.3G 5.6G 60% /tmp/tata-data
/dev/sda2 14.0G 8.3G 5.6G 60% /etc/hosts
/dev/sda2 14.0G 8.3G 5.6G 60% /dev/termination-log
/dev/sda2 14.0G 8.3G 5.6G 60% /etc/hostname
/dev/sda2 14.0G 8.3G 5.6G 60% /etc/resolv.conf
shm 64.0M 0 64.0M 0% /dev/shm
tmpfs 2.1G 12.0K 2.1G 0% /run/secrets/kubernetes.io/serviceaccount
tmpfs 1.1G 0 1.1G 0% /proc/acpi
tmpfs 64.0M 0 64.0M 0% /proc/kcore
tmpfs 64.0M 0 64.0M 0% /proc/keys
tmpfs 64.0M 0 64.0M 0% /proc/timer_list
tmpfs 1.1G 0 1.1G 0% /proc/scsi
tmpfs 1.1G 0 1.1G 0% /sys/firmware
bash-5.0#
total 0
Question : Create a persistent volume with name app-motor, of capacity 2Gi and
access mode ReadWriteMany. The type of volume is hostPath location is
/srv/app-tata
Soloution:
kubectl config use-context ek8s
In this question, it is asked us to create only PV, thus we will only create PV.
Open
URL : https://kubernetes.io –> Documentation –> Search => PV
Accessmode ==> Open first link and search for “accessmode”
Copy the content link below:
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv0003
spec:
capacity:
storage: 5Gi
volumeMode: Filesystem
accessModes:
– ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: slow
mountOptions:
– hard
– nfsvers=4.1
nfs:
path: /tmp
server: 172.17.0.2
Now, modify the Yaml file as per the question
[root@master1 ~]# cat <<EOF>> question8-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: app-motor
spec:
capacity:
storage: 2Gi
accessModes:
– ReadWriteMany
hostPath:
path: “/srv/app-tata”
EOF
[root@master1 ~]# kubectl apply -f question8-pv.yaml
persistentvolume/app-motor created
[root@master1 ~]# kubectl get pv/app-motor
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
app-motor 2Gi RWX Retain Available manual 14m
You can also check this PV by describe sub-command. It gives you more details.
[root@master1 ~]# kubectl describe pv/app-motor
Name: app-motor
Labels: type=local
Annotations: <none>
Finalizers: [kubernetes.io/pv-protection]
StorageClass: manual
Status: Available
Claim:
Reclaim Policy: Retain
Access Modes: RWX
VolumeMode: Filesystem
Capacity: 2Gi
Node Affinity: <none>
Message:
Source:
Type: HostPath (bare host directory volume)
Path: /srv/app-tata
HostPathType:
Events: <none>
[root@master1 ~]#
[…] Storage 10% Link […]