"Mastering Kubernetes: A Step-by-Step PVC Example"

Understanding Kubernetes Persistent Volumes and Persistent Volume Claims: An Example

In the dynamic world of container orchestration, Kubernetes offers a robust ecosystem for managing and scaling applications. One of the key aspects of this is persistent storage, which is handled through Persistent Volumes (PV) and Persistent Volume Claims (PVC). Let's dive into an example to understand how to create and use PVCs in Kubernetes.

What are Persistent Volumes and Persistent Volume Claims?

Before we delve into the example, let's briefly understand what Persistent Volumes and Persistent Volume Claims are.

  • Persistent Volumes (PV) are network storage resources provided by various storage providers like EBS, NFS, or even cloud storage services. They are independent of the lifecycle of the pods and can outlive them.
  • Persistent Volume Claims (PVC) are requests for storage by a pod. They are bound to a PV, and the pod can use the storage until the PVC is deleted.

Creating a Persistent Volume

Let's start by creating a PV. For this example, we'll use an EBS volume. Here's a simple YAML definition:

a diagram with the words how to learn kubernets on it and an image of
a diagram with the words how to learn kubernets on it and an image of

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  awsElasticBlockStore:
    fsType: ext4
    volumeID: "vol-abcdefghijklmnopqrstuvwxyz"

Creating a Persistent Volume Claim

Now, let's create a PVC that will use this PV. Here's a YAML definition for a PVC:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi

In this PVC, we're requesting 3Gi of storage with the ReadWriteOnce access mode. This PVC will be bound to the PV we created earlier.

Using the PVC in a Pod

Finally, let's use this PVC in a pod. Here's a YAML definition for a pod that mounts the PVC:

the kubernets concept is depicted in this graphic above it's description
the kubernets concept is depicted in this graphic above it's description

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: nginx:latest
    volumeMounts:
    - name: my-volume
      mountPath: /usr/share/nginx/html
  volumes:
  - name: my-volume
    persistentVolumeClaim:
      claimName: my-pvc

In this pod, we're mounting the PVC to the Nginx HTML directory. This means that any data written to this directory will be persistent and not lost when the pod is restarted.

Accessing the PVC from the Pod

To access the PVC from the pod, you can use the following environment variable:

export PVC_NAME=my-pvc
export PVC_PATH=/usr/share/nginx/html

Then, in your pod, you can use these environment variables to access the PVC:

a poster with the words kubernets network and other information on it's side
a poster with the words kubernets network and other information on it's side

echo "Hello, World!" > $PVC_PATH/index.html

This will write a simple HTML file to the PVC, which will be persistent and accessible even if the pod is restarted.

Conclusion

In this article, we've walked through an example of creating and using a Persistent Volume Claim in Kubernetes. Understanding how to use PVCs is crucial for ensuring that your data is persistent and not lost when pods are restarted or rescheduled. With this knowledge, you can now manage persistent storage in your Kubernetes clusters effectively.

how kubernets works end - to - end with the blueprinting application
how kubernets works end - to - end with the blueprinting application
the kubernets networking diagram
the kubernets networking diagram
La vie et l'héritage de Mahomet, prophète de l'islam
La vie et l'héritage de Mahomet, prophète de l'islam
Investigating Kubernetes from Inside | Apriorit
Investigating Kubernetes from Inside | Apriorit
Learn Kubernetes in Under 3 Hours: A Detailed Guide to Orchestrating Containers
Learn Kubernetes in Under 3 Hours: A Detailed Guide to Orchestrating Containers
#devops #aws #azure #gcp #kubernetes #ckad #devops #cloudnative #kodekloud #containers | REDDY M.
#devops #aws #azure #gcp #kubernetes #ckad #devops #cloudnative #kodekloud #containers | REDDY M.
the kuberites debug chat sheet is shown in blue and red colors
the kuberites debug chat sheet is shown in blue and red colors
Openshift vs Kubernetes - ClickIT
Openshift vs Kubernetes - ClickIT
What Is Kubernetes? K8s Architecture for Cloud-Native Networks
What Is Kubernetes? K8s Architecture for Cloud-Native Networks
Kubernetes Basics Explained in 10 Minutes [ DevOps ] || Techtter ||
Kubernetes Basics Explained in 10 Minutes [ DevOps ] || Techtter ||
Kubernetes
Kubernetes
the kubernets architecture components diagram is shown in blue and pink, with text on
the kubernets architecture components diagram is shown in blue and pink, with text on
the components of kubernets are labeled in red and blue, with arrows pointing to
the components of kubernets are labeled in red and blue, with arrows pointing to
Kubernetes cost reduction Digitalocean Kubernetes Cost, Budgeting, Coding, Instagram
Kubernetes cost reduction Digitalocean Kubernetes Cost, Budgeting, Coding, Instagram
the kubernets explained workflow diagram
the kubernets explained workflow diagram
AI-Native Product Engineering & Enterprise Solutions
AI-Native Product Engineering & Enterprise Solutions
Top 10 must-know Kubernetes design patterns
Top 10 must-know Kubernetes design patterns
Docker Vs Kubernetes
Docker Vs Kubernetes
¿Qué es Kubernetes? | Microsoft Azure
¿Qué es Kubernetes? | Microsoft Azure
CI/CD for Kubernetes With Jenkins and Spinnaker
CI/CD for Kubernetes With Jenkins and Spinnaker
Kubernetes_workshop Alternatives
Kubernetes_workshop Alternatives
kubernetes consulting companies
kubernetes consulting companies
two diagrams showing the different types of web pages and what they are used to create them
two diagrams showing the different types of web pages and what they are used to create them