Wednesday, May 23, 2018

Kubernetes - Volumes

Volumes in Kubernetes are a directory accessible to all containers running in a Pod. It also provides additional guarantee that the data is preserved across container restarts. A Volume can be backed with aws or nfs or azure or even the host location. below are the different types of volumes,
   Node local Volumes such as emptyDir or hostPath
   Network volumes such as nfs , glusterfs or cephfs
   Cloud volumes such as Aws elastic block, Azure disk and gce Persistent disk
   Special volumes like secretes or gitrepo

In this article we will see how we can use the node local volumes such as emptyDir and hostPath.

emptyDir - A volume type that provides an empty directory that containers in the pod can read and write from. When the pod is removed from the node , the data in the emptyDir is deleted for ever. As the name says it is empty. This volume type can be used with either the storage volumes of the node machine itself or an optional RAM disk for higher performance. As said the data will be lost during machine reboots and this can be used when we need some temporary space or have containers that process data in the emptyDir and hand it off to another container. The emptyDir volume can be attached from the local node or from local memory event. To Create a emptyDir volume we can create as,
[root@manja17-I13330 kubenetes-config]# cat emptyDir-mysql.yml
apiVersion: v1
kind: Pod
metadata:
 name: "mysql"
 labels:
  name: "lbl-k8s-mysql"
spec:
 containers:
  - image: docker.io/jagadesh1982/testing-service
    name:  "testing"
    imagePullPolicy: IfNotPresent
    ports:
     - containerPort: 3306
    volumeMounts:
      - mountPath: /data-mount
        name: data
 volumes:
   - name: data
     emptyDir: {}

Now when we login to the container and see the mount points we can see,
[root@manja17-I13330 kubenetes-config]# kubectl exec mysql -c testing -it bash
root@mysql:/usr/src/app# cd /data-mount
root@mysql:/data-mount# pwd
/data-mount
root@mysql:/data-mount# touch hello
root@mysql:/data-mount# echo "hello world" >> ./hello
root@mysql:/data-mount# exit
exit

We can see that a mount point /data-mount is available inside the container.

emptyDir with memory - Lets create the same above emptyDir with the medium memory as below,
[root@manja17-I13330 kubenetes-config]# cat emptyDir-memory-pod.yml
apiVersion: v1
kind: Pod
metadata:
 name: "mysql"
 labels:
  name: "lbl-k8s-mysql"
spec:
 containers:
  - image: docker.io/jagadesh1982/testing-service
    name:  "testing"
    imagePullPolicy: IfNotPresent
    ports:
     - containerPort: 3306
    volumeMounts:
      - mountPath: /data-mount
        name: data
 volumes:
   - name: data
     emptyDir:
       medium: Memory

There is a new element that came in between which the medium which says this is a Memory. start the container and see what are present,
[root@manja17-I13330 kubenetes-config]# kubectl exec mysql -c testing -it bash
root@mysql:/# ls
bin   data-mount  etc lib media  opt root sbin sys                 tmp var
boot  dev      home lib64 mnt    proc run srv testing_service.py  usr
root@mysql:/# cd data-mount/
root@mysql:/data-mount# pwd
/data-mount
root@mysql:/data-mount# cd ..
root@mysql:/# exit
exit

hostPath - A host Path volume mounts a file or directory from the host node file system into your pod.  To attach a node local directory into the pod we can use,[root@manja17-I13330 kubenetes-config]# cat hostpath-mount.yml
apiVersion: v1
kind: Pod
metadata:
 name: testing-service
spec:
 containers:
  - image: docker.io/jagadesh1982/testing-service
    name:  testing
    imagePullPolicy: IfNotPresent
    ports:
     - containerPort: 80
    volumeMounts:
      - mountPath: /usr/share/nginx/html
        name: data
 volumes:
   - name: data
     hostPath:
       path: /tmp

In the above we are attaching the node /tmp location to the pod /usr/share/nginx/html location. So what ever the file available in the /tmp location of the node will be seen in the /usr/share/nginx/html directory.

More to Come , Happy learning :-)

No comments :

Post a Comment