kubernetes dynamic NFS provisioning

In Kubernetes, a volume can be thought of as a directory that is accessible to the containers in a pod. We have different types of volumes in Kubernetes and the type defines how the volume is created and it's content.

The concept of volume was present with the Docker, however, the only issue was that the volume was very much limited to a particular pod. As soon as the life of a pod ended, the volume was also lost.

On the other hand, the volumes that are created through Kubernetes are not limited to any container. It supports any or all the containers deployed inside the pod of Kubernetes. A key advantage of Kubernetes volume is, it supports different kinds of storage wherein the pod can use multiple of them at the same time.

Persistent Volume and Persistent Volume Claim
Persistent Volume (PV) − It’s a piece of network storage that has been provisioned by the administrator. It’s a resource in the cluster which is independent of any individual pod that uses the PV.

Persistent Volume Claim (PVC) − The storage requested by Kubernetes for its pods is known as PVC. The user does not need to know the underlying provisioning. The claims must be created in the same namespace where the pod is created.

# cat Persistent-Volume.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv0001
spec:
capacity:
storage: 10Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
mountOptions:
- hard
- nfsvers=4.1
nfs:
path: /tmp
server: 172.17.0.2

In the above code, we have defined −
kind: PersistentVolume → We have defined the kind as PersistentVolume which tells Kubernetes that the yaml file being used is to create the Persistent Volume.
name pv0001 → Name of PersistentVolume that we are creating.
capacity: → This spec will define the capacity of PV that we are trying to create.
storage: 10Gi → This tells the underlying infrastructure that we are trying to claim 10Gi space on the defined path.
ReadWriteOnce → This tells the access rights of the volume that we are creating.
path: "/tmp/data01," → This definition tells the machine that we are trying to create volume under this path on the underlying infrastructure.

Creating PersistentVolume
# kubectl create -f Persistent-Volume.yaml
persistentvolume "pv0001" created

Checking PersistentVolume
# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv0001 10Gi RWO Retain Bound default/myclaim-1 5m40s

Describing PersistentVolume
# kubectl describe pv pv0001
Name: pv0001
Labels:
Annotations: pv.kubernetes.io/bound-by-controller: yes
Finalizers: [kubernetes.io/pv-protection]
StorageClass:
Status: Bound
Claim: default/myclaim-1
Reclaim Policy: Retain
Access Modes: RWO
VolumeMode: Filesystem
Capacity: 10Gi
Node Affinity:
Message:
Source:
Type: NFS (an NFS mount that lasts the lifetime of a pod)
Server: 172.17.0.2
Path: /tmp
ReadOnly: false
Events:

creating Persistent Volume Claim
# catPersistentVolumeClaim.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: myclaim-1
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 3Gi

In the above code, we have defined −
kind: PersistentVolumeClaim → It instructs the underlying infrastructure that we are trying to claim a specified amount of space.
name myclaim-1 → Name of the claim that we are trying to create.
ReadWriteOnce → This specifies the mode of the claim that we are trying to create.
storage: 3Gi → This will tell Kubernetes about the amount of space we are trying to claim.
# kubectl create -f PersistentVolumeClaim.yaml
persistentvolumeclaim "myclaim-1" created

Getting Details About PVC
# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
myclaim-1 Bound pv0001 10Gi RWO 4h2m

Describe PVC
# kubectl describe pv pv0001
Name: pv0001
Labels:
Annotations: pv.kubernetes.io/bound-by-controller: yes
Finalizers: [kubernetes.io/pv-protection]
StorageClass:
Status: Bound
Claim: default/myclaim-1
Reclaim Policy: Retain
Access Modes: RWO
VolumeMode: Filesystem
Capacity: 10Gi
Node Affinity:
Message:
Source:
Type: NFS (an NFS mount that lasts the lifetime of a pod)
Server: 172.17.0.2
Path: /tmp
ReadOnly: false
Events:

Using PV and PVC with POD
Creating pv and pvc pod yaml
# catpv-pvc.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod
labels:
name: frontendhttp
spec:
containers:
- name: myfrontend
image: nginx
ports:
- containerPort: 80
name: "http-server"
volumeMounts:
- mountPath: "/usr/share/tomcat/html"
name: mypd
volumes:
- name: mypd
persistentVolumeClaim:
claimName: myclaim-1

In the above code, we have defined −
volumeMounts: → This is the path in the container on which the mounting will take place.
Volume: → This definition defines the volume definition that we are going to claim.
persistentVolumeClaim: → Under this, we define the volume name which we are going to use in the defined pod.

Create pv and pvcpod
# kubectl create -f pv-pvc.yaml
pod "mypod" created

Describe pod
# kubectl get pods
NAME READY STATUS RESTARTS AGE
mypod 1/1 Running 0 2m

clean up
# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
myclaim-1 Bound pv0001 10Gi RWO 13m

# kubectl delete pvc myclaim-1
persistentvolumeclaim "myclaim-1" deleted

# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv0001 10Gi RWO Retain Released default/myclaim-1 19m

# kubectl delete pv pv0001
persistentvolume "pv0001" deleted






Recent Comments

No comments

Leave a Comment