
PersistentVolumes are storage that is available to a Kubernetes cluster. PersistentVolumeClaims enable Pods to access PersistentVolumes.
Without PersistentVolumeClaims Pods are mostly ephemeral, so you should use PersistentVolumeClaims for any data that you expect to survive Pod scaling, updating, or migrating.
In this lab, you learn how to perform the following tasks:
- Create manifests for PersistentVolumes (PVs) and PersistentVolumeClaims (PVCs) for Google Cloud persistent disks (dynamically created or existing)
- Mount Google Cloud persistent disk PVCs as volumes in Pods
- Use manifests to create StatefulSets
- Mount Google Cloud persistent disk PVCs as volumes in StatefulSets
- Verify the connection of Pods in StatefulSets to particular PVs as the Pods are stopped and restarted
Task 1. Create PVs and PVCs
In this task, you create a PVC, which triggers Kubernetes to automatically create a PV.
Connect to the lab GKE cluster
- In Cloud Shell, type the following command to set the environment variable for the zone and cluster name.
export my_zone=us-central1-a
export my_cluster=standard-cluster-1

2. Configure access to your cluster for kubectl:
gcloud container clusters get-credentials $my_cluster --zone $my_zone

Create and apply a manifest with a PVC
Most of the time, you don’t need to directly configure PV objects or create Compute Engine persistent disks. Instead, you can create a PVC, and Kubernetes automatically provisions a persistent disk for you.
You create the PVC in this task using the pvc-demo.yaml
manifest file that has been provided for you. This creates a 30 gigabyte PVC called hello-web-disk
that can be mounted as read-write volume on a single node at a time.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: hello-web-disk
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 30Gi
- In Cloud Shell enter the following command to clone the repository to the lab Cloud Shell.
git clone https://github.com/GoogleCloudPlatform/training-data-analyst
2. Create a soft link as a shortcut to the working directory.
ln -s ~/training-data-analyst/courses/ak8s/v1.1 ~/ak8s

3. Change to the directory that contains the sample files for this lab.
cd ~/ak8s/Storage/

4. To show that you currently have no PVCs, execute the following command:
kubectl get persistentvolumeclaim

5. To create the PVC, execute the following command:
kubectl apply -f pvc-demo.yaml


6. To show your newly created PVC, execute the following command:
kubectl get persistentvolumeclaim


Task 2. Mount and verify Google Cloud persistent disk PVCs in Pods
In this task, you attach your persistent disk PVC to a Pod. You mount the PVC as a volume as part of the manifest for the Pod.
Mount the PVC to a Pod
The manifest file pod-volume-demo.yaml
deploys an nginx container, attaches the pvc-demo-volume
to the Pod and mounts that volume to the path /var/www/html
inside the nginx container. Files saved to this directory inside the container will be saved to the persistent volume and persist even if the Pod and the container are shutdown and recreated.
pod-volume-demo.yaml:
kind: Pod
apiVersion: v1
metadata:
name: pvc-demo-pod
spec:
containers:
- name: frontend
image: nginx
volumeMounts:
- mountPath: "/var/www/html"
name: pvc-demo-volume
volumes:
- name: pvc-demo-volume
persistentVolumeClaim:
claimName: hello-web-disk
- To create the Pod with the volume, execute the following command:
kubectl apply -f pod-volume-demo.yaml
2. List the Pods in the cluster.
kubectl get pods

If you do this quickly after creating the Pod you will see the status listed as “ContainerCreating” while the volume is mounted before the status changes to “Running”.
3. To verify the PVC is accessible within the Pod, you must gain shell access to your Pod. To start the shell session, execute the following command:
kubectl exec -it pvc-demo-pod -- sh

4. To create a simple text message as a web page in the Pod enter the following commands:
echo Test webpage in a persistent volume!>/var/www/html/index.htmlchmod +x /var/www/html/index.html
5. Verify the text file contains your message.
cat /var/www/html/index.html

6. Enter the following command to leave the interactive shell on the nginx container.
exit
Test the persistence of the PV
You will now delete the Pod from the cluster, confirm that the PV still exists, then redeploy the Pod and verify the contents of the PV remain intact.
- Delete the pvc-demo-pod.
kubectl delete pod pvc-demo-pod
2. List the Pods in the cluster.
kubectl get pods

There should be no Pods on the cluster.
3. To show your PVC, execute the following command:
kubectl get persistentvolumeclaim

Your PVC still exists, and was not deleted when the Pod was deleted.
4. Redeploy the pvc-demo-pod.
kubectl apply -f pod-volume-demo.yaml
5. List the Pods in the cluster.
kubectl get pods

The Pod will deploy and the status will change to “Running” faster this time because the PV already exists and does not need to be created.
6. To verify the PVC is is still accessible within the Pod, you must gain shell access to your Pod. To start the shell session, execute the following command:
kubectl exec -it pvc-demo-pod -- sh
7. To verify that the text file still contains your message execute the following command:
cat /var/www/html/index.html

The contents of the persistent volume were not removed, even though the Pod was deleted from the cluster and recreated.
8. Exit out of container by entering “exit” command.
Task 3. Create StatefulSets with PVCs
In this task, you use your PVC in a StatefulSet. A StatefulSet is like a Deployment, except that the Pods are given unique identifiers.
Release the PVC
- Before you can use the PVC with the statefulset, you must delete the Pod that is currently using it. Execute the following command to delete the Pod:
kubectl delete pod pvc-demo-pod
2. Confirm the Pod has been removed.
kubectl get pods

Create a StatefulSet
The manifest file statefulset-demo.yaml
creates a StatefulSet that includes a LoadBalancer service and three replicas of a Pod containing an nginx container and a volumeClaimTemplate for 30 gigabyte PVCs with the name hello-web-disk
. The nginx containers mount the PVC called hello-web-disk
at /var/www/html
as in the previous task.
statefulset-demo.yaml:
kind: Service
apiVersion: v1
metadata:
name: statefulset-demo-service
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9376
type: LoadBalancer
---apiVersion: apps/v1
kind: StatefulSet
metadata:
name: statefulset-demo
spec:
selector:
matchLabels:
app: MyApp
serviceName: statefulset-demo-service
replicas: 3
updateStrategy:
type: RollingUpdate
template:
metadata:
labels:
app: MyApp
spec:
containers:
- name: stateful-set-container
image: nginx
ports:
- containerPort: 80
name: http
volumeMounts:
- name: hello-web-disk
mountPath: "/var/www/html"
volumeClaimTemplates:
- metadata:
name: hello-web-disk
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 30Gi
- To create the StatefulSet with the volume, execute the following command:
kubectl apply -f statefulset-demo.yaml

You now have a statefulset running behind a service named statefulset-demo-service
.

Verify the connection of Pods in StatefulSets
- Use “kubectl describe” to view the details of the StatefulSet:
kubectl describe statefulset statefulset-demo

Note the event status at the end of the output. The service and statefulset created successfully.

2. List the Pods in the cluster.
kubectl get pods

3. To list the PVCs, execute the following command:
kubectl get pvc


The original hello-web-disk is still there and you can now see the individual PVCs that were created for each Pod in the new statefulset Pod.
4. Use “kubectl describe” to view the details of the first PVC in the StatefulSet:
kubectl describe pvc hello-web-disk-statefulset-demo-0

Task 4. Verify the persistence of Persistent Volume connections to Pods managed by StatefulSets
In this task, you verify the connection of Pods in StatefulSets to particular PVs as the Pods are stopped and restarted.
- To verify that the PVC is accessible within the Pod, you must gain shell access to your Pod. To start the shell session, execute the following command:
kubectl exec -it statefulset-demo-0 -- sh
2. Verify that there is no index.html
text file in the /var/www/html
directory.
cat /var/www/html/index.html

3. To create a simple text message as a web page in the Pod enter the following commands:
echo Test webpage in a persistent volume!>/var/www/html/index.html
chmod +x /var/www/html/index.html

4. Verify the text file contains your message.
cat /var/www/html/index.html

5. Enter the following command to leave the interactive shell on the nginx container.
exit
6. Delete the Pod where you updated the file on the PVC.
kubectl delete pod statefulset-demo-0
7. List the Pods in the cluster.
kubectl get pods

You will see that the StatefulSet is automatically restarting the statefulset-demo-0
Pod.
Note: You need to wait until the Pod status shows that it is running again.
8. Connect to the shell on the new statefulset-demo-0
Pod.
kubectl exec -it statefulset-demo-0 -- sh
9. Verify that the text file still contains your message.
cat /var/www/html/index.html

The StatefulSet restarts the Pod and reconnects the existing dedicated PVC to the new Pod ensuring that the data for that Pod is preserved.
Exit out of container.
This Concludes our lab demonstration for “Configuring Persistent Storage for Google Kubernetes Engine”.
Your article helped me a lot, is there any more related content? Thanks!
thank you