Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • View All Red Hat Products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Secure Development & Architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • Product Documentation
    • API Catalog
    • Legacy Documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Create and manage local persistent volumes with CodeReady Containers

April 20, 2022
JooHo Lee
Related topics:
ContainersKubernetes
Related products:
Developer ToolsRed Hat OpenShift LocalRed Hat OpenShift Container Platform

Share:

    A persistent volume (PV) is a common way to preserve data in case a container is accidentally lost in Kubernetes or Red Hat OpenShift. This article shows you how to manage persistent volumes with the NFS Provisioner Operator that I developed.

    Difficulties with PVs

    In the early days of container-based development, each user had to ask an administrator to create a PV for that user's containers. Usually the administrator created 100 PVs in advance when the cluster was installed. The administrator also had to clean up the used PVs when they were released. Obviously, this process was inefficient and really burdensome.

    Dynamic provisioning using StorageClass was developed to solve this problem. With StorageClass, you no longer have to manually manage your PVs—a provisioner manages them for you. Sounds good, right?

    But the next question is how to set up the StorageClass on the cluster without cost. If you can afford it, the easiest way is to use Red Hat OpenShift Dedicated, which provides the default gp2 StorageClass. But it is not free.

    Let's say you want to play around with an OpenShift cluster installed on your laptop using Red Hat CodeReady Containers. The environment is absolutely free and under your control. Wouldn't it be great if this cluster had a StorageClass? With such an environment, you could test most scenarios without charge.

    The NFS Provisioner Operator is open source and available on OperatorHub.io, which means that it can be easily installed via OpenShift's OperatorHub menu. The Operator uses the Kubernetes NFS subdir external provisioner from kuberentes-sigs internally.

    Set up persistent volumes anywhere

    To start, you need to have an OpenShift cluster, version 4.9.15 or later, and to log into the cluster with the cluster-admin role user.

    Begin by installing the NFS Provisioner Operator:

    # Login
    oc login -u kubeadmin -p kubeadmin https://api.crc.testing:6443
    
    # Create a new namespace
    oc new-project nfsprovisioner-operator
    
    # Deploy NFS Provisioner operator in the terminal (You can also use OpenShift Console
    cat << EOF | oc apply -f -  
    apiVersion: operators.coreos.com/v1alpha1
    kind: Subscription
    metadata:
      name: nfs-provisioner-operator
      namespace: openshift-operators
    spec:
      channel: alpha
      installPlanApproval: Automatic
      name: nfs-provisioner-operator
      source: community-operators
      sourceNamespace: openshift-marketplace
    EOF

    Next, create a directory in the node and add a label to that node:

    # Check nodes
    oc get nodes
    NAME                 STATUS   ROLES           AGE   VERSION
    crc-8rwmc-master-0   Ready    master,worker   54d   v1.22.3+e790d7f
    
    # Set Env variable for the target node name
    export target_node=$(oc get node --no-headers -o name|cut -d'/' -f2)
    oc label node/${target_node} app=nfs-provisioner
    
    # ssh to the node
    oc debug node/${target_node}
    
    # Create a directory and set up the Selinux label.
    $ chroot /host
    $ mkdir -p /home/core/nfs
    $ chcon -Rvt svirt_sandbox_file_t /home/core/nfs
    $ exit; exit

    Now you need to deploy an NFS server using the created folder on a HostPath volume. Note that you could use an existing persistent volume claim (PVC) for the NFS server as well.

    # Create NFSProvisioner Custom Resource
    cat << EOF | oc apply -f -  
    apiVersion: cache.jhouse.com/v1alpha1
    kind: NFSProvisioner
    metadata:
      name: nfsprovisioner-sample
      namespace: nfsprovisioner-operator
    spec:
      nodeSelector: 
        app: nfs-provisioner
      hostPathDir: "/home/core/nfs"
    EOF
    
    # Check if NFS Server is running
    oc get pod
    NAME                               READY   STATUS    RESTARTS   AGE
    nfs-provisioner-77bc99bd9c-57jf2   1/1     Running   0          2m32s

    Finally, you need to make the NFS StorageClass the default:

    # Update annotation of the NFS StorageClass
    oc patch storageclass nfs -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
    
    # Check the default next to nfs StorageClass
    oc get sc
    NAME            PROVISIONER       RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
    nfs (default)   example.com/nfs   Delete          Immediate           false                  4m29s

    Congratulations—you have a StorageClass. Verify it as follows:

    # Create a test PVC
    oc apply -f https://raw.githubusercontent.com/Jooho/jhouse_openshift/master/test_cases/operator/test/test-pvc.yaml
    persistentvolumeclaim/nfs-pvc-example created
    
    # Check the test PV/PVC
    oc get pv, pvc
    
    NAME                                                        CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM                                                 STORAGECLASS   REASON   AGE
    persistentvolume/pvc-e30ba0c8-4a41-4fa0-bc2c-999190fd0282   1Mi        RWX            Delete           Bound       nfsprovisioner-operator/nfs-pvc-example               nfs                     5s
    
    NAME                                    STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
    persistentvolumeclaim/nfs-pvc-example   Bound    pvc-e30ba0c8-4a41-4fa0-bc2c-999190fd0282   1Mi        RWX            nfs            5s

    The output shown here indicates that the NFS server, NFS provisioner, and NFS StorageClass are all working fine. You can use the NFS StorageClass for any test scenarios that need PVC.

    CodeReady Containers allow you to do quite a bit with a local installation of RedHat OpenShift on your own hardware. For more, read my earlier article on Red Hat Developer, Configure CodeReady Containers for AI/ML development.

    Last updated: November 8, 2023

    Recent Posts

    • Cloud bursting with confidential containers on OpenShift

    • Reach native speed with MacOS llama.cpp container inference

    • A deep dive into Apache Kafka's KRaft protocol

    • Staying ahead of artificial intelligence threats

    • Strengthen privacy and security with encrypted DNS in RHEL

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue