Kubernetes - Volumes
Persistent volume (PV) is a piece of storage provided by an administrator in a Kubernetes cluster.
When a developer needs persistent storage for an application in the cluster, they request that storage by creating a persistent volume claim (PVC) and then mounting the volume to a path in the pod
Once that is done, the pod claims any volume that matches its requirements (such as size, access mode, and so on).
An administrator can create multiple PVs with different capacities and configurations.
It is up to the developer to provide a PVC for storage, and then Kubernetes matches a suitable PV with the PVC.
If there is no PV to match the PVC, the StorageClass dynamically creates a PV and binds it to the PVC.
It is important to note that Kubernetes does not restrict PVs to a namespace, which means that a pod in any namespace can claim a PV for storage.
I am trying to create a pod with volume. But not creating a volume at first sight.
root@master:/kube# kubectl get pvNo resources foundroot@master:/kube# kubectl get pvcNo resources found in default namespace.root@master:/kube#root@master:/kube# cat nginx.ymlapiVersion: v1kind: Podmetadata:name: nginx-pv-podspec:containers:- name: nginx-containerimage: nginxports:- containerPort: 80name: "http-server"volumeMounts:- mountPath: "/usr/share/nginx/html"name: nginx-storagevolumes:- name: nginx-storagepersistentVolumeClaim:claimName: nginx-pv-claimroot@master:/kube#root@master:/kube# kubectl get podsNAME READY STATUS RESTARTS AGEnginx-pv-pod 0/1 Pending 0 41sroot@master:/kube#0/1 nodes are available: 1 persistentvolumeclaim "nginx-pv-claim" not found.I am creating a pod with PVC. But, no PVC or PV Created. Hence we got the above error.Creating a PVC:apiVersion: v1kind: PersistentVolumeClaimmetadata:name: persistent-volume-claimspec:accessModes:- ReadWriteOnceresources:requests:storage: 1Giselector:matchLabels:pv: localstorageClassName: hostpathpersistentvolumeclaim/persistent-volume-claim createdroot@master:/kube# kubectl get pvcNAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGEpersistent-volume-claim Pending hostpath 3sroot@master:/kube#Ended up with the error:Warning ProvisioningFailed 12s (x2 over 14s) persistentvolume-controller storageclass.storage.k8s.io "hostpath" not foundNow, creating a PVC without storageclass. Ended up with the error:Normal FailedBinding 6s (x2 over 13s) persistentvolume-controller no persistent volumes available for this claim and no storage class is setSo, PVC needs PV for binding the volume.Let me create Persistent Volume:root@master:/kube# cat pv.ymlapiVersion: v1kind: PersistentVolumemetadata:name: persistent-volumespec:capacity:storage: 1GiaccessModes:- ReadWriteOncehostPath:path: "/data"storageClassName: slowroot@master:/kube# mkdir -p "/mnt/data"root@master:/kube# ls -l "/mnt/data"total 0root@master:/kube#The configuration file specifies that the volume is at /mnt/data on the cluster's Node.The configuration also specifies a size of 1 gibibytes and an access mode of ReadWriteOnce, which means the volume can be mounted as read-write by a single NodeIt defines the StorageClass name manual for the PersistentVolume, which will be used to bind PersistentVolumeClaim requests to this PersistentVolume.root@master:/kube# kubectl apply -f pv.ymlpersistentvolume/task-pv-volume createdroot@master:/kube# kubectl get pvNAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGEtask-pv-volume 1Gi RWO Retain Available manual 3sroot@master:/kube#root@master:/kube# kubectl describe pv task-pv-volumeName: task-pv-volumeLabels: type=localAnnotations: <none>Finalizers: [kubernetes.io/pv-protection]StorageClass: manualStatus: AvailableClaim:Reclaim Policy: RetainAccess Modes: RWOVolumeMode: FilesystemCapacity: 1GiNode Affinity: <none>Message:Source:Type: HostPath (bare host directory volume)Path: /mnt/dataHostPathType:Events: <none>root@master:/kube#The output shows that the PersistentVolume has a STATUS of Available. This means it has not yet been bound to a PersistentVolumeClaim.Pods use PersistentVolumeClaims to request physical storage.Now, creating a persistent volume claim.root@master:/kube# cat pvc.ymlapiVersion: v1kind: PersistentVolumeClaimmetadata:name: task-pv-claimspec:storageClassName: manualaccessModes:- ReadWriteOnceresources:requests:storage: 300Miroot@master:/kube#We are requesting for PVC to be created from a PV with storageclassname called "manual".root@master:/kube# kubectl apply -f pvc.ymlpersistentvolumeclaim/task-pv-claim createdroot@master:/kube# kubectl get pvcNAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGEtask-pv-claim Bound task-pv-volume 1Gi RWO manual 2sroot@master:/kube# kubectl describe pvc task-pv-claimName: task-pv-claimNamespace: defaultStorageClass: manualStatus: BoundVolume: task-pv-volumeLabels: <none>Annotations: pv.kubernetes.io/bind-completed: yespv.kubernetes.io/bound-by-controller: yesFinalizers: [kubernetes.io/pvc-protection]Capacity: 1GiAccess Modes: RWOVolumeMode: FilesystemUsed By: <none>Events: <none>root@master:/kube# kubectl get pvNAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGEtask-pv-volume 1Gi RWO Retain Bound default/task-pv-claim manual 3m23sroot@master:/kube#Now the output shows a STATUS of Bound.Lets create a pod.apiVersion: v1kind: Podmetadata:name: task-pv-podspec:volumes:- name: task-pv-storagepersistentVolumeClaim:claimName: task-pv-claimcontainers:- name: task-pv-containerimage: nginxports:- containerPort: 80name: "http-server"volumeMounts:- mountPath: "/usr/share/nginx/html"name: task-pv-storageroot@master:/kube# kubectl get podsNAME READY STATUS RESTARTS AGEtask-pv-pod 1/1 Running 0 37sroot@master:/kube#root@master:/kube# kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATEStask-pv-pod 1/1 Running 0 53s 10.244.0.12 master <none> <none>root@master:/kube#
Comments
Post a Comment