# 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](https://docs.rafay.co/learn/quickstart/kubernetes/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.

[Kubernetes 201-PersistentVolumes](https://www.youtube.com/watch?v=MiuKjDE-kAo)

---

## 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](https://github.com/RafaySystems/getstarted).

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  
01. In the Terminal or Command Prompt, create the **test** namespace.
   ```
   kubectl create namespace test
   ```
02. Change to the **test** namespace.
   ```
   kubectl config set-context --current --namespace=test
   ```
03. Add the **1-pv** PersistentVolume to your environment using a YAML file.
   ```
   kubectl create -f 1-pv.yaml
   ```
04. Add **2-pv** to your environment.
   ```
   kubectl create -f 2-pv.yaml
   ```
05. List the PersistentVolumes. `pv` is the same as `persistentvolume`.
   ```
   kubectl get pv
   ```
06. Add **1-pvc** to your environment.
   ```
   kubectl create -f 1-pvc.yaml
   ```
07. Add **2-pvc** to your environment.
   ```
   kubectl create -f 2-pvc.yaml
   ```
08. 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
   ```
09. 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](https://docs.rafay.co/learn/quickstart/kubernetes/persistentvolumes/#nestedblock--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
   ```

---

## 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
```
