Aller au contenu

Lab 03: Volume Management in OKS

  • Create and use PersistentVolumes (PV) and PersistentVolumeClaims (PVC) with a StorageClass.
  • Test volume behavior with simple applications.
  1. A running Kubernetes cluster provided by OKS.
  2. Access to the kubectl CLI.
  3. Administrator privileges on the cluster.
  4. Basic knowledge of Kubernetes objects: Pod, PVC, etc.

Ensure you have the necessary permissions to access the target cluster. You must have a user or role that allows you to export or retrieve the cluster configuration.

Terminal window
oks-cli cluster kubeconfig --cluster-name my-cluster --project-name my-project > kubeconfig.yaml
export KUBECONFIG=./kubeconfig.yaml
Terminal window
kubectl get nodes

OKS provides a default StorageClass for dynamic volumes. If you forget to create a custom StorageClass, Kubernetes will automatically use the default one. To check the available StorageClasses, run:

Terminal window
kubectl get storageclass

You should get a list of StorageClass like this:

Terminal window
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
bsu-gp2 (default) bsu.csi.outscale.com Retain WaitForFirstConsumer true 30d
bsu-gp2-del bsu.csi.outscale.com Delete WaitForFirstConsumer true 30d
bsu-io1-50 bsu.csi.outscale.com Retain WaitForFirstConsumer true 30d
bsu-io1-50-del bsu.csi.outscale.com Delete WaitForFirstConsumer true 30d
bsu-standard bsu.csi.outscale.com Retain WaitForFirstConsumer true 30d
bsu-standard-del bsu.csi.outscale.com Delete WaitForFirstConsumer true 30d

You can see that in the list of available StorageClass in our OKS cluster, the bsu-gp2 storage class is marked as the default (default). This means that when creating a PVC, if no storage class is specified, this one will be used automatically.

If you want to define another default storageClass, you can do it with:

Terminal window
kubectl patch storageclass bsu-gp2-del -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'

Create a dedicated namespace:

Terminal window
kubectl create namespace lab-volumes

We will create a PersistentVolumeClaim (PVC) requesting 5 Gi of storage.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: bsu-claim
namespace: lab-volumes
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: bsu-standard

Apply the YAML file to create your PVC:

Terminal window
kubectl apply -f pvc.yaml

Once applied, you can verify that it has been created:

Terminal window
kubectl get pvc -n lab-volumes
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE
bsu-claim Pending bsu-standard <unset> 50s

You will notice that the STATUS is Pending, meaning it is not yet bound to a PersistentVolume. This is expected: the VOLUMEBINDINGMODE is WaitForFirstConsumer, so until a Pod uses this PVC, the PV will not be provisioned.

Create a simple Pod that mounts the PVC and writes to a file:

apiVersion: v1
kind: Pod
metadata:
name: app
namespace: lab-volumes
spec:
containers:
- name: app
image: centos:centos7
command: ["/bin/sh"]
args: ["-c", "while true; do echo $(date -u) >> /data/out.txt; sleep 5; done"]
volumeMounts:
- name: persistent-storage
mountPath: /data
volumes:
- name: persistent-storage
persistentVolumeClaim:
claimName: bsu-claim
Terminal window
kubectl apply -f pod-rwo.yaml

Now the PVC should switch to Bound:

Terminal window
kubectl get pvc -n lab-volumes
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
bsu-claim Bound pvc-xxxx-xxxx-xxxx-xxxx 5Gi RWO bsu-standard 5s

This means the PVC is now attached to a real volume.

Check that the Pod is running:

Terminal window
kubectl get pods -n lab-volumes

Access it:

Terminal window
kubectl exec -it app -n lab-volumes -- /bin/sh

Check the file:

Terminal window
cat /data/out.txt

You should see timestamps every 5 seconds.

Follow live:

Terminal window
tail -f /data/out.txt
Terminal window
kubectl apply -f https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/release-8.0/client/config/crd/snapshot.storage.k8s.io_volumesnapshotclasses.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/release-8.0/client/config/crd/snapshot.storage.k8s.io_volumesnapshotcontents.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/release-8.0/client/config/crd/snapshot.storage.k8s.io_volumesnapshots.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/release-8.0/deploy/kubernetes/snapshot-controller/rbac-snapshot-controller.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/release-8.0/deploy/kubernetes/snapshot-controller/setup-snapshot-controller.yaml
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
name: bsu-volume-snapshot
namespace: lab-volumes
spec:
volumeSnapshotClassName: csi-osc-vsc
source:
persistentVolumeClaimName: bsu-claim
Terminal window
kubectl describe volumesnapshot.snapshot.storage.k8s.io bsu-volume-snapshot
Terminal window
kubectl delete pod app -n lab-volumes
kubectl delete pvc bsu-claim -n lab-volumes
apiVersion: v1
kind: Pod
metadata:
name: app
namespace: lab-volumes
spec:
containers:
- name: app
image: centos:centos7
command: ["/bin/sh"]
args: ["-c", "while true; do echo $(date -u) >> /data/out.txt; sleep 5; done"]
volumeMounts:
- name: persistent-storage
mountPath: /data
volumes:
- name: persistent-storage
persistentVolumeClaim:
claimName: bsu-snapshot-restored-claim
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: bsu-snapshot-restored-claim
namespace: lab-volumes
spec:
accessModes:
- ReadWriteOnce
storageClassName: bsu-sc
resources:
requests:
storage: 5Gi
dataSource:
name: bsu-volume-snapshot
kind: VolumeSnapshot
apiGroup: snapshot.storage.k8s.io
Terminal window
kubectl exec -it app -n lab-volumes -- /bin/sh
cat /data/out.txt

Delete all created resources.