Services DevOps DevSecOps Cloud Consulting Infrastructure Automation Managed Services AIOps MLOps DataOps Microservices 🔐 Private AINEW Solutions DevOps Transformation CI/CD Automation Platform Engineering Security Automation Zero Trust Security Compliance Automation Cloud Migration Kubernetes Migration Cloud Cost Optimisation AI-Powered Operations Data Platform Modernisation SRE & Observability Legacy Modernisation Managed IT Services 🔐 Private AI DeploymentNEW Products ✨ ZippyOPS AINEW 🛡️ ArmorPlane 🔒 DevSecOpsAsService 🖥️ LabAsService 🤝 Collab 🧪 SandboxAsService 🎬 DemoAsService Bootcamp 🔄 DevOps Bootcamp ☁️ Cloud Engineering 🔒 DevSecOps 🛡️ Cloud Security ⚙️ Infrastructure Automation 📡 SRE & Observability 🤖 AIOps & MLOps 🧠 AI Engineering 🎓 ZOLS — Free Learning Company About Us Projects Careers Get in Touch

How to Create and Manage Persistent Volumes in Kubernetes

Understanding Persistent Volumes in Kubernetes

In Kubernetes, a volume acts as a directory accessible by containers within a pod. Unlike Docker volumes, which were tied to specific pods and lost once the pod was deleted, Kubernetes Persistent volumes offer more flexibility. They are designed to be used by multiple containers within the same pod and persist beyond the lifecycle of individual containers. One of the standout features of Kubernetes volumes is their ability to support various types of storage. As a result, pods can use multiple storage solutions simultaneously.

When managing Kubernetes storage, two critical components come into play: Persistent Volumes (PV) and Persistent Volume Claims (PVC). These elements enable better storage management, ensuring data is not lost when pods are recreated or deleted.

Kubernetes Persistent Volumes setup for containerized applications

Persistent Volume vs. Persistent Volumes Claim

What is a Persistent Volume (PV)?

A Persistent Volume (PV) is a piece of network storage provisioned by the Kubernetes administrator. It is an independent resource within the cluster, not tied to any individual pod. This decoupling ensures that data persists even when pods are deleted or rescheduled. Kubernetes administrators manage PVs, and they can be configured to meet specific storage needs, such as NFS or block storage.

What is a Persistent Volume Claim (PVC)?

A Persistent Volume Claim (PVC) is a request for storage by Kubernetes users or applications. It specifies the amount of storage required, access modes, and other attributes. The beauty of PVCs is that users don’t need to understand the underlying storage infrastructure—Kubernetes handles the rest. PVCs must be created within the same namespace as the pod that will use the storage.

Steps to Create and Manage Persistent Volumes in Kubernetes

Step 1: Creating a Persistent Volume (PV)

To create a PV, define the required storage attributes in a YAML file. Here’s an example configuration for a 10Gi NFS volume:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0001
spec:
  capacity:
    storage: 10Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  mountOptions:
    - hard
    - nfsvers=4.1
  nfs:
    path: /tmp
    server: 172.17.0.2

After defining the PV, use kubectl to create it in Kubernetes:

kubectl create -f Persistent-Volume.yaml

To check if the PV was created successfully:

kubectl get pv

You should see output similar to this:

NAME     CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM   STORAGECLASS   REASON   AGE
pv0001   10Gi       RWO            Retain           Bound     default/myclaim-1              5m40s

Step 2: Creating a Persistent Volume Claim (PVC)

Next, create a PVC to request the storage. Below is a sample PVC YAML definition requesting 3Gi of storage:

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

Deploy the PVC with:

kubectl create -f PersistentVolumeClaim.yaml

To verify that the PVC is bound to the PV:

kubectl get pvc

You should see output like this:

NAME        STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
myclaim-1   Bound    pv0001   10Gi       RWO             4h2m

Step 3: Using PVC in a Pod

Once the PVC is bound, you can use it in a pod. Below is a YAML configuration for a pod that uses the PVC we just created:

apiVersion: v1
kind: Pod
metadata:
  name: mypod
  labels:
    name: frontendhttp
spec:
  containers:
    - name: myfrontend
      image: nginx
      ports:
        - containerPort: 80
      volumeMounts:
        - mountPath: "/usr/share/tomcat/html"
          name: mypd
  volumes:
    - name: mypd
      persistentVolumeClaim:
        claimName: myclaim-1

To create the pod, run:

kubectl create -f pv-pvc.yaml

Verify that the pod is running by using:

kubectl get pods

The output should indicate that the pod is successfully running:

NAME    READY   STATUS   RESTARTS   AGE
mypod   1/1     Running  0          2m

Kubernetes Volumes: Key Benefits

The ability to use Persistent Volumes and Persistent Volume Claims gives Kubernetes users significant flexibility. This setup is ideal for stateful applications, as it ensures that the data is persistent and survives pod restarts. Additionally, Kubernetes volumes allow you to manage storage across multiple types of storage systems, such as NFS, block storage, and cloud-based solutions.

Integrating Kubernetes Volumes with DevOps Practices

At ZippyOPS, we help businesses manage their cloud infrastructure and containerized applications with cutting-edge DevOps solutions. Whether you need to implement DevOps, DevSecOps, DataOps, or MLOps, our team provides consulting, implementation, and managed services to optimize your workflows.

We specialize in Kubernetes management, microservices, and automated operations, offering expertise in security, infrastructure, and cloud solutions. For a seamless Kubernetes storage setup or to enhance your entire infrastructure, explore our comprehensive solutions:

For tailored advice or to optimize your Kubernetes operations, reach out to us directly at sales@zippyops.com.

Conclusion

Kubernetes volumes, especially Persistent Volumes and Persistent Volume Claims, offer significant advantages for managing storage in containerized environments. By using PVs and PVCs, you can ensure data persistence across pod lifecycles and maintain high availability for stateful applications. To get the most out of Kubernetes storage, work with experts who can streamline your setup, such as those at ZippyOPS.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top