- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Cloud

Creating Persistent Storage for Pods in Kubernetes
Pods in Kubernetes are ephemeral, which makes the local container filesytem unusable, as you can never ensure the pod will remain. To decouple your storage from your pods, you will be creating a persistent volume to mount for use by your pods. You will be deploying a redis image. You will first create the persistent volume, then create the pod YAML for deploying the pod to mount the volume. You will then delete the pod and create a new pod, which will access that same volume.

Lab Info
Table of Contents
-
Challenge
Create a PersistentVolume.
-
Use the following YAML spec for the PersistentVolume named
redis-pv.yaml
:apiVersion: v1 kind: PersistentVolume metadata: name: redis-pv spec: storageClassName: "" capacity: storage: 1Gi accessModes: - ReadWriteOnce hostPath: path: "/mnt/data"
-
Then, create the PersistentVolume:
kubectl apply -f redis-pv.yaml
-
-
Challenge
Create a PersistentVolumeClaim.
-
Use the following YAML spec for the PersistentVolumeClaim named
redis-pvc.yaml
:apiVersion: v1 kind: PersistentVolumeClaim metadata: name: redisdb-pvc spec: storageClassName: "" accessModes: - ReadWriteOnce resources: requests: storage: 1Gi
-
Then, create the PersistentVolumeClaim:
kubectl apply -f redis-pvc.yaml
-
-
Challenge
Create the redispod image, with a mounted volume to mount path `/data`
-
Use the following YAML spec for the pod named
redispod.yaml
:apiVersion: v1 kind: Pod metadata: name: redispod spec: containers: - image: redis name: redisdb volumeMounts: - name: redis-data mountPath: /data ports: - containerPort: 6379 protocol: TCP volumes: - name: redis-data persistentVolumeClaim: claimName: redisdb-pvc
-
Then, create the pod:
kubectl apply -f redispod.yaml
-
Verify the pod was created:
kubectl get pods
-
-
Challenge
Connect to the container and write some data.
-
Connect to the container and run the
redis-cli
:kubectl exec -it redispod -- redis-cli
-
Set the key space
server:name
and value "redis server":SET server:name "redis server"
-
Run the
GET
command to verify the value was set:GET server:name
-
Exit the
redis-cli
:QUIT
-
-
Challenge
Delete `redispod` and create a new pod named `redispod2`.
-
Delete the existing
redispod
:kubectl delete pod redispod
-
Open the file
redispod.yaml
and change line 4 fromname: redispod
to:name: redispod2
-
Create a new pod named
redispod2
:kubectl apply -f redispod.yaml
-
-
Challenge
Verify the volume has persistent data.
-
Connect to the container and run
redis-cli
:kubectl exec -it redispod2 -- redis-cli
-
Run the
GET
command to retrieve the data written previously:GET server:name
-
Exit the
redis-cli
:QUIT
-
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.