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.
- Open the Terminal.
- Navigate to the Downloads folder.
cd ./Downloads
- 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
- Use the nano text editor in the Terminal.
nano 1-pv.yaml
- Copy and paste the configuration below into the text editor.
- Press Cmd + X, then type Y and press Return to save the 1-pv.yaml file.
- Repeat steps 4 through 6 to create the 1-pvc.yaml, 2-pv.yaml, and 2-pvc.yaml files.
- Open the command prompt.
- Navigate to the Downloads folder.
cd ./Downloads
- Use the following command to create an empty YAML file in your Downloads folder.
copy NUL 1-pv.yaml
- Open the 1-pv.yaml file with a text editor. For example, use Notepad++ to edit the YAML file.
- Copy and paste the configuration below into the text editor.
- Save the 1-pv.yaml file.
- Repeat steps 3 through 6 to create the 1-pvc.yaml, 2-pv.yaml, and 2-pvc.yaml files.
- Open the Terminal.
- Navigate to the Downloads folder.
cd ./Downloads
- 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
- Use the nano text editor in the Terminal.
nano 1-pv.yaml
- Copy and paste the configuration below into the text editor.
- Press Cmd + X, then type Y and press Return to save the 1-pv.yaml file.
- Repeat steps 4 through 6 to create the 1-pvc.yaml, 2-pv.yaml, and 2-pvc.yaml files.
Add a PV¶
- In the Terminal or Command Prompt, create the test namespace.
kubectl create namespace test
- Change to the test namespace.
kubectl config set-context --current --namespace=test
- Add the 1-pv PersistentVolume to your environment using a YAML file.
kubectl create -f 1-pv.yaml
- Add 2-pv to your environment.
kubectl create -f 2-pv.yaml
-
List the PersistentVolumes.
pv
is the same aspersistentvolume
.kubectl get pv
-
Add 1-pvc to your environment.
kubectl create -f 1-pvc.yaml
- Add 2-pvc to your environment.
kubectl create -f 2-pvc.yaml
-
List the PersistentVolumeClaims.
pvc
is the same aspersistentvolumeclaim
. The 2-pvc should be in the Pending state. The 1-pv PV only allows the 1-pvc PVC.kubectl get pvc
-
Delete the 2-pvc PVC.
kubectl delete pvc 2-pvc
- Edit the 2-pvc.yaml file. Change
volumeName
from1-pv
to2-pv
. Refer to the YAML files steps for editing a YAML file. - Add the updated 2-pvc PVC.
kubectl create -f 2-pvc.yaml
-
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