Version - 2025.01
Last update : 2025/01/17 15:30
The file system of a container in a pod is ephemeral, i.e. it exists only during the life cycle of the container. If the container is deleted or re-created, the file system is lost.
Volumes enable data to be stored outside the container's file system, while still allowing the container to access it.
A Persistent Volume is an abstract resource that can be consumed by pods. To access the Persistent Volume, the pod needs a Persistent Volume Claim to mount the Persistent Volume in the pod.
Volumes and Persistent Volumes have a Volume Type (Type of Volume). The Volume Type determines the Storage Method of the data. Storage methods include:
Important: For more information on Storage Methods, see the K8s documentation at this page.
Volumes are configured in the pod specification, not the container. The two most important Volume Types are hostPath and emptyDir:
A volumeMount is configured in the container specification, not the pod.
Start by creating the volume.yaml file:
To do: Copy the content from here and paste it into your file.
root@kubemaster:~# vi volume.yaml
root@kubemaster:~# cat volume.yaml
apiVersion: v1
kind: Pod
metadata:
name: volumepod
spec:
restartPolicy: Never
containers:
- name: busybox
image: busybox
command: ['sh', '-c', 'echo Success! > /output/success.txt']
volumeMounts:
- name: myvolume
mountPath: /output
volumes:
- name: myvolume
hostPath:
path: /var/data
Important: This pod will write the string Success! to the file /output/success.txt inside the container and then stop because the value of restartPolicy is Never. The myvolume volume will be mounted on /output in the container, thanks to the volumeMount configuration, and on /var/data/ in the node hosting the pod.
Create the volumepod pod:
root@kubemaster:~# kubectl create -f volume.yaml pod/volumepod created
Identify the node on which the pod runs:
root@kubemaster:~# kubectl get pod volumepod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES volumepod 0/1 Completed 0 3m10s 192.168.150.41 kubenode2.ittraining.loc <none> <none>
Connect to the identified node:
root@kubemaster:~# ssh -l trainee kubenode2 trainee@kubenode2's password: trainee Linux kubenode2.ittraining.loc 4.9.0-19-amd64 #1 SMP Debian 4.9.320-2 (2022-06-30) x86_64 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Sun Sep 4 13:24:39 2022 from 192.168.56.2
Check for the presence and content of the file /var/data/success.txt:
trainee@kubenode2:~$ cat /var/data/success.txt Success!
Return to the kubemaster VM:
trainee@kubenode2:~$ exit logout Connection to kubenode2 closed. root@kubemaster:~#
Now create the shared.yaml file:
To do: Copy the content from here and paste it into your file.
root@kubemaster:~# vi shared.yaml
root@kubemaster:~# cat shared.yaml
apiVersion: v1
kind: Pod
metadata:
name: sharedvolume
spec:
containers:
- name: busybox1
image: busybox
command: ['sh', '-c', 'while true; do echo Success! > /output/output.txt; sleep 5; done']
volumeMounts:
- name: myvolume
mountPath: /output
- name: busybox2
image: busybox
command: ['sh', '-c', 'while true; do cat /input/output.txt; sleep 5; done']
volumeMounts:
- name: myvolume
mountPath: /input
volumes:
- name: myvolume
emptyDir: {}
Important: This file will create two pods. The first, busybox1, will write the string Success! to the container's /output/output.txt file every 5 seconds. The /output directory is known as myvolume. This same volume will be available to the second pod container, busybox2, where it will be mounted at /input. The busybox2 container will print the contents of /input/output.txt on standard output every 5 seconds.
Create the two pods:
root@kubemaster:~# kubectl create -f shared.yaml pod/sharedvolume created
Check that both pods are running:
root@kubemaster:~# kubectl get pods sharedvolume NAME READY STATUS RESTARTS AGE sharedvolume 2/2 Running 0 5m55s
Now look at the logs for the second container:
root@kubemaster:~# kubectl logs sharedvolume -c busybox2 Success! Success! Success! Success! Success! Success! Success! Success! Success! Success! Success! Success! Success! Success! Success!
Important: Note that busybox2 has printed the contents of the file /input/output.txt to its standard output.
Create the localdisk.yaml file to define the StorageClass called localdisk:
To do: Copy the content from here and paste it into your file.
root@kubemaster:~# vi localdisk.yaml root@kubemaster:~# cat localdisk.yaml apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: localdisk provisioner: kubernetes.io/no-provisioner allowVolumeExpansion: true
Important: Note that the allowVolumeExpansion value is true.
Create the StorageClass localdisk :
root@kubemaster:~# kubectl create -f localdisk.yaml
Create the mypv.yaml file to define the PersistentVolume called mypv:
To do: Copy the content from here and paste it into your file.
root@kubemaster:~# vi mypv.yaml
root@kubemaster:~# cat mypv.yaml
kind: PersistentVolume
apiVersion: v1
metadata:
name: mypv
spec:
storageClassName: localdisk
persistentVolumeReclaimPolicy: Recycle
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /var/output
Important: Note the value of accessModes.
There are four types of accessModes:
Important: Note that the persistentVolumeReclaimPolicy value is Recycle.
Important: AccessMode availability depends on the type of storage service. The ReadWriteOnce mode is always available. For more information on accessModes, see this page.
There are three types of PersistentVolumeReclaimPolicy:
Create the PersistentVolume mypv:
root@kubemaster:~# kubectl create -f mypv.yaml persistentvolume/mypv created
Check the PersistentVolume status:
root@kubemaster:~# kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE mypv 1Gi RWO Recycle Available localdisk 5m28s
Important: Note that the STATUS value is Available.
Create the mypvc.yaml file to define the PersistentVolumeClaim called my-pvc:
To do: Copy the content from here and paste it into your file.
root@kubemaster:~# vi mypvc.yaml
root@kubemaster:~# cat mypvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
storageClassName: localdisk
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100Mi
Important: Note that the value of storageClassName is localdisk.
Create the PersistentVolumeClaim my-pvc:
root@kubemaster:~# kubectl create -f mypvc.yaml persistentvolumeclaim/my-pvc created
Check the PersistentVolume status:
root@kubemaster:~# kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE mypv 1Gi RWO Recycle Bound default/my-pvc localdisk 9m33s
Important: Note that the STATUS value is Bound. Also note that a PersistentVolume can only be associated with one PersistentVolumeClaim at a time.
Check the PersistentVolumeClaim status:
root@kubemaster:~# kubectl get pvc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE my-pvc Bound mypv 1Gi RWO localdisk 72s
Important: Note that the STATUS value is Bound.
Create the mypvcpod.yaml file to define the pod called pv-pod:
To do: Copy the content from here and paste it into your file.
root@kubemaster:~# vi mypvcpod.yaml
root@kubemaster:~# cat mypvcpod.yaml
apiVersion: v1
kind: Pod
metadata:
name: pv-pod
spec:
restartPolicy: Never
containers:
- name: busybox
image: busybox
command: ['sh', '-c', 'echo Success! > /output/success.txt']
volumeMounts:
- name: pv-storage
mountPath: /output
volumes:
- name: pv-storage
persistentVolumeClaim:
claimName: my-pvc
Create the pv-pod pod:
root@kubemaster:~# kubectl create -f mypvcpod.yaml pod/pv-pod created
Important: Note that the pod uses the persistentVolumeClaim my-pvc which is mounted on /output in the busybox container.
Modify the storage: value of the PersistentVolumeClaim:
root@kubemaster:~# kubectl edit pvc my-pvc --record
...
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 200Mi
storageClassName: localdisk
volumeMode: Filesystem
volumeName: mypv
...
Save the modification:
root@kubemaster:~# kubectl edit pvc my-pvc --record Flag --record has been deprecated, --record will be removed in the future persistentvolumeclaim/my-pvc edited
Important: Note the edit confirmation message.
Delete the pv-pod pod and the my-pvc PersistentVolumeClaim:
root@kubemaster:~# kubectl delete pod pv-pod pod “pv-pod” deleted root@kubemaster:~# kubectl delete pvc my-pvc persistentvolumeclaim “my-pvc” deleted
Check the PersistentVolume status:
root@kubemaster:~# kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE mypv 1Gi RWO Recycle Available localdisk 23m
Important: Note that the STATUS value is Available again.
Copyright © 2025 Hugh Norris