Ubuntu 18.04
Sponsored Link

Kubernetes : Use Persistent Storage2018/10/22

 
Use Persistent Storage in Kubernetes Cluster.
This example is based on the environment like follows.
For example, run NFS Server on Master Node and configure Pods can use NFS area as external storage.
-----------+---------------------------+--------------------------+------------
           |                           |                          |
       eth0|10.0.0.30              eth0|10.0.0.51             eth0|10.0.0.52
+----------+-----------+   +-----------+----------+   +-----------+----------+
|   [ dlp.srv.world ]  |   | [ node01.srv.world ] |   | [ node02.srv.world ] |
|      Master Node     |   |      Worker Node     |   |      Worker Node     |
|       NFS Server     |   |                      |   |                      |
+----------------------+   +----------------------+   +----------------------+

[1]
Run NFS Server on Master Node, refer to here.
On this example, configure [/var/lib/nfs-share] directory as NFS shared directory.
[2] Define PV (Persistent Volume) object and PVC (Persistent Volume Claim) object on Master Node.
# create PV definition

root@dlp:~#
vi nfs-pv.yml
apiVersion: v1
kind: PersistentVolume
metadata:
  # any PV name
  name: nfs-pv
spec:
  capacity:
    # storage size
    storage: 10Gi
  accessModes:
    # 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: /var/lib/nfs-share
    server: 10.0.0.30
    readOnly: false

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

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

NAME     CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
nfs-pv   10Gi       RWX            Retain           Available                                   5s

# create PVC definition

root@dlp:~#
vi nfs-pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  # any PVC name
  name: nfs-pvc
spec:
  accessModes:
  # ReadWriteMany(RW from multi nodes), ReadWriteOnce(RW from a node), ReadOnlyMany(R from multi nodes)
  - ReadWriteMany
  resources:
     requests:
       # storage size to use
       storage: 1Gi

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

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

NAME      STATUS    VOLUME    CAPACITY   ACCESS MODES   STORAGECLASS   AGE
nfs-pvc   Bound     nfs-pv    10Gi       RWX                           4s
[3] Create a Pod with PVC above.
root@dlp:~#
vi nginx-nfs.yml
apiVersion: v1
kind: Pod
metadata:
  # any Pod name
  name: nginx-nfs
  labels:
    name: 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@dlp:~#
kubectl create -f nginx-nfs.yml

pod "nginx-nfs" created
# create a test file under the NFS shared directory

root@dlp:~#
echo 'NFS Persistent Storage Test' > /var/lib/nfs-share/index.html
root@dlp:~#
kubectl get pods -o wide

NAME        READY   STATUS    RESTARTS   AGE   IP           NODE               NOMINATED NODE
nginx-nfs   1/1     Running   0          51s   10.244.2.4   node02.srv.world   <none>

# verify accessing

root@dlp:~#
curl 10.244.2.4

NFS Persistent Storage Test
Matched Content