Merge pull request #29488 from lkxed/20230605-1-How-to-Setup-Dynamic-NFS-Provisioning-in-Kubernetes-Cluster

[手动选题][tech]: 20230605.1 ️ How to Setup Dynamic NFS Provisioning in Kubernetes Cluster.md
This commit is contained in:
Xingyu.Wang 2023-06-06 15:11:45 +08:00 committed by GitHub
commit 4d04de0187
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,237 @@
[#]: subject: "How to Setup Dynamic NFS Provisioning in Kubernetes Cluster"
[#]: via: "https://www.linuxtechi.com/dynamic-nfs-provisioning-kubernetes/"
[#]: author: "Pradeep Kumar https://www.linuxtechi.com/author/pradeep/"
[#]: collector: "lkxed"
[#]: translator: " "
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
How to Setup Dynamic NFS Provisioning in Kubernetes Cluster
======
In this post, we will show you how to setup dynamic nfs provisioning in Kubernetes (k8s) cluster.
Dynamic NFS storage provisioning in Kubernetes allows you to automatically provision and manage NFS (Network File System) volumes for your Kubernetes applications on-demand. It enables the creation of persistent volumes (PVs) and persistent volume claims (PVCs) without requiring manual intervention or pre-provisioned storage.
The NFS provisioner is responsible for dynamically creating PVs and binding them to PVCs. It interacts with the NFS server to create directories or volumes for each PVC.
##### Prerequisites
- Pre-installed Kubernetes Cluster
- A Regular user which has admin rights on Kubernetes cluster
- Internet Connectivity
Without any further delay, lets deep dive into steps
### Step 1) Prepare the NFS Server
In my case, I am going to install NFS server on my Kubernetes master node (Ubuntu 22.04). Login to master node and run following commands,
```
$ sudo apt update
$ sudo apt install nfs-kernel-server -y
```
Create the following folder and share it using nfs,
```
$ sudo mkdir /opt/dynamic-storage
$ sudo chown -R nobody:nogroup /opt/dynamic-storage
$ sudo chmod 777 /opt/dynamic-storage
```
Add the following entries in /etc/exports file
```
$ sudo vi /etc/exports
/opt/dynamic-storage 192.168.1.0/24(rw,sync,no_subtree_check)
```
Save and close the file.
Note: Dont forget to change network in exports file that suits to your deployment.
To make above changes into the effect, run
```
$ sudo exportfs -a
$ sudo systemctl restart nfs-kernel-server
$ sudo systemctl status nfs-kernel-server
```
![NFS-Service-Status-Kubernetes-Master-Ubuntu][1]
On the worker nodes, install nfs-common package using following apt command.
```
$ sudo apt install nfs-common -y
```
### Step 2) Install and Configure NFS Client Provisioner
NFS subdir external provisioner deploy the NFS client provisioner in your Kubernetes cluster. The provisioner is responsible for dynamically creating and managing Persistent Volumes (PVs) and Persistent Volume Claims (PVCs) backed by NFS storage.
So, to install NFS subdir external provisioner, first install helm using following set of commands,
```
$ curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
$ chmod 700 get_helm.sh
$ ./get_helm.sh
```
Enable the helm repo by running following beneath command,
```
$ helm repo add nfs-subdir-external-provisioner https://kubernetes-sigs.github.io/nfs-subdir-external-provisioner
```
Deploy provisioner using following helm command
```
$ helm install -n nfs-provisioning --create-namespace nfs-subdir-external-provisioner nfs-subdir-external-provisioner/nfs-subdir-external-provisioner --set nfs.server=192.168.1.139 --set nfs.path=/opt/dynamic-storage
```
![helm-install-nfs-provisioning-kubernetes-cluster][2]
Above helm command will automatically create nfs-provisioning namespace and will install nfs provisioner pod/deployment, storage class with name (nfs-client) and will created the required rbac.
```
$ kubectl get all -n nfs-provisioning
$ kubectl get sc -n nfs-provisioning
```
![kubectl-get-all-nfs-provisioning-kubernetes-cluster][3]
Perfect, output above confirms that provisioner pod and storage class is created successfully.
### Step 3) Create Persistent Volume Claims (PVCs)
Lets a create PVC to request storage for your pod or deployment. PVC will request for a specific amount of storage from a StorageClass (nfs-client).
```
$ vi demo-pvc.yml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: demo-claim
  namespace: nfs-provisioning
spec:
  storageClassName: nfs-client
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Mi
```
save & close the file.
![PVC-Yaml-Dynamic-NFS-Kubernetes][4]
Run following kubectl command to create pvc using above created yml file,
```
$ kubectl create -f demo-pvc.yml
```
Verify whether PVC and PV are created or not,
```
$ kubectl get pv,pvc -n nfs-provisioning
```
![Verify-pv-pvc-dynamic-nfs-kubernetes-cluster][5]
Great, above output shows that pv and pvc are created successfully.
### Step 4) Test and Verify Dynamic NFS Provisioning
In order to test and verify dynamic nfs provisioning, spin up a test pod using following yml file,
```
$ vi test-pod.yml
kind: Pod
apiVersion: v1
metadata:
  name: test-pod
  namespace: nfs-provisioning
spec:
  containers:
  - name: test-pod
    image: busybox:latest
    command:
      - "/bin/sh"
    args:
      - "-c"
      - "touch /mnt/SUCCESS && sleep 600"
    volumeMounts:
      - name: nfs-pvc
        mountPath: "/mnt"
  restartPolicy: "Never"
  volumes:
    - name: nfs-pvc
      persistentVolumeClaim:
        claimName: demo-claim
```
![Pod-Yml-Dynamic-NFS-kubernetes][6]
Deploy the pod using following kubectl command,
```
$ kubectl create -f test-pod.yml
```
Verify the status of test-pod,
```
$ kubectl get pods -n nfs-provisioning
```
![Verify-Test-Pod-Using-NFS-Volume-Kubernetes][7]
Login to the pod and verify that nfs volume is mounted or not.
```
$ kubectl exec -it test-pod -n nfs-provisioning /bin/sh
```
![Access-Dynamic-NFS-Inside-Pod-Kubernetes][8]
Great, above output from the pod confirms that dynamic NFS volume is mounted and accessible.
In the last, delete the pod and PVC and check whether pv is deleted automatically or not.
```
$ kubectl delete -f test-pod.yml
$ kubectl delete -f demo-pvc.yml
$ kubectl get pv,pvc -n  nfs-provisioning
```
![Delete-Pod-PVC-Dynamic-NFS][9]
Thats all from this post, I hope you have found it informative. Feel free to post your queries and feedback in below comments section.
--------------------------------------------------------------------------------
via: https://www.linuxtechi.com/dynamic-nfs-provisioning-kubernetes/
作者:[Pradeep Kumar][a]
选题:[lkxed][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.linuxtechi.com/author/pradeep/
[b]: https://github.com/lkxed/
[1]: https://www.linuxtechi.com/wp-content/uploads/2023/06/NFS-Service-Status-Kubernetes-Master-Ubuntu.png
[2]: https://www.linuxtechi.com/wp-content/uploads/2023/06/helm-install-nfs-provisioning-kubernetes-cluster.png
[3]: https://www.linuxtechi.com/wp-content/uploads/2023/06/kubectl-get-all-nfs-provisioning-kubernetes-cluster.png
[4]: https://www.linuxtechi.com/wp-content/uploads/2023/06/PVC-Yaml-Dynamic-NFS-Kubernetes.png
[5]: https://www.linuxtechi.com/wp-content/uploads/2023/06/Verify-pv-pvc-dynamic-nfs-kubernetes-cluster.png
[6]: https://www.linuxtechi.com/wp-content/uploads/2023/06/Pod-Yml-Dynamic-NFS-kubernetes.png
[7]: https://www.linuxtechi.com/wp-content/uploads/2023/06/Verify-Test-Pod-Using-NFS-Volume-Kubernetes.png
[8]: https://www.linuxtechi.com/wp-content/uploads/2023/06/Access-Dynamic-NFS-Inside-Pod-Kubernetes.png
[9]: https://www.linuxtechi.com/wp-content/uploads/2023/06/Delete-Pod-PVC-Dynamic-NFS.png