Kubernetes Volumes - PV/PVC

 

A PersistentVolume (PV) in Kubernetes is a pool of pre-provisioned storage resources in a Kubernetes cluster, that can be used across different user environments. 

Its lifecycle is separate from a Pod that uses the PersistentVolume.

A PersistentVolumeClaim (PVC), is a process of storage requests from PVs by the users in Kubernetes.

Kubernetes binds PVs with the PVCs based on the request and property set on those PVs.

Kubernetes searches for PVs that correspond to the PVCs’ requested capacity and specified properties, so that each PVC can bind to a single PV. 

NOTE: Both the PVCs and the Pod using them must be in the same namespace.

PVs are created by the cluster administrator or dynamically by Kubernetes, whereas users/developers create PVCs.

PVs are cluster resources provisioned by an administrator, whereas PVCs are a user’s request for storage and resources. 

PVCs consume PVs resources, but not vice versa.

A PV is similar to a node in terms of cluster resources, while a PVC is like a Pod in the context of cluster resource consumption.

Creating a LVM based filesystem on worker node and configure PV on it.

root@worker01:~# pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created.
root@worker01:~#

root@worker01:~# vgcreate data /dev/sdb
  Volume group "data" successfully created
root@worker01:~# 

root@worker01:~# lvcreate -n data_lv -l 100%FREE data
  Logical volume "data_lv" created.
root@worker01:~#

root@worker01:~# mkfs -t ext4 /dev/data/data_lv

root@worker01:~# mount /dev/data/data_lv /data

root@worker01:~# df -TH /data
Filesystem               Type  Size  Used Avail Use% Mounted on
/dev/mapper/data-data_lv ext4  5.2G   25k  5.0G   1% /data
root@worker01:~#

We are done with filesystem creation.

Lets proceed with PV - Persistence Volume creation.

root@masterk8s:/docker# cat pv.yml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-volume-1
  labels:
    type: local
spec:
  capacity:
    storage: 500Mi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/data"
root@masterk8s:/docker#

root@masterk8s:/docker# kubectl apply -f pv.yml
persistentvolume/pv-volume-1 created
root@masterk8s:/docker#

root@masterk8s:/docker# kubectl get pv
NAME          CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
pv-volume-1   500Mi      RWO            Retain           Available                                   8s
root@masterk8s:/docker#

Status of the PV is AVAILABLE.

Lets create PVC - Persistence Volume Claim.

root@masterk8s:/docker# cat pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-claim-1
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi
root@masterk8s:/docker#

Once the PVC is created the status of the PV changes from "Available" to "Bound".

root@masterk8s:/docker# kubectl get pv
NAME          CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                 STORAGECLASS   REASON   AGE
pv-volume-1   500Mi      RWO            Retain           Bound    default/pvc-claim-1                           80s
root@masterk8s:/docker# kubectl get pvc
NAME          STATUS   VOLUME        CAPACITY   ACCESS MODES   STORAGECLASS   AGE
pvc-claim-1   Bound    pv-volume-1   500Mi      RWO                           12s
root@masterk8s:/docker#

Lets create a pod to use the PVC:

root@masterk8s:/docker# cat nginx.yml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    name: www
spec:
  containers:
  - name: www
    image: nginx:alpine
    ports:
      - containerPort: 80
        name: www
    volumeMounts:
      - name: nginx-storage
        mountPath: /usr/share/nginx/html
  volumes:
    - name: nginx-storage
      persistentVolumeClaim:
        claimName: pvc-claim-1
root@masterk8s:/docker#

root@masterk8s:/docker# kubectl apply -f nginx.yml
pod/nginx created
root@masterk8s:/docker#






Comments

Popular posts from this blog

SRE/DevOps Syllabus

AWS Code Commit - CI/CD Series Part 1

Docker - Preventing IP overlapping