K8s: dynamic provisioning of persistent volumes on AWS

Ivan Katliarchuk
2 min readJan 2, 2019

--

A PersistentVolume (PV) is a piece of storage in the cluster that has been provisioned by an administrator. It is a resource in the cluster just like a node is a cluster resource.

A PersistentVolumeClaim (PVC) is a request for storage by a user. It is similar to a pod. Pods consume node resources and PVCs consume PV resources.

Prerequisites

In this post, we are focusing on AWS EKS setup, in particular how to dynamically provision persisted volumes for applications and effectively provide a storage as a service.

Create PersistentVolume

All that we need is a manifest

file: aws_volume_default.yml

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: generic
provisioner: kubernetes.io/aws-ebs
parameters:
type: gp2
zones: eu-west-1a, eu-west-1b
iopsPerGB: "10"
fsType: ext4

And a command to provision a given volume

$ kubectl create -f aws_volume_default.yml>> storageclass.storage.k8s.io/generic created$ kubectl get sc> NAME                PROVISIONER             AGE
> generic (default) kubernetes.io/aws-ebs 1m

Create PersistentVolumeClaim

This is how volume claim may look like, it can be a part of pod deployment manifest

file: volume_claim.yml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: slow
labels:
app: nginx
spec:
storageClassName: generic
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi

Now application developer is able to claim a volume.

$ kubectl create -f volume_claim.yml
> persistentvolumeclaim/slow created

Validate that everything is set correctly.

$ kubectl get pvc> NAME   STATUS VOLUME  CAPACITY ACCESS STORAGECLASS   AGE
> slow Bound pvc-* 3Gi RWO generic 55s

Deploy a custom pod

Volume is provisioned and now we can use it. Let’s deploy a custompod with volume attached to it.

file: custompod.yml

kind: Pod
apiVersion: v1
metadata:
name: custompod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- mountPath: "/var/www/html"
name: external
volumes:
- name: external
persistentVolumeClaim:
claimName: slow

Deploy.

$ kubectl create -f custompod.yml> pod/custompod created

And test access

$ kubectl exec -it custompod -- /bin/bash -c "ls -la /var/www"> html

In case something goes wrong

kubectl get events

Or just delete deployments and start again

Let’s keep in touch

I’d love to hear your suggestions and ideas for K8s training materials. Thanks for reading!

--

--