Kubernetes : Dynamic Volume Provisioning (NFS)2023/07/28 |
To use Dynamic Volume Provisioning feature when using Persistent Storage, it's possible to create PV (Persistent Volume) dynamically without creating PV manually by Cluster Administrator when created PVC (Persistent Volume Claim) by users. This example is based on the environment like follows. For example, run NFS Server on Control Plane Node and configure dynamic volume provisioning with NFS provisioner. -----------+---------------------------+--------------------------+------------ | | | eth0|10.0.0.25 eth0|10.0.0.71 eth0|10.0.0.72 +----------+-----------+ +-----------+-----------+ +-----------+-----------+ | [ ctrl.srv.world ] | | [snode01.srv.world] | | [snode02.srv.world] | | Control Plane | | Worker Node | | Worker Node | +----------------------+ +-----------------------+ +-----------------------+ |
[1] |
Run NFS Server on Control Plane Node, refer to here.
On this example, configure [/home/nfsshare] directory as NFS share. |
[2] | |
[3] | Install NFS Client Provisioner with Helm. |
root@ctrl:~#
helm repo add nfs-subdir-external-provisioner https://kubernetes-sigs.github.io/nfs-subdir-external-provisioner/
# nfs.server = (NFS server's hostname or IP address) # nfs.path = (NFS share Path) root@ctrl:~# helm install nfs-client -n kube-system --set nfs.server=10.0.0.25 --set nfs.path=/home/nfsshare nfs-subdir-external-provisioner/nfs-subdir-external-provisioner
NAME: nfs-client LAST DEPLOYED: Fri Jul 28 01:35:08 2023 NAMESPACE: kube-system STATUS: deployed REVISION: 1 TEST SUITE: Noneroot@ctrl:~# kubectl get pods -n kube-system NAME READY STATUS RESTARTS AGE ..... ..... nfs-client-nfs-subdir-external-provisioner-8689b9b8c6-n4s2f 1/1 Running 0 28s |
[4] | This is an example to use dynamic volume provisioning by a Pod. |
root@ctrl:~# kubectl get pv No resources found in default namespace. root@ctrl:~# kubectl get pvc No resources found in default namespace. root@ctrl:~# kubectl get storageclass NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE nfs-client cluster.local/nfs-client-nfs-subdir-external-provisioner Delete Immediate true 2m10s apiVersion: v1 kind: PersistentVolumeClaim metadata: name: my-provisioner annotations: # specify StorageClass name volume.beta.kubernetes.io/storage-class: nfs-client spec: accessModes: - ReadWriteOnce resources: requests: # volume size storage: 5Gi
root@ctrl:~#
root@ctrl:~# kubectl apply -f my-pvc.yml persistentvolumeclaim/my-provisioner created kubectl get pvc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE my-provisioner Bound pvc-b3f9795c-95c6-4fc5-926c-80bdbc2e4296 5Gi RWO nfs-client 9s # PV is generated dynamically root@ctrl:~# kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE pvc-b3f9795c-95c6-4fc5-926c-80bdbc2e4296 5Gi RWO Delete Bound default/my-provisioner nfs-client 34s
root@ctrl:~#
vi my-pod.yml
apiVersion: v1
kind: Pod
metadata:
name: my-mginx
spec:
containers:
- name: my-mginx
image: nginx
ports:
- containerPort: 80
name: web
volumeMounts:
- mountPath: /usr/share/nginx/html
name: nginx-pvc
volumes:
- name: nginx-pvc
persistentVolumeClaim:
# PVC name you created
claimName: my-provisioner
kubectl apply -f my-pod.yml pod/my-mginx created root@ctrl:~# kubectl get pod my-mginx -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES my-mginx 1/1 Running 0 6s 192.168.211.142 snode02.srv.world <none> <none>root@ctrl:~# kubectl exec my-mginx -- df /usr/share/nginx/html Filesystem 1K-blocks Used Available Use% Mounted on 10.0.0.25:/home/nfsshare/default-my-provisioner-pvc-b3f9795c-95c6-4fc5-926c-80bdbc2e4296 80503808 4645888 71723008 7% /usr/share/nginx/html # verify accessing to create test index file root@ctrl:~# echo "Nginx Index" > index.html root@ctrl:~# kubectl cp index.html my-mginx:/usr/share/nginx/html/index.html root@ctrl:~# curl 192.168.211.142 Nginx Index # when removing, to remove PVC, then PV is also removed dynamically root@ctrl:~# kubectl delete pod my-mginx pod "my-mginx" deleted root@ctrl:~# kubectl delete pvc my-provisioner persistentvolumeclaim "my-provisioner" deleted root@ctrl:~# kubectl get pv No resources found in default namespace. |
[5] | To use StatefulSet, it's possible to specify [volumeClaimTemplates]. |
root@ctrl:~# kubectl get pv No resources found in default namespace. root@ctrl:~# kubectl get pvc No resources found in default namespace. root@ctrl:~# kubectl get storageclass NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE nfs-client cluster.local/nfs-client-nfs-subdir-external-provisioner Delete Immediate true 6m54s
root@ctrl:~#
vi statefulset.yml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: my-mginx
spec:
serviceName: my-mginx
replicas: 1
selector:
matchLabels:
app: my-mginx
template:
metadata:
labels:
app: my-mginx
spec:
containers:
- name: my-mginx
image: nginx
volumeMounts:
- name: data
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: data
spec:
# specify StorageClass name
storageClassName: nfs-client
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 5Gi
kubectl apply -f statefulset.yml statefulset.apps/my-mginx created root@ctrl:~# kubectl get statefulset NAME READY AGE my-mginx 1/1 10sroot@ctrl:~# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES my-mginx-0 1/1 Running 0 17s 192.168.211.143 snode02.srv.world <none> <none>root@ctrl:~# kubectl get pvc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE data-my-mginx-0 Bound pvc-0eff9731-5cb6-4b75-9772-202d2f9d27f7 5Gi RWO nfs-client 45sroot@ctrl:~# kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE pvc-0eff9731-5cb6-4b75-9772-202d2f9d27f7 5Gi RWO Delete Bound default/data-my-mginx-0 nfs-client 59s |
Sponsored Link |
|