Kubernetes : Dynamic Volume Provisioning (NFS)2024/06/07 |
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. +----------------------+ +----------------------+ | [ ctrl.srv.world ] | | [ dlp.srv.world ] | | Manager Node | | Control Plane | +-----------+----------+ +-----------+----------+ eth0|10.0.0.25 eth0|10.0.0.30 | | ------------+--------------------------+----------- | | eth0|10.0.0.51 eth0|10.0.0.52 +-----------+----------+ +-----------+----------+ | [ node01.srv.world ] | | [ node02.srv.world ] | | Worker Node#1 | | Worker Node#2 | +----------------------+ +----------------------+ |
For example, configure dynamic volume provisioning with NFS provisioner that NFS storage is provided from [nfs.srv.world (10.0.0.35)]. |
|
[1] |
Run NFS Server, refer to here. |
[2] |
Worker Nodes need to be able to mount NFS share on NFS server. |
[3] | |
[4] | Install NFS Client Provisioner. |
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.35 --set nfs.path=/home/nfsshare nfs-subdir-external-provisioner/nfs-subdir-external-provisioner
NAME: nfs-client LAST DEPLOYED: Fri Jun 7 04:45:13 2024 NAMESPACE: kube-system STATUS: deployed REVISION: 1 TEST SUITE: Noneroot@ctrl:~# kubectl get deployment -n kube-system NAME READY UP-TO-DATE AVAILABLE AGE calico-kube-controllers 1/1 1 1 4h54m coredns 2/2 2 2 4h55m metrics-server 1/1 1 1 16m nfs-client-nfs-subdir-external-provisioner 1/1 1 1 19s |
[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 83s apiVersion: v1 kind: PersistentVolumeClaim metadata: name: my-provisioner spec: accessModes: - ReadWriteOnce # specify StorageClass name storageClassName: nfs-client 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 VOLUMEATTRIBUTESCLASS AGE my-provisioner Bound pvc-7b9f17ed-fc6f-4e7e-99dc-96324c1a4076 5Gi RWO nfs-client <unset> 5s # PV is generated dynamically root@ctrl:~# kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS VOLUMEATTRIBUTESCLASS REASON AGE pvc-7b9f17ed-fc6f-4e7e-99dc-96324c1a4076 5Gi RWO Delete Bound default/my-provisioner nfs-client <unset> 35s
root@ctrl:~#
vi my-pod.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
selector:
matchLabels:
run: my-nginx
replicas: 1
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- name: my-nginx
image: nginx
ports:
- containerPort: 80
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 deployment.apps/my-nginx created root@ctrl:~# kubectl get pods NAME READY STATUS RESTARTS AGE my-nginx-69d9cb4f47-zzjj7 1/1 Running 0 5sroot@ctrl:~# kubectl exec my-nginx-69d9cb4f47-zzjj7 -- df /usr/share/nginx/html Filesystem 1K-blocks Used Available Use% Mounted on 10.0.0.35:/home/nfsshare/default-my-provisioner-pvc-7b9f17ed-fc6f-4e7e-99dc-96324c1a4076 164028416 0 155623424 0% /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-nginx-69d9cb4f47-zzjj7:/usr/share/nginx/html/index.html root@ctrl:~# kubectl expose deployment my-nginx --type="NodePort" --port 80 root@ctrl:~# kubectl port-forward service/my-nginx --address 127.0.0.1 80:80 & root@ctrl:~# curl localhost Handling connection for 80 Nginx Index # when removing pods, to remove PVC, then PV is also removed dynamically root@ctrl:~# kubectl delete deployment my-nginx deployment.apps "my-nginx" deleted root@ctrl:~# kubectl delete pvc my-provisioner persistentvolumeclaim "my-provisioner" deleted root@ctrl:~# kubectl get pv No resources found |
[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 9m51s
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 6sroot@ctrl:~# kubectl get pods NAME READY STATUS RESTARTS AGE my-mginx-0 1/1 Running 0 31sroot@ctrl:~# kubectl get pvc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE data-my-mginx-0 Bound pvc-4e2ce32d-c318-4669-b570-949043fc9d30 5Gi RWO nfs-client <unset> 48sroot@ctrl:~# kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS VOLUMEATTRIBUTESCLASS REASON AGE pvc-4e2ce32d-c318-4669-b570-949043fc9d30 5Gi RWO Delete Bound default/data-my-mginx-0 nfs-client <unset> 78s |
Sponsored Link |