Lab 03: Volume Management in OKS
- Create and use PersistentVolumes (PV) and PersistentVolumeClaims (PVC) with a StorageClass.
- Test volume behavior with simple applications.
Prerequisites
Section intitulée « Prerequisites »- A running Kubernetes cluster provided by OKS.
- Access to the
kubectlCLI. - Administrator privileges on the cluster.
- Basic knowledge of Kubernetes objects: Pod, PVC, etc.
Verify Cluster Access
Section intitulée « Verify Cluster Access »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.
Retrieve the kubeconfig file
Section intitulée « Retrieve the kubeconfig file »oks-cli cluster kubeconfig --cluster-name my-cluster --project-name my-project > kubeconfig.yamlexport KUBECONFIG=./kubeconfig.yamlTest the cluster
Section intitulée « Test the cluster »kubectl get nodesStep 1: Verifying the Default StorageClass
Section intitulée « Step 1: Verifying the Default StorageClass »Initial verification
Section intitulée « Initial verification »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:
kubectl get storageclassYou should get a list of StorageClass like this:
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGEbsu-gp2 (default) bsu.csi.outscale.com Retain WaitForFirstConsumer true 30dbsu-gp2-del bsu.csi.outscale.com Delete WaitForFirstConsumer true 30dbsu-io1-50 bsu.csi.outscale.com Retain WaitForFirstConsumer true 30dbsu-io1-50-del bsu.csi.outscale.com Delete WaitForFirstConsumer true 30dbsu-standard bsu.csi.outscale.com Retain WaitForFirstConsumer true 30dbsu-standard-del bsu.csi.outscale.com Delete WaitForFirstConsumer true 30dYou 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:
kubectl patch storageclass bsu-gp2-del -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'Step 2: Creating a ReadWriteOnce (RWO) PVC
Section intitulée « Step 2: Creating a ReadWriteOnce (RWO) PVC »Deploy a test environment
Section intitulée « Deploy a test environment »Create a dedicated namespace:
kubectl create namespace lab-volumesDefine the PVC
Section intitulée « Define the PVC »We will create a PersistentVolumeClaim (PVC) requesting 5 Gi of storage.
apiVersion: v1kind: PersistentVolumeClaimmetadata: name: bsu-claim namespace: lab-volumesspec: accessModes: - ReadWriteOnce resources: requests: storage: 5Gi storageClassName: bsu-standardApply the PVC
Section intitulée « Apply the PVC »Apply the YAML file to create your PVC:
kubectl apply -f pvc.yamlOnce applied, you can verify that it has been created:
kubectl get pvc -n lab-volumesNAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGEbsu-claim Pending bsu-standard <unset> 50sYou 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.
Step 3: Deploying a Pod Using the PVC
Section intitulée « Step 3: Deploying a Pod Using the PVC »Define the Pod
Section intitulée « Define the Pod »Create a simple Pod that mounts the PVC and writes to a file:
apiVersion: v1kind: Podmetadata: name: app namespace: lab-volumesspec: 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-claimApply the Pod
Section intitulée « Apply the Pod »kubectl apply -f pod-rwo.yamlNow the PVC should switch to Bound:
kubectl get pvc -n lab-volumesNAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGEbsu-claim Bound pvc-xxxx-xxxx-xxxx-xxxx 5Gi RWO bsu-standard 5sThis means the PVC is now attached to a real volume.
Test the Pod
Section intitulée « Test the Pod »Check that the Pod is running:
kubectl get pods -n lab-volumesAccess it:
kubectl exec -it app -n lab-volumes -- /bin/shCheck the file:
cat /data/out.txtYou should see timestamps every 5 seconds.
Follow live:
tail -f /data/out.txtApply external snapshotter CRDs
Section intitulée « Apply external snapshotter CRDs »kubectl apply -f https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/release-8.0/client/config/crd/snapshot.storage.k8s.io_volumesnapshotclasses.yamlkubectl apply -f https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/release-8.0/client/config/crd/snapshot.storage.k8s.io_volumesnapshotcontents.yamlkubectl apply -f https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/release-8.0/client/config/crd/snapshot.storage.k8s.io_volumesnapshots.yamlkubectl apply -f https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/release-8.0/deploy/kubernetes/snapshot-controller/rbac-snapshot-controller.yamlkubectl apply -f https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/release-8.0/deploy/kubernetes/snapshot-controller/setup-snapshot-controller.yamlCreate a VolumeSnapshot
Section intitulée « Create a VolumeSnapshot »apiVersion: snapshot.storage.k8s.io/v1kind: VolumeSnapshotmetadata: name: bsu-volume-snapshot namespace: lab-volumesspec: volumeSnapshotClassName: csi-osc-vsc source: persistentVolumeClaimName: bsu-claimWait until ready
Section intitulée « Wait until ready »kubectl describe volumesnapshot.snapshot.storage.k8s.io bsu-volume-snapshotDelete the application
Section intitulée « Delete the application »kubectl delete pod app -n lab-volumeskubectl delete pvc bsu-claim -n lab-volumesRestore from snapshot
Section intitulée « Restore from snapshot »apiVersion: v1kind: Podmetadata: name: app namespace: lab-volumesspec: 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: v1kind: PersistentVolumeClaimmetadata: name: bsu-snapshot-restored-claim namespace: lab-volumesspec: accessModes: - ReadWriteOnce storageClassName: bsu-sc resources: requests: storage: 5Gi dataSource: name: bsu-volume-snapshot kind: VolumeSnapshot apiGroup: snapshot.storage.k8s.ioValidate data restoration
Section intitulée « Validate data restoration »kubectl exec -it app -n lab-volumes -- /bin/shcat /data/out.txtDelete all created resources.