Persistent volume mount in Elastic Kubernetes service

Persistent storage is necessary for storing application data. We have seen how we can mount a local volume in docker to persist any data. If you haven’t read that, you can just go through my earlier article.

When using an orchestrator, we will need to do few more things than just to mount a persistent store to a Pod(container). In this article, we will explore the AWS Elastic Kubernetes service to create a cluster and then mount a persistent Elastic blob store so that any change that we do in our app shall persist.

To get started, you will need to set aws-cli and Terraform on your machine. In EKS, it’s a bit tedious to create a cluster using aws-cli commands manually and therefore we will use terraform to get this prerequisite done.

You can follow the steps on this link from Hashicorp, which guides you on how to create and configure an EKS cluster. Once your cluster is up and running, we will execute few kubectl commands to mount a persistent Elastic Blob Store.

The usual process to claim a persistent store is to first create a volume in AWS followed by a storage-class then create a persistent volume(PV) and finally a persistent volume claim(PVC) that will be used by a Pod. Alternatively, we can just skip the creation of persistent volume and go ahead and create a persistent volume claim kind object which will automatically create a corresponding PV for you

Note that an EBS Volume can only be used by a single Pod at the same time. Thus, the access mode of your PVC can only be ReadWriteOnce.

So, let’s first create a volume through aws-cli or console

aws ec2 create-volume --region <aws-region> --availability-zone <aws-az> --size <size-in-Gb> --volume-type <type>
AWS console EC2 volume available

Then let’s go ahead and create a storage class if you don’t have a default one in your cluster.

storage-class.yaml

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: standard
provisioner: kubernetes.io/aws-ebs
parameters:
type: gp2
reclaimPolicy: Retain
mountOptions:
- debug
volumeBindingMode: Immediate

Now let’s create a persitent volume claim(PVC)

persistent-volume-claim.yaml

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: myclaim2
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: standard

We can create these objects simply by using kubectl create command

kubectl create -f <file-name>.yaml

Now let’s create a workload or a pod that is going to utilize this volume and a load-balancer service to expose our running container.

pod.yaml

kind: Pod
apiVersion: v1
metadata:
name: nginx-pod
labels:
app: mynginx
spec:
containers:
- name: myfrontend
image: nginx
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: mypd
volumes:
- name: mypd
persistentVolumeClaim:
claimName: myclaim2

load-balancer.yaml

apiVersion: v1
kind: Service
metadata:
name: lb-service
labels:
run: lb-service
spec:
selector:
app: mynginx
ports:
- port: 80
type: LoadBalancer

We can just validate once all objects have been created using kubectl get command.

Kubectl get objects

If volume is not in bound state, we can check the event logs to get an idea of any problem.

kubectl get events
volume attached event

We can also verify that the volume state has now changed to ‘in-use’ in aws console.

AWS console EC2 volume in-use

Now, just to make sure that we are able to persist any data, we will go ahead and create a index.html file under the mounted path of the pod and validate that our site is alive.

index.html
nginx home page in browser

Now, that we have nginx homepage that we want to persist, we will kill this pod and launch a new pod mounting the same volume path where we have our data of interest and validate that it exists and can be utilized.

So, let’s delete the old pod named nginx-pod and create a new pod named new-nginx-pod to validate.

New pod with existing volume mounted
index.html created earlier has persisted in volume and now used by new pod

This is how we can persist any data in our kubernetes cluster. Apart from using EBS, there is Elastic File system service(EFS) for the same purpose with some differences.

I hope you found this article insightful, Thanks for reading! 😃

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store