Kubernetes : Use External Storage2025/01/27 |
|
Configure Persistent Storage in Kubernetes Cluster. 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 cluster that pods can use NFS share as external storage on NFS server [nfs.srv.world (10.0.0.35)]. |
|
| [1] |
Configure NFS Server, 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 nfs: # NFS server definition path: /home/nfsshare server: 10.0.0.35 readOnly: false kubectl create -f nfs-pv.yml persistentvolume "nfs-pv" created [centos@ctrl ~]$ kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS VOLUMEATTRIBUTESCLASS REASON AGE nfs-pv 10Gi RWX Retain Available <unset> 5s
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
# any PVC name
name: nfs-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10Gi
kubectl create -f nfs-pvc.yml persistentvolumeclaim "nfs-pvc" created [centos@ctrl ~]$ kubectl get pvc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE nfs-pvc Bound nfs-pv 10Gi RWX <unset> 4s |
| [3] | Create Pods with PVC above. |
|
[centos@ctrl ~]$
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
[centos@ctrl ~]$
[centos@ctrl ~]$ kubectl apply -f nginx-nfs.yml deployment.apps/nginx-nfs created kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-nfs-57fb56bc6b-6dsm6 1/1 Running 0 12s 192.168.241.134 node02.srv.world <none> <none> nginx-nfs-57fb56bc6b-9jzm6 1/1 Running 0 12s 192.168.40.195 node01.srv.world <none> <none> nginx-nfs-57fb56bc6b-rjp4n 1/1 Running 0 12s 192.168.241.133 node02.srv.world <none> <none>
[centos@ctrl ~]$
kubectl expose deployment nginx-nfs --type="NodePort" --port 80 service/nginx-nfs exposed [centos@ctrl ~]$ kubectl port-forward service/nginx-nfs --address 127.0.0.1 8081:80 &
# create a test file under the NFS share [centos@ctrl ~]$ kubectl exec nginx-nfs-57fb56bc6b-6dsm6 -- sh -c "echo 'NFS Persistent Storage Test' > /usr/share/nginx/html/index.html"
# verify access [centos@ctrl ~]$ curl localhost:8081 Handling connection for 8081 NFS Persistent Storage Test |
| Sponsored Link |
|
|