Ubuntu 24.04
Sponsored Link

MicroK8s : Dynamic Volume Provisioning (NFS)2024/06/14

 

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.

[1]
Configure NFS Server on any node, refer to here.
On ths example, it uses [/home/nfsshare] directory on NFS Server that is running on [nfs.srv.world (10.0.0.35)].
[2] Install NFS Client Provisioner with Helm.
root@dlp:~#
microk8s helm3 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@dlp:~#
microk8s helm3 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 14 03:48:46 2024
NAMESPACE: kube-system
STATUS: deployed
REVISION: 1
TEST SUITE: None

root@dlp:~#
microk8s kubectl get pods -n kube-system

NAME                                                          READY   STATUS    RESTARTS       AGE
.....
.....
nfs-client-nfs-subdir-external-provisioner-864f5ff99b-rfqkz   1/1     Running   0              8m52s
[3] This is an example to use dynamic volume provisioning by a Pod.
root@dlp:~#
microk8s kubectl get storageclass

NAME                          PROVISIONER                                                RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
microk8s-hostpath (default)   microk8s.io/hostpath                                       Delete          WaitForFirstConsumer   false                  107m
nfs-client                    cluster.local/nfs-client-nfs-subdir-external-provisioner   Delete          Immediate              true                   9m29s

# create PVC

root@dlp:~#
vi my-pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-provisioner
spec:
  accessModes:
    - ReadWriteOnce
  # specify StorageClass name
  storageClassName: nfs-client
  resources:
    requests:
      # volume size
      storage: 5Gi

root@dlp:~#
microk8s kubectl apply -f my-pvc.yml

persistentvolumeclaim/my-provisioner configured
root@dlp:~#
microk8s kubectl get pvc

NAME             STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
my-provisioner   Bound    pvc-74a17dbd-6743-4472-9519-4f0b765fa2b4   5Gi        RWO            nfs-client     <unset>                 3m32s

# PV is generated dynamically

root@dlp:~#
microk8s kubectl get pv

NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                               STORAGECLASS        VOLUMEATTRIBUTESCLASS   REASON   AGE
pvc-74a17dbd-6743-4472-9519-4f0b765fa2b4   5Gi        RWO            Delete           Bound    default/my-provisioner              nfs-client          <unset>                          4m9s
pvc-fbf13c3e-0c3d-4ab0-bd26-0f139fdf1643   30Gi       RWX            Delete           Bound    container-registry/registry-claim   microk8s-hostpath   <unset>                          112m

root@dlp:~#
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

root@dlp:~#
microk8s kubectl apply -f my-pod.yml

deployment.apps/my-nginx created
root@dlp:~#
microk8s kubectl get pods -o wide

NAME                        READY   STATUS    RESTARTS   AGE     IP            NODE            NOMINATED NODE   READINESS GATES
my-nginx-66fc75ffb8-lkbzz   1/1     Running   0          2m45s   10.1.142.88   dlp.srv.world   <none>           <none>

root@dlp:~#
microk8s kubectl exec my-nginx-66fc75ffb8-lkbzz -- df /usr/share/nginx/html

Filesystem                                                                               1K-blocks  Used Available Use% Mounted on
10.0.0.35:/home/nfsshare/default-my-provisioner-pvc-74a17dbd-6743-4472-9519-4f0b765fa2b4 164028416     0 155623424   0% /usr/share/nginx/html

# verify accessing to create test index file

root@dlp:~#
echo "Nginx Index" > index.html

root@dlp:~#
microk8s kubectl cp index.html my-nginx-66fc75ffb8-lkbzz:/usr/share/nginx/html/index.html

root@dlp:~#
curl 10.1.142.88

Nginx Index
# when removing, to remove PVC, then PV is also removed dynamically

root@dlp:~#
microk8s kubectl delete deployment my-nginx

deployment.apps "my-nginx" deleted
root@dlp:~#
microk8s kubectl delete pvc my-provisioner

persistentvolumeclaim "my-provisioner" deleted
root@dlp:~#
microk8s kubectl get pv

NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                               STORAGECLASS        VOLUMEATTRIBUTESCLASS   REASON   AGE
pvc-fbf13c3e-0c3d-4ab0-bd26-0f139fdf1643   30Gi       RWX            Delete           Bound    container-registry/registry-claim   microk8s-hostpath   <unset>                          120m
[4] To use StatefulSet, it's possible to specify [volumeClaimTemplates].
root@dlp:~#
microk8s kubectl get storageclass

NAME                          PROVISIONER                                                RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
microk8s-hostpath (default)   microk8s.io/hostpath                                       Delete          WaitForFirstConsumer   false                  122m
nfs-client                    cluster.local/nfs-client-nfs-subdir-external-provisioner   Delete          Immediate              true                   24m

root@dlp:~#
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

root@dlp:~#
microk8s kubectl apply -f statefulset.yml

statefulset.apps/my-mginx created
root@dlp:~#
microk8s kubectl get statefulset

NAME       READY   AGE
my-mginx   1/1     10s

root@dlp:~#
microk8s kubectl get pods -o wide

NAME         READY   STATUS    RESTARTS   AGE   IP            NODE            NOMINATED NODE   READINESS GATES
my-mginx-0   1/1     Running   0          15s   10.1.142.89   dlp.srv.world   <none>           <none>

root@dlp:~#
microk8s kubectl get pvc

NAME              STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
data-my-mginx-0   Bound    pvc-ea072275-904a-4529-9fd1-453b4c5ff007   5Gi        RWO            nfs-client     <unset>                 46s

root@dlp:~#
microk8s kubectl get pv

NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                               STORAGECLASS        VOLUMEATTRIBUTESCLASS   REASON   AGE
pvc-ea072275-904a-4529-9fd1-453b4c5ff007   5Gi        RWO            Delete           Bound    default/data-my-mginx-0             nfs-client          <unset>                          72s
pvc-fbf13c3e-0c3d-4ab0-bd26-0f139fdf1643   30Gi       RWX            Delete           Bound    container-registry/registry-claim   microk8s-hostpath   <unset>                          124m
Matched Content