Disclaimer
PersistentVolume and PersistentVolumeClaim is API for users and administrators that abstracts details of how storage is provided from how it is consumed. PV are resources in the cluster. PVC are requests for those resources and also act as claim checks to the resource.
PersistentVolume life cycle is :
Two types of PV provisioning is static (PV needs to be created before by administrator) and dynamic (PV is created at the same time of PVC, need to configure StorageClass first).
Source image : https://btech.id/
Persistent Volumes (VP)
Persistent Volumes Claim (VPC)
Logical Topology
Workflow
sudo apt -y install nfs-common
sudo mount -t nfs 10.10.10.5:/nfs-path-dir/ local-dir
#kubectl apply -f nfs-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv
spec:
capacity:
storage: 15Gi
volumeMode: Filesystem
##storageClassName: slow
##persistentVolumeReclaimPolicy: Retain
accessModes:
- ReadWriteMany
nfs:
server: kubernetes.nfs
path: "/volumes"
#kubectl apply -f nfs-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nfs-pvc
spec:
accessModes:
- ReadWriteMany
##storageClassName: slow
resources:
requests:
storage: 15Gi
kubernetes@master:~$ kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
nfs-pv 15Gi RWX Retain Bound default/nfs-pvc 44m
kubernetes@master:~$ kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
nfs-pvc Bound nfs-pv 15Gi RWX 42m
kubernetes@kubernetes:~$
apiVersion: v1
kind: Pod
metadata:
name: nginx-with-pv
spec:
volumes:
- name: nginx-with-pv
persistentVolumeClaim:
claimName: nfs-pvc
containers:
- name: nginx-with-pv
image: nginx
ports:
- containerPort: 80
name: "http-server"
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: nginx-with-pv
kubernetes.master$ kubectl get pod
kubernetes.master$ echo "hello world!" > /volumes/index.html
kubernetes.master$ kubectl exec -it [nginx-pod-name] /bin/bash
nginx-pod# curl http://localhost
Reference : https://kubernetes.io/docs/tasks/configure-pod-container/configure-persistent-volume-storage/