Ubuntu 24.04
Sponsored Link

Kubernetes : Use External Storage2024/06/07

 

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.
On this example, set [/home/nfsshare] directory as NFS share.

[2] Create PV (Persistent Volume) object and PVC (Persistent Volume Claim) object.
# create PV definition

root@ctrl:~#
vi nfs-pv.yml
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

root@ctrl:~#
kubectl create -f nfs-pv.yml

persistentvolume "nfs-pv" created
root@ctrl:~#
kubectl get pv

NAME     CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   VOLUMEATTRIBUTESCLASS   REASON   AGE
nfs-pv   10Gi       RWX            Retain           Available                          <unset>                          4s

# create PVC definition

root@ctrl:~#
vi nfs-pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  # any PVC name
  name: nfs-pvc
spec:
  accessModes:
  - ReadWriteMany
  resources:
     requests:
       storage: 10Gi

root@ctrl:~#
kubectl create -f nfs-pvc.yml

persistentvolumeclaim "nfs-pvc" created
root@ctrl:~#
kubectl get pvc

NAME      STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
nfs-pvc   Bound    nfs-pv   10Gi       RWX                           <unset>                 12s
[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:~#
kubectl apply -f nginx-nfs.yml

deployment.apps/nginx-nfs created
root@ctrl:~#
kubectl get pods -o wide

NAME                         READY   STATUS    RESTARTS   AGE     IP                NODE               NOMINATED NODE   READINESS GATES
nginx-nfs-75cd8b4bb5-7b6qw   1/1     Running   0          2m26s   192.168.40.197    node01.srv.world   <none>           <none>
nginx-nfs-75cd8b4bb5-7gp7d   1/1     Running   0          2m26s   192.168.241.131   node02.srv.world   <none>           <none>
nginx-nfs-75cd8b4bb5-zqgsh   1/1     Running   0          2m26s   192.168.241.132   node02.srv.world   <none>           <none>

root@ctrl:~#
kubectl expose deployment nginx-nfs --type="NodePort" --port 80

service/nginx-nfs exposed
root@ctrl:~#
kubectl port-forward service/nginx-nfs --address 0.0.0.0 80:80 &
# create a test file under the NFS share

root@ctrl:~#
kubectl exec nginx-nfs-75cd8b4bb5-7b6qw -- sh -c "echo 'NFS Persistent Storage Test' > /usr/share/nginx/html/index.html"
[4] Access to application and verify running.
Matched Content