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 pv
No resources found
root@master:/kube# kubectl get pvc
No resources found in default namespace.
root@master:/kube#

root@master:/kube# cat nginx.yml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pv-pod
spec:
  containers:
    - name: nginx-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: nginx-storage
  volumes:
    - name: nginx-storage
      persistentVolumeClaim:
        claimName: nginx-pv-claim
root@master:/kube#

root@master:/kube# kubectl get pods
NAME           READY   STATUS    RESTARTS   AGE
nginx-pv-pod   0/1     Pending   0          41s
root@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: v1
kind: PersistentVolumeClaim
metadata:
  name: persistent-volume-claim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  selector:
    matchLabels:
      pv: local
  storageClassName: hostpath

persistentvolumeclaim/persistent-volume-claim created
root@master:/kube# kubectl get pvc
NAME                      STATUS    VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
persistent-volume-claim   Pending                                      hostpath       3s
root@master:/kube# 

Ended up with the error:

Warning  ProvisioningFailed  12s (x2 over 14s)  persistentvolume-controller  storageclass.storage.k8s.io "hostpath" not found

Now, 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 set

So, PVC needs PV for binding the volume.

Let me create Persistent Volume:

root@master:/kube# cat pv.yml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: persistent-volume
spec:
  capacity:
   storage: 1Gi
  accessModes:
   - ReadWriteOnce
  hostPath:
    path: "/data"
  storageClassName: slow

root@master:/kube# mkdir -p "/mnt/data"
root@master:/kube# ls -l "/mnt/data"
total 0
root@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 Node

It 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.yml
persistentvolume/task-pv-volume created
root@master:/kube# kubectl get pv
NAME             CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
task-pv-volume   1Gi       RWO            Retain           Available           manual                  3s
root@master:/kube#

root@master:/kube# kubectl describe pv task-pv-volume
Name:            task-pv-volume
Labels:          type=local
Annotations:     <none>
Finalizers:      [kubernetes.io/pv-protection]
StorageClass:    manual
Status:          Available
Claim:
Reclaim Policy:  Retain
Access Modes:    RWO
VolumeMode:      Filesystem
Capacity:        1Gi
Node Affinity:   <none>
Message:
Source:
    Type:          HostPath (bare host directory volume)
    Path:          /mnt/data
    HostPathType:
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.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 300Mi
root@master:/kube#

We are requesting for PVC to be created from a PV with storageclassname called "manual".

root@master:/kube# kubectl apply -f pvc.yml
persistentvolumeclaim/task-pv-claim created
root@master:/kube# kubectl get pvc
NAME            STATUS   VOLUME           CAPACITY   ACCESS MODES   STORAGECLASS   AGE
task-pv-claim   Bound    task-pv-volume   1Gi        RWO            manual         2s
root@master:/kube# kubectl describe pvc task-pv-claim
Name:          task-pv-claim
Namespace:     default
StorageClass:  manual
Status:        Bound
Volume:        task-pv-volume
Labels:        <none>
Annotations:   pv.kubernetes.io/bind-completed: yes
               pv.kubernetes.io/bound-by-controller: yes
Finalizers:    [kubernetes.io/pvc-protection]
Capacity:      1Gi
Access Modes:  RWO
VolumeMode:    Filesystem
Used By:       <none>
Events:        <none>

root@master:/kube# kubectl get pv
NAME              CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM                   STORAGECLASS   REASON   AGE
task-pv-volume    1Gi        RWO            Retain           Bound       default/task-pv-claim   manual                  3m23s
root@master:/kube#

Now the output shows a STATUS of Bound. 

Lets create a pod.

apiVersion: v1
kind: Pod
metadata:
  name: task-pv-pod
spec:
  volumes:
    - name: task-pv-storage
      persistentVolumeClaim:
        claimName: task-pv-claim
  containers:
    - name: task-pv-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: task-pv-storage

root@master:/kube# kubectl get pods
NAME          READY   STATUS    RESTARTS   AGE
task-pv-pod   1/1     Running   0          37s
root@master:/kube#

root@master:/kube# kubectl get pods -o wide
NAME          READY   STATUS    RESTARTS   AGE   IP            NODE     NOMINATED NODE   READINESS GATES
task-pv-pod   1/1     Running   0          53s   10.244.0.12   master   <none>           <none>
root@master:/kube#











Comments

Popular posts from this blog

SRE/DevOps Syllabus

AWS Code Commit - CI/CD Series Part 1

Docker - Preventing IP overlapping