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

Implement State Persistence for Kubernetes Pods

Part of the power of Kubernetes containers comes from their ephemeral nature. They can be easily created, destroyed, and replaced, and this makes them easy to manage. However, sometimes we need to maintain persistent data that survives beyond the life of a pod. In this lab, we will go through how to implement state persistence using PersistentVolumes and PersistentVolume claims. We will create persistent storage and consume it using a pod.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 1h 0m
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

    Create a descriptor file with vi mysql-pv.yml:

    kind: PersistentVolume
    apiVersion: v1
    metadata:
      name: mysql-pv
    spec:
      storageClassName: localdisk
      capacity:
        storage: 1Gi
      accessModes:
        - ReadWriteOnce
      hostPath:
        path: "/mnt/data"
    

    Create the PersistentVolume.

    kubectl apply -f mysql-pv.yml
    
  2. Challenge

    Create a PersistentVolumeClaim

    Create a descriptor file with vi mysql-pv-claim.yml:

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: mysql-pv-claim
    spec:
      storageClassName: localdisk
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 500Mi
    

    Create the PersistentVolumeClaim:

    kubectl apply -f mysql-pv-claim.yml
    
  3. Challenge

    Create a MySQL Pod configured to use the PersistentVolumeClaim

    Create a descriptor file with vi mysql-pod.yml:

    apiVersion: v1
    kind: Pod
    metadata:
      name: mysql-pod
    spec:
      containers:
      - name: mysql
        image: mysql:5.6
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: password
        volumeMounts:
        - name: mysql-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim
    

    Create the Pod:

    kubectl apply -f mysql-pod.yml
    

    Check the status of the pod with kubectl get pod mysql-pod. After a few moments it should be in the RUNNING status.

    You can also verify that the pod is interacting with the filesystem at /mnt/data on the node. Log in to the node from the Kube master like this, using the same as the password for the kube master:

    ssh [email protected]
    

    Check the contents of the PersistentVolume directory:

    ls /mnt/data
    

    You should see files and directories there related to MySQL. These were created by the pod, meaning that your PersistentVolume and PersistentVolumeClaim are working!

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