In the world of Kubernetes, managing storage is crucial for running stateful applications effectively. AWS Elastic Block Store (EBS) is a popular choice for persistent storage in Kubernetes clusters on AWS. This guide will walk you through attaching an AWS EBS volume to a Kubernetes pod using YAML configurations. We’ll cover defining a Storage Class, setting up PersistentVolume (PV), establishing a PersistentVolumeClaim (PVC), and configuring a deployment to mount the volume.

Define a Storage Class Configuration (01-storage-class.yaml)

The first step in attaching an EBS volume to a Kubernetes pod is to define a Storage Class. A Storage Class allows administrators to describe their “classes” of storage. Here’s a sample 01-storage-class.yaml file:

apiVersion: storage.k8s.io/v1

kind: StorageClass

metadata:

  name: aws-ebs

provisioner: kubernetes.io/aws-ebs

parameters:

  type: gp2

  fsType: ext4

In this configuration:

  • provisioner specifies that AWS EBS will be used as the storage backend.
  • parameters define the type of EBS volume (gp2) and the file system type (ext4).

Set Up PersistentVolume with 02-pv.yaml

Next, you need to set up a PersistentVolume (PV). A PV is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes. Below is an example 02-pv.yaml file:

apiVersion: v1

kind: PersistentVolume

metadata:

  name: pv-aws-ebs

spec:

  capacity:

    storage: 10Gi

  volumeMode: Filesystem

  accessModes:

    – ReadWriteOnce

  persistentVolumeReclaimPolicy: Retain

  storageClassName: aws-ebs

  awsElasticBlockStore:

    volumeID: vol-0abcdef1234567890

    fsType: ext4

In this configuration:

  • volumeID should be replaced with the actual ID of your AWS EBS volume.
  • capacity specifies the size of the volume.
  • accessModes defines how the volume can be accessed. ReadWriteOnce means the volume can be mounted as read-write by a single node.
  • persistentVolumeReclaimPolicy determines what happens to the PV when its claim is deleted.

Establish a PersistentVolumeClaim using 03-pvc.yaml

Once the PV is set up, you must create a PersistentVolumeClaim (PVC). A PVC is a request for storage by a user. It is similar to a Pod in that Pods consume node resources and PVCs consume PV resources. Here’s a sample 03-pvc.yaml file:

apiVersion: v1

kind: PersistentVolumeClaim

metadata:

  name: pvc-aws-ebs

spec:

  accessModes:

    – ReadWriteOnce

  resources:

    requests:

      storage: 10Gi

  storageClassName: aws-ebs

In this configuration:

  • accessModes should match the access modes defined in your PV.
  • resources specify the amount of storage requested.
  • storageClassName ensures that the PVC uses the correct Storage Class.

Configure Deployment and Mount Volume in 04-deployment.yaml

Finally, you must configure your deployment to use the PVC and mount the volume. Here’s an example 04-deployment.yaml file:

apiVersion: apps/v1

kind: Deployment

metadata:

  name: ebs-deployment

spec:

  replicas: 1

  selector:

    matchLabels:

      app: ebs-app

  template:

    metadata:

      labels:

        app: ebs-app

    spec:

      containers:

        – name: ebs-container

          image: nginx

          volumeMounts:

            – mountPath: “/usr/share/nginx/html”

              name: ebs-volume

      volumes:

        – name: ebs-volume

          persistentVolumeClaim:

            claimName: pvc-aws-ebs

In this configuration:

  • The volumeMounts section mounts the PVC to a specific path inside the container (/usr/share/nginx/html).
  • The volumes section refers to the PVC created earlier.

Conclusion

Attaching an AWS EBS volume to a Kubernetes pod involves defining a Storage Class, setting up a PersistentVolume, establishing a PersistentVolumeClaim, and configuring a deployment to mount the volume. By following these steps, you can ensure your stateful applications have the persistent storage they need to function correctly.

References

Use Amazon EBS storage

Using Amazon EBS snapshots for persistent storage with your Amazon EKS cluster by leveraging add-ons