Creating a Storage Class in Amazon Web Services

Introduction

You can set up persistent storage in Amazon EKS using either of the following options:

This page includes the necessary steps for each of the two options listed above.

Prerequisites

Before you complete the steps in either section, you must:

  • Make sure you're logged in to Amazon Management Console

  • Set AWS Identity and Access Management (IAM) permissions for creating and attaching a policy to the Amazon EKS worker node role CSI Driver Role.

  • Create your Amazon EKS cluster and join your worker nodes to the cluster.

  • For Amazon EBS CSI add-on, make sure you have an existing cluster that's version 1.18 or later. To see the required platform version, run the following command.

    aws eks describe-addon-versions --addon-name aws-ebs-csi-driver
  • Have an existing IAM OpenID Connect (OIDC) provider for your cluster. To determine if you already have a cluster, or to create one, see Create an IAM OIDC provider for your cluster.

Steps to create Disk and Network Volumes

The main focus of the next steps will be Deploying and testing the Amazon EBS CSI driver.

Adding the Amazon EBS CSI add-on

For this step, you need to follow the Amazon instructions presented in the article linked here.

Setting the proper context

Starting here, you will work in the terminal. Make sure you're connected to the cluster and that the cluster is the current context. Use the command kubectl config --help to obtain the necessary information.

Creating the storage class spec.yaml file

Create a spec.yaml file with the following contents:

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: bns-disk-sc
provisioner: ebs.csi.aws.com
volumeBindingMode: WaitForFirstConsumer

Applying the spec.yaml file

Apply the file using the command below. Line 2 contains the expected Output.

kubectl apply -f spec.yaml
// OUTPUT
storageclass.storage.k8s.io/bns-disk-sc created

Verifying the presence of the bns-disk-sc storage class

Use the command below to verify the presence of the storage class:

kubectl get sc
// OUTPUT
NAME            PROVISIONER             RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
bns-disk-sc     ebs.csi.aws.com         Delete          WaitForFirstConsumer   false                  2m5s
gp2 (default)   kubernetes.io/aws-ebs   Delete          WaitForFirstConsumer   false                  50m

Testing the EBS CSI driver

1. Create two files to test the CSI driver:

  • Create a claim.yaml file with the contents below. Later, the claim.yaml file will generate the test PVC.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: ebs-claim
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: bns-disk-sc
  resources:
    requests:
      storage: 4Gi
  • Create a pod.yaml file with the contents below. Later, the pod.yaml file will generate the test Pod.

apiVersion: v1
kind: Pod
metadata:
  name: app
spec:
  containers:
  - name: app
    image: centos
    command: ["/bin/sh"]
    args: ["-c", "while true; do echo $(date -u) >> /data/out.txt; sleep 5; done"]
    volumeMounts:
    - name: persistent-storage
      mountPath: /data
  volumes:
  - name: persistent-storage
    persistentVolumeClaim:
      claimName: ebs-claim

2. Apply claim.yaml:

kubectl apply -f claim.yaml
// OUTPUT
persistentvolumeclaim/ebs-claim created

3. Apply pod.yaml:

kubectl apply -f pod.yaml
// OUTPUT
pod/app created

4. Wait until the app pod reaches the status Running. To check that the pod reached the Running status, perform the following command:

kubectl get pods -w
// OUTPUT
NAME   READY   STATUS              RESTARTS   AGE
app    0/1     Pending             0          0s
app    0/1     Pending             0          4s
app    0/1     ContainerCreating   0          5s
app    1/1     Running             0          17s

5. Check for the presence of a persistent volume that has the following properties:

  • STORAGECLASS set to bns-disk-sc

  • CLAIM set to default/ebs-claim

kubectl get pv -o wide
// OUTPUT
NAME                                     ...  CLAIM               STORAGECLASS   REASON   AGE    VOLUMEMODE
pvc-e87327ce-1111-2222-3333-9651b92bce57 ...  default/ebs-claim   bns-disk-sc             2m2s   Filesystem

6. Verify that the app pod is writing data to the volume:

kubectl exec -it app -- cat /data/out.txt
// OUTPUT
Wed Jul 20 08:28:52 UTC 2022
Wed Jul 20 08:28:57 UTC 2022
Wed Jul 20 08:29:02 UTC 2022

7. If the your results are similar with the output displayed above, then you've completed the process successfully and you can delete the test resources.

  • Start by deleting the Pod:

kubectl delete -f pod.yaml
// OUTPUT
pod "app" deleted
  • Delete the PVC. This will also cause the PV to be deleted:

kubectl delete -f claim.yaml
// OUTPUT
persistentvolumeclaim "ebs-claim" deleted
  • Check if the PV displayed at step 5 is no longer present:

kubectl get pv pvc-e87327ce-1111-2222-3333-9651b92bce57
// OUTPUT
Error from server (NotFound): persistentvolumes "pvc-e87327ce-1111-2222-3333-9651b92bce57" not found

Last updated

Was this helpful?