Pages

Tuesday, August 7, 2018

Kubernetes - Volumes with NFS

Share it Please
While making a NFS or any cloud backed storage we Will require some additional components. We need to create a Persistent Volume and Persistent volume Claim for using the volumes

PersistentVolume - Low level representation of the storage volume
PersistentVolumeClaim - Binding between a Pod and Persistent Volume
Volume Driver - code that will be used to communicate with the back end storage
StorageClass - Allows dynamic provisioning of Persistent volume

Lets create a Persistent Volume,
[root@manja17-I13330 kubenetes-config]# cat nfs-pv.yml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: test-nfs
spec:
  capacity:
    storage: 1Mi
  accessModes:
    - ReadWriteMany
  nfs:
    server: 10.131.175.138
    path: "/nfsshare"

In the above config, we are actually using a nfs volume from the ip address “10.131.175.138” where nfs shares where already created under the /nfsshare. Run this and see how it works

[root@manja17-I13330 kubenetes-config]# kubectl create -f nfs-pv.yml
persistentvolume "test-nfs" created
If we check the mount command from our worker nodes we can see the below output, 

[root@manja17-I14021 nfsshare]# mount | grep nfsshare
10.131.175.138:/nfsshare on /mnt/nfsshare type nfs4 (rw,relatime,vers=4.1,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=10.131.36.181,local_lock=none,addr=10.131.175.138)

We can see that the /nfsshare from 10.131.165.138 is mounted onto the /mnt/nfssshare on the worker nodes. In the above case we are creating a persistent volume with 1MI size

Now lets create a PersistentVolumeClaim -
[root@manja17-I13330 kubenetes-config]# cat nfs-pvc.yml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: test-nfs
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Mi

The Persistent volume claim works in different way. In the above configuration we are asking K8 to provide us a volume which has storage of 1MI. We are not calling the persistent volume directly rather we are creating a persistent Volume claim and giving K8 the authority of identifying the volumes with the size that we requested.if that finds with a pv with that volume it allocates that to the pod.
Sample Pod Configuration - Now lets create a Pod which will use the NFS volume
[root@manja17-I13330 kubenetes-config]# cat nfs-pod.yml
apiVersion: v1
kind: ReplicationController
metadata:
  name: nfs-web
spec:
  replicas: 1
  selector:
    role: web-frontend
  template:
    metadata:
      labels:
        role: web-frontend
    spec:
      containers:
      - name: web
        image: nginx
        ports:
          - name: web
            containerPort: 80
        volumeMounts:
            # name must match the volume name below
            - name: test-nfs
              mountPath: "/usr/share/nginx/html"
      volumes:
      - name: test-nfs
        persistentVolumeClaim:
          claimName: test-nfs

In the above pod configuration, the main elements are Volumes and Volume mounts. The volume element contains the Persistent Volume claim details that we created earlier. The Volume mount is the place where if volume with our requirement is available it will mount on the location /usr/share/nginx/html inside the container.

Run the Container and see how it works -
[root@manja17-I13330 kubenetes-config]# kubectl get pods
NAME                       READY          STATUS             RESTARTS   AGE
nfs-web-qk2qn         1/1               Running            0                4m

[root@manja17-I13330 kubenetes-config]# kubectl exec -it nfs-web-qk2qn -- bash
root@nfs-web-qk2qn:/# cd /usr/share/nginx/html/
root@nfs-web-qk2qn:/usr/share/nginx/html# touch container.txt
root@nfs-web-qk2qn:/usr/share/nginx/html# cat container.txt
root@nfs-web-qk2qn:/usr/share/nginx/html# echo "this is from container" >> container.txt
root@nfs-web-qk2qn:/usr/share/nginx/html# cat container.txt
this is from container
root@nfs-web-qk2qn:/usr/share/nginx/html# exit

exit

No comments :

Post a Comment