Skip to content

EKS Storage - Storage Classes, Persistent Volume Claims

Step-01: Introduction

  • We are going to create a MySQL Database with persistence storage using AWS EBS Volumes
Kubernetes Object YAML File
Storage Class 01-storage-class.yml
Persistent Volume Claim 02-persistent-volume-claim.yml
Config Map 03-UserManagement-ConfigMap.yml
Deployment, Environment Variables, Volumes, VolumeMounts 04-mysql-deployment.yml
ClusterIP Service 05-mysql-clusterip-service.yml

Kubernetes Manifests

#01-storage-class.yml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata: 
  name: ebs-sc
provisioner: ebs.csi.aws.com
volumeBindingMode: WaitForFirstConsumer 
#02-persistent-volume-claim.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: ebs-mysql-pv-claim
spec: 
  accessModes:
    - ReadWriteOnce
  storageClassName: ebs-sc
  resources: 
    requests:
      storage: 4Gi
#03-UserManagement-ConfigMap.yml
apiVersion: v1
kind: ConfigMap
metadata:
  name: usermanagement-dbcreation-script
data: 
  mysql_usermgmt.sql: |-
    DROP DATABASE IF EXISTS usermgmt;
    CREATE DATABASE usermgmt; 
#04-mysql-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec: 
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate 
  template: 
    metadata: 
      labels: 
        app: mysql
    spec: 
      containers:
        - name: mysql
          image: mysql:5.6
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: dbpassword11
          ports:
            - containerPort: 3306
              name: mysql    
          volumeMounts:
            - name: mysql-persistent-storage
              mountPath: /var/lib/mysql    
            - name: usermanagement-dbcreation-script
              mountPath: /docker-entrypoint-initdb.d #https://hub.docker.com/_/mysql Refer Initializing a fresh instance                                            
      volumes: 
        - name: mysql-persistent-storage
          persistentVolumeClaim:
            claimName: ebs-mysql-pv-claim
        - name: usermanagement-dbcreation-script
          configMap:
            name: usermanagement-dbcreation-script
#05-mysql-clusterip-service.yml
apiVersion: v1
kind: Service
metadata: 
  name: mysql
spec:
  selector:
    app: mysql 
  ports: 
    - port: 3306  
  clusterIP: None # This means we are going to use Pod IP    

Step-02: Create following Kubernetes manifests

Create Storage Class manifest

  • https://kubernetes.io/docs/concepts/storage/storage-classes/#volume-binding-mode
  • Important Note: WaitForFirstConsumer mode will delay the volume binding and provisioning of a PersistentVolume until a Pod using the PersistentVolumeClaim is created.

Create Persistent Volume Claims manifest

# Create Storage Class & PVC
kubectl apply -f kube-manifests/

# List Storage Classes
kubectl get sc

# List PVC
kubectl get pvc 

# List PV
kubectl get pv

Create ConfigMap manifest

  • We are going to create a usermgmt database schema during the mysql pod creation time which we will leverage when we deploy User Management Microservice.

Create MySQL Deployment manifest

  • Environment Variables
  • Volumes
  • Volume Mounts

Create MySQL ClusterIP Service manifest

  • At any point of time we are going to have only one mysql pod in this design so ClusterIP: None will use the Pod IP Address instead of creating or allocating a separate IP for MySQL Cluster IP service.

Step-03: Create MySQL Database with all above manifests

# Create MySQL Database
kubectl apply -f kube-manifests/

# List Storage Classes
kubectl get sc

# List PVC
kubectl get pvc 

# List PV
kubectl get pv

# List pods
kubectl get pods 

# List pods based on  label name
kubectl get pods -l app=mysql

Step-04: Connect to MySQL Database

# Connect to MYSQL Database
kubectl run -it --rm --image=mysql:5.6 --restart=Never mysql-client -- mysql -h mysql -pdbpassword11

# Verify usermgmt schema got created which we provided in ConfigMap
mysql> show schemas;

AWS EKS - Elastic Kubernetes Service - Masterclass

Image

Step-05: References

  • We need to discuss references exclusively here.
  • These will help you in writing effective templates based on need in your environments.
  • Few features are still in alpha stage as on today (Example:Resizing), but once they reach beta you can start leveraging those templates and make your trials.
  • EBS CSI Driver: https://github.com/kubernetes-sigs/aws-ebs-csi-driver
  • EBS CSI Driver Dynamic Provisioning: https://github.com/kubernetes-sigs/aws-ebs-csi-driver/tree/master/examples/kubernetes/dynamic-provisioning
  • EBS CSI Driver - Other Examples like Resizing, Snapshot etc: https://github.com/kubernetes-sigs/aws-ebs-csi-driver/tree/master/examples/kubernetes
  • k8s API Reference Doc: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#storageclass-v1-storage-k8s-io