How to use AWS EBS as volume in EKS

black laptop computer turned on showing computer codes
Reading Time: 3 minutes

From the time kubernetes is in the world it has become easy to deploy containerized application.with volume attached.We all know that k8s gives the dynamic provision approach.

Firstly Persistent volume is needed in case of stateful application. Secondly an application which persist its state is called stateful. A database is a stateful one , the data is to be stored inspite of the stopped container.

Why do we need to manage storage ?

We need to manage storage because it allows to maintain multiple forms of data in k8s.

Containers use the principle of immutability. In addition to this means that when containers are destroyed , including all data , created during the lifetime of the containers. Immutability is not always appropriate, however. For example, applications that need to share information or maintain state cannot lose this information. In these cases, containers must have a location to permanently store information, which should outlive the lifespan of an individual container.

How K8S provides dynamic provisioning of volumes?

In kubernetes it is easy to dynamically provision persistent volume claim.

A Persistent volume is a piece of storage , given dynamically by the admin. Or by using any storage class. It is considered as any resource.

A persistent volume claim is the claim of the above resource that can be claimed. So kubernetes gives these option to provision volume.

In other words each persistent volume provides storage resources to pods . In the same way that nodes provide compute.Each PV uses a plug into interface with different type of backend storage with support for EBS , Azure EBS etc.

Prerequisite for setting up the provisioning

  • The provisioning is set up on AWS-EKS.
  • To perform the set up please use eks as the k8s service.

How to set up dynamic provisioning ?

To set up the dynamic provisioning we will use k8s configuration file. The yaml config will be used to create the storage class. Below is the file of storage class.

kind: StorageClass

apiVersion: storage.k8s.io/v1

metadata:

 name: aws-ebs-k8

 labels:

 deploy: aws-ebs-k8

provisioner: kubernetes.io/aws-ebs

volumeBindingMode: Immediate

parameters:

 type: gp2 # This configures SSDs (recommended).

allowVolumeExpansion: true

reclaimPolicy: Retain

allowedTopologies:

- matchLabelExpressions:

 — key: failure-domain.beta.kubernetes.io/zone

 values:

 — ap-south-1a

Explanation of above yaml file.

  • Kind: -> defines the type of k8s object created
  • The metadata represents the name of the storage class that we want to make.
  • The provisioner represents the type of provision we need in the config.
  • Volume binding immediate shows that as soon as the claim will be created the binding will be done.
  • key and value is declared as the domain needs the zone with it. .

To create the storage class in the cluster run the below command

cd /directory

kubectl create -f filename.yml -n namesapce

example : - kubectl create -f storage.yml -n practise

Output

WIth the above command the storage class is created in the confined namespace. After that to get the storage class put the below command

kubectl get sc -n ns

exmaple :- kubectl get sc -n practise

Moreover Now the storage class is created with provisioning of aws ebs in the region ap-south-1. The thing is that now this storage class be will used to claim in the volume claim template.

Applying this configuration would create storage class claim.

How to create persistent volume claim.

Next we will create a persistent volume claim which will use the above storage class. The storage class is configured above in the configuration. Secondly to create the volume claim template use the below configuration file .

The configuration file below shows the persistent volume claims

apiVersion: v1

kind: PersistentVolumeClaim

metadata:

 labels:

   deploy: pvc-k8

 name: pvc-k8

spec:

 accessModes:

 - ReadWriteOnce

 resources:

  requests:

   storage: 20Gi

 storageClassName: aws-ebs-k8

Now the above file shows the configuration to use the aws ebs class storage.

Explanation

  • The object is declared in kind.
  • Secondly storage class name is as we made the storage class.
  • Lastly ReadWriteOnce is the access mode that the pv is claiming.

Lastly to apply the above configuration run the below commad.

kubectl create -f Peristent.yml -n practise

To get the pvc run the below command.

kubectl get pvc -n practise

Output

Conclusion

So in the above blog we saw how to use AWS EBS as the storage.However the EBS is used as a provisioner in the k8s. Apart from this you can use many more storage classes .

In other words this gives persistent storage and helps to create a block for the same.

Reference

https://kubernetes.io/docs/concepts/storage/storage-classes/

Written by 

Rishivant is a enthusiastic devops learner at Knoldus. He believes in going 1% up everyday and showcases his learning in his work.