Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

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.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 30m
Published
Clock icon Mar 31, 2019

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Create a PersistentVolume.

    1. 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"
      
    2. Then, create the PersistentVolume:

      kubectl apply -f redis-pv.yaml
      
  2. Challenge

    Create a PersistentVolumeClaim.

    1. 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
      
    2. Then, create the PersistentVolumeClaim:

      kubectl apply -f redis-pvc.yaml
      
  3. Challenge

    Create the redispod image, with a mounted volume to mount path `/data`

    1. 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
      
    2. Then, create the pod:

      kubectl apply -f redispod.yaml
      
    3. Verify the pod was created:

      kubectl get pods
      
  4. Challenge

    Connect to the container and write some data.

    1. Connect to the container and run the redis-cli:

      kubectl exec -it redispod -- redis-cli
      
    2. Set the key space server:name and value "redis server":

      SET server:name "redis server"
      
    3. Run the GET command to verify the value was set:

      GET server:name
      
    4. Exit the redis-cli:

      QUIT
      
  5. Challenge

    Delete `redispod` and create a new pod named `redispod2`.

    1. Delete the existing redispod:

      kubectl delete pod redispod
      
    2. Open the file redispod.yaml and change line 4 from name: redispod to:

      name: redispod2
      
    3. Create a new pod named redispod2:

      kubectl apply -f redispod.yaml
      
  6. Challenge

    Verify the volume has persistent data.

    1. Connect to the container and run redis-cli:

      kubectl exec -it redispod2 -- redis-cli
      
    2. Run the GET command to retrieve the data written previously:

      GET server:name
      
    3. Exit the redis-cli:

      QUIT
      

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans