Kubernetes > Pods > Volumes > GCE Persistent Disk
We can attach persistent volumes to Pods, these volumes remain unchanged even when there is a Pod failure, Google compute engine standard persistent disk is an example of persistent volume on the Cloud.
Let’s create a blank disk in the same zone as the Kubernetes cluster
To view the list of compute engine disks
networkandcode@cloudshell:~ (kubernetes-cka-224606)$ gcloud compute disks list
NAME ZONE SIZE_GB TYPE STATUS
disk-1 us-east1-b 10 pd-standard READY
gke-standard-cluster-1-default-pool-a97a9634-019h us-central1-a 100 pd-standard READY
gke-standard-cluster-1-default-pool-a97a9634-glnz us-central1-a 100 pd-standard READY
gke-standard-cluster-1-default-pool-a97a9634-rxrw us-central1-a 100 pd-standard READY
networkandcode@cloudshell:~ (kubernetes-cka-224606)$
We need to refer the name of this disk, in our Pod definition
Let’s view the Pod definition
networkandcode@cloudshell:~ (kubernetes-cka-224606)$ cat podWithPersistentVolume.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: apache-pod
spec:
containers:
- name: apache-container
image: httpd
volumeMounts:
- name: apache-volume
mountPath: /tmp/apache
volumes:
- name: apache-volume
gcePersistentDisk:
pdName: disk-1
...
Let’s create the Pod
networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl create -f podWithPersistentVolume.yaml
pod "apache-pod" created
The Pod is successfully running
networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl get pods
NAME READY STATUS RESTARTS AGE
apache-pod 1/1 Running 0 20s
Let’s view more details about the Pod
networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl describe po/apache-pod
…TRUNCATED
Volumes:
apache-volume:
Type: GCEPersistentDisk (a Persistent Disk resource in Google Compute Engine)
PDName: disk-1
…TRUNCATED
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 50s default-scheduler Successfully assigned default/apache-pod to gke-standard-cluster-1-defaul
t-pool-a97a9634-glnz
Normal SuccessfulAttachVolume 42s attachdetach-controller AttachVolume.Attach succeeded for volume "apache-volume"
Normal Pulling 40s kubelet, gke-standard-cluster-1-default-pool-a97a9634-glnz pulling image "httpd"
Normal Pulled 40s kubelet, gke-standard-cluster-1-default-pool-a97a9634-glnz Successfully pulled image "httpd"
Normal Created 40s kubelet, gke-standard-cluster-1-default-pool-a97a9634-glnz Created container
Normal Started 40s kubelet, gke-standard-cluster-1-default-pool-a97a9634-glnz Started container
Let’s login to the Pod and launch bash
networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl exec -it apache-pod -- /bin/bash
root@apache-pod:/usr/local/apache2#
To change the directory to the volume mount path, as specified in the Pod definition
root@apache-pod:/usr/local/apache2# cd /tmp/apache
Let’s create a text file and add some text to it
root@apache-pod:/tmp/apache# echo "Sample Text" > test.txt
root@apache-pod:/tmp/apache# ls
lost+found test.txt
root@apache-pod:/tmp/apache# cat test.txt
Sample Text
To exit out of the Pod
root@apache-pod:/tmp/apache# exit
exit
To delete the Pod
networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl delete pod apache-pod
pod "apache-pod" deleted
networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl get pods
No resources found.
To create the Pod again
networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl apply -f podWithPersistentVolume.yaml
pod "apache-pod" created
To getinto the bash prompt of the Pod
networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl exec -it apache-pod -- /bin/bash
To check the contents of the volume mount path
root@apache-pod:/usr/local/apache2# ls /tmp/apache/
lost+found test.txt
The file is still there as the volume is persistent
root@apache-pod:/usr/local/apache2# cat /tmp/apache/test.txt
Sample Text
--end-of-post--