Kubernetes : Use External Storage2022/11/02 |
Configure Persistent Storage in Kubernetes Cluster.
This example is based on the environment like follows.
For example, run NFS Server on Control Plane Node and configure Pods can use NFS area as external storage.
-----------+---------------------------+--------------------------+------------ | | | eth0|10.0.0.25 eth0|10.0.0.71 eth0|10.0.0.72 +----------+-----------+ +-----------+-----------+ +-----------+-----------+ | [ ctrl.srv.world ] | | [snode01.srv.world] | | [snode02.srv.world] | | Control Plane | | Worker Node | | Worker Node | +----------------------+ +-----------------------+ +-----------------------+ |
[1] |
Run NFS Server on Control Plane Node, refer to here.
On this example, configure [/home/nfsshare] directory as NFS share. |
[2] | Create PV (Persistent Volume) object and PVC (Persistent Volume Claim) object on Control Plane Node. |
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's definition path: /home/nfsshare server: 10.0.0.25 readOnly: false kubectl create -f nfs-pv.yml persistentvolume "nfs-pv" created root@ctrl:~# kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE nfs-pv 10Gi RWX Retain Available 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 root@ctrl:~# kubectl get pvc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE nfs-pvc Bound nfs-pv 10Gi RWX 11s |
[3] | Create Pods with PVC above. |
root@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
root@ctrl:~#
root@ctrl:~# kubectl create -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-7c7d5d4896-k2kn4 1/1 Running 0 93s 192.168.186.68 snode01.srv.world <none> <none> nginx-nfs-7c7d5d4896-stkkq 1/1 Running 0 93s 192.168.186.67 snode01.srv.world <none> <none> nginx-nfs-7c7d5d4896-z8898 1/1 Running 0 94s 192.168.211.130 snode02.srv.world <none> <none>root@ctrl:~# kubectl expose deployment nginx-nfs --type="NodePort" --port 80 service/nginx-nfs exposed root@ctrl:~# kubectl get service nginx-nfs NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx-nfs NodePort 10.104.124.105 <none> 80:32037/TCP 5s # create a test file under the NFS shared directory root@ctrl:~# echo 'NFS Persistent Storage Test' > /home/nfsshare/index.html
# verify accesses root@ctrl:~# curl 10.104.124.105 NFS Persistent Storage Test |
Sponsored Link |