Kubernetes : Dynamic Volume Provisioning (NFS)2022/11/03 |
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: Thu Nov 3 06:24:42 2022 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-fc65bd7d7-65khg 1/1 Running 0 23s |
[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 35m 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-dfc3f716-f559-4746-a866-94b2aaff41ee 5Gi RWO nfs-client 8s # PV is generated dynamically root@ctrl:~# kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE pvc-dfc3f716-f559-4746-a866-94b2aaff41ee 5Gi RWO Delete Bound default/my-provisioner nfs-client 40s
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 14s 192.168.186.65 snode01.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-dfc3f716-f559-4746-a866-94b2aaff41ee 79158272 9739264 65764352 13% /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.186.65 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 41m
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 35s 192.168.186.66 snode01.srv.world <none> <none>root@ctrl:~# kubectl get pvc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE data-my-mginx-0 Bound pvc-2a22ed58-17a7-48de-9ff0-d283c936fa20 5Gi RWO nfs-client 72sroot@ctrl:~# kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE pvc-2a22ed58-17a7-48de-9ff0-d283c936fa20 5Gi RWO Delete Bound default/data-my-mginx-0 nfs-client 94s |
Sponsored Link |
|