Learn Kubernetes 201 - Part 3 - Using PV - Rafay Product Documentation

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:

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

Kubernetes 201-PersistentVolumes


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.

Add a PV

  1. In the Terminal or Command Prompt, create the test namespace.
kubectl create namespace test
  1. Change to the test namespace.
kubectl config set-context --current --namespace=test
  1. Add the 1-pv PersistentVolume to your environment using a YAML file.
kubectl create -f 1-pv.yaml
  1. Add 2-pv to your environment.
kubectl create -f 2-pv.yaml
  1. List the PersistentVolumes. pv is the same as persistentvolume.
kubectl get pv
  1. Add 1-pvc to your environment.
kubectl create -f 1-pvc.yaml
  1. Add 2-pvc to your environment.
kubectl create -f 2-pvc.yaml
  1. 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
  1. Delete the 2-pvc PVC.
kubectl delete pvc 2-pvc
  1. 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.
  2. Add the updated 2-pvc PVC.
kubectl create -f 2-pvc.yaml
  1. List the PersistentVolumeClaims. The 2-pvc is bound to the 2-pv.
kubectl get pvc

PersistentVolume YAML files

These YAML files create PersistentVolumes (PV).

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.

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