MicroK8s : Use External Storage2026/05/19 |
|
Configure External Storage in MicroK8s. For example, it uses NFS for it. |
|
| [1] |
Configure NFS Server on any node, refer to here. |
| [2] | Create PV (Persistent Volume) object and PVC (Persistent Volume Claim) object. |
apiVersion: v1 kind: PersistentVolume metadata: # any PV name name: nfs-pv spec: capacity: # storage size storage: 10Gi accessModes: # Access Modes: # - ReadWriteMany (RW from multi nodes) # - ReadWriteOnce (RW from a node) # - ReadOnlyMany (R from multi nodes) - ReadWriteMany persistentVolumeReclaimPolicy: # retain even if pods terminate Retain storageClassName: nfs-server nfs: # NFS server's definition path: /home/nfsshare server: 10.0.0.35 readOnly: false microk8s kubectl apply -f nfs-pv.yml persistentvolume "nfs-pv" created ubuntu@dlp:~$ microk8s kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS VOLUMEATTRIBUTESCLASS REASON AGE nfs-pv 10Gi RWX Retain Available nfs-server <unset> 6s
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
# any PVC name
name: nfs-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10Gi
storageClassName: nfs-server
microk8s kubectl apply -f nfs-pvc.yml persistentvolumeclaim "nfs-pvc" created ubuntu@dlp:~$ microk8s kubectl get pvc AME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE nfs-pvc Bound nfs-pv 10Gi RWX nfs-server <unset> 7s |
| [3] | Create Pods with PVC above. |
|
ubuntu@dlp:~$
vi nginx-nfs.yml apiVersion: apps/v1 kind: Deployment metadata: # any Deployment name name: nginx-nfs labels: name: nginx-nfs spec: replicas: 3 selector: matchLabels: app: nginx-nfs template: metadata: labels: app: nginx-nfs spec: containers: - name: nginx-nfs image: nginx ports: - name: web containerPort: 80 volumeMounts: - name: nfs-share # mount point in container mountPath: /usr/share/nginx/html volumes: - name: nfs-share persistentVolumeClaim: # PVC name you created claimName: nfs-pvc
ubuntu@dlp:~$
ubuntu@dlp:~$ microk8s kubectl apply -f nginx-nfs.yml deployment.apps/nginx-nfs created microk8s kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-nfs-d9bcf65f5-dc9gb 1/1 Running 0 36s 10.1.142.72 dlp.srv.world <none> <none> nginx-nfs-d9bcf65f5-hhqxg 1/1 Running 0 36s 10.1.40.197 node01.srv.world <none> <none> nginx-nfs-d9bcf65f5-w5grw 1/1 Running 0 36s 10.1.142.73 dlp.srv.world <none> <none>ubuntu@dlp:~$ microk8s kubectl expose deployment nginx-nfs --type="NodePort" --port 80 service/nginx-nfs exposed ubuntu@dlp:~$ microk8s kubectl get service nginx-nfs NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx-nfs NodePort 10.152.183.53 <none> 80:32071/TCP 5s # create a test file under the NFS shared directory ubuntu@dlp:~$ microk8s kubectl exec nginx-nfs-d9bcf65f5-dc9gb -- sh -c "echo 'NFS Persistent Storage Test' > /usr/share/nginx/html/index.html"
# verify accesses on a node which users can access to microk8s cluster ubuntu@dlp:~$ curl 10.152.183.53 NFS Persistent Storage Test |
| Sponsored Link |
|
|