Skip to content

Part 3: Using PV

This is Part 3 of a multi-part, self-paced quick start exercise.

Note

This exercise requires MicroK8s and uses alias kubectl='microk8s kubectl'. If you do not have these already installed and running, see "Prerequisites".


What Will You Do

In part 3, you will:

  • Deploy two PersistentVolumeClaims (PVC) to the same PersistentVolume (PV).
  • See that one PVC is allowed and the other PVC is not allowed on the same PV. This is because only one PVC is allowed for a PV.
  • Update a PVC to connect to a different PV.

Estimated Time

Estimated time for this exercise is 15 minutes. Watch a video of this exercise.


Using PersistentVolumes

A PersistentVolume (PV) is a piece of storage in the cluster that has been manually provisioned by an administrator, or dynamically provisioned by Kubernetes using a StorageClass. A PersistentVolumeClaim (PVC) is a request for storage by a user that can be fulfilled by a PV. PersistentVolumes and PersistentVolumeClaims are independent from Pod life cycles and preserve data through restarting, rescheduling, and even deleting Pods.

YAML files

Create two PV YAML files to configure storage and two PVC YAML files to test connecting PVCs to PVs. You could create the YAML file from the command line, use a text editor, or download the PV YAML file from the Git repository.

  1. Open the Terminal.
  2. Navigate to the Downloads folder.
    cd ./Downloads
    
  3. Use the following command to create an empty YAML file in your Downloads folder.
    touch 1-pv.yaml 2-pv.yaml 1-pvc.yaml 2-pvc.yaml 
    
  4. Use the nano text editor in the Terminal.
    nano 1-pv.yaml
    
  5. Copy and paste the configuration below into the text editor.
  6. Press Cmd + X, then type Y and press Return to save the 1-pv.yaml file.
  7. Repeat steps 4 through 6 to create the 1-pvc.yaml, 2-pv.yaml, and 2-pvc.yaml files.
  1. Open the command prompt.
  2. Navigate to the Downloads folder.
    cd ./Downloads
    
  3. Use the following command to create an empty YAML file in your Downloads folder.
    copy NUL 1-pv.yaml
    
  4. Open the 1-pv.yaml file with a text editor. For example, use Notepad++ to edit the YAML file.
  5. Copy and paste the configuration below into the text editor.
  6. Save the 1-pv.yaml file.
  7. Repeat steps 3 through 6 to create the 1-pvc.yaml, 2-pv.yaml, and 2-pvc.yaml files.
  1. Open the Terminal.
  2. Navigate to the Downloads folder.
    cd ./Downloads
    
  3. Use the following command to create an empty YAML file in your Downloads folder.
    touch 1-pv.yaml 2-pv.yaml 1-pvc.yaml 2-pvc.yaml 
    
  4. Use the nano text editor in the Terminal.
    nano 1-pv.yaml
    
  5. Copy and paste the configuration below into the text editor.
  6. Press Cmd + X, then type Y and press Return to save the 1-pv.yaml file.
  7. Repeat steps 4 through 6 to create the 1-pvc.yaml, 2-pv.yaml, and 2-pvc.yaml files.

Add a PV

  1. In the Terminal or Command Prompt, create the test namespace.
    kubectl create namespace test
    
  2. Change to the test namespace.
    kubectl config set-context --current --namespace=test
    
  3. Add the 1-pv PersistentVolume to your environment using a YAML file.
    kubectl create -f 1-pv.yaml
    
  4. Add 2-pv to your environment.
    kubectl create -f 2-pv.yaml
    
  5. List the PersistentVolumes. pv is the same as persistentvolume.

    kubectl get pv
    

    List PV

  6. Add 1-pvc to your environment.

    kubectl create -f 1-pvc.yaml
    

  7. Add 2-pvc to your environment.
    kubectl create -f 2-pvc.yaml
    
  8. List the PersistentVolumeClaims. pvc is the same as persistentvolumeclaim. The 2-pvc should be in the Pending state. The 1-pv PV only allows the 1-pvc PVC.

    kubectl get pvc
    

    List PVC

  9. Delete the 2-pvc PVC.

    kubectl delete pvc 2-pvc
    

  10. Edit the 2-pvc.yaml file. Change volumeName from 1-pv to 2-pv. Refer to the YAML files steps for editing a YAML file.
  11. Add the updated 2-pvc PVC.
    kubectl create -f 2-pvc.yaml
    
  12. List the PersistentVolumeClaims. The 2-pvc is bound to the 2-pv.

    kubectl get pvc
    

    List PVC


PersistentVolume YAML files

These YAML files create PersistentVolumes (PV).

  • The storage capacity is 5Gi (5 Gibibytes), which is close to 5GB.
  • The volumeMode is set to Filesystem which mounts the volume into Pods in the directory. If the volume is backed by a block device and the device is empty, Kubernetes creates a filesystem on the device before mounting it for the first time.
  • The accessMode of ReadWriteOnce allows read-write by a single node.
  • The storageClassName is set to slow which provisions standard disk-like persistent disks.
  • The claimRef identifies the PVC name and the Namespace name allowed to use this PersistentVolume.
  • The hostPath provides the path to the storage.

1-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: 1-pv
spec:
  capacity:
    storage: 5Gi
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  storageClassName: slow
  claimRef:
    name: 1-pvc
    namespace: test
  hostPath:
    path: /var/lib/pv

2-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: 2-pv
spec:
  capacity:
    storage: 5Gi
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  storageClassName: slow
  claimRef:
    name: 2-pvc
    namespace: test
  hostPath:
    path: /var/lib/pv

PersistentVolumeClaim YAML files

These YAML files create PersistentVolumeClaims (PVC). This exercise demonstrates what happens when you create a PVC that is not allowed by the PV.

  • The name under metadata is the PVC name. This is important if the PV uses a claimRef to restrict access to the persistent volumes.
  • The volumeName is the PV name.

1-pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: 1-pvc
  namespace: test
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: slow
  volumeName: 1-pv

2-pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: 2-pvc
  namespace: test
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: slow
  volumeName: 1-pv