Pages

Friday, July 20, 2018

Kubernetes Volumes

There will be always cases where we need to save data on to a drive or disk. Containers
when stopped will destroy the data stored inside the container. Kubernetes provides us a way to manage storage for containers. Kubernetes uses volumes for adding additional storage to the pods. In this article we will see how we can create volumes and attach them to the pods. For the demo we will use NFS as our additional drive.

Important elements for running a state ful containers are,
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 volumes.

I used this link for creating nfs share and access that from the worker nodes where our pods
will run.

Persistent Volume - lets create a persistent volume for our Pod.
[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

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 -
[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
mysql             1/1        Running            0 1d
nfs-web-qk2qn     1/1        Running            0 4m

Now log in to the nfs container and see if we can access the nfs drives,
[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

More to Come, Happy learning :-)

No comments :

Post a Comment