Script for creating EBS persistent volumes in OpenShift/Kubernetes

If you aren't using the automated dynamic volume provisioning (which you should!). Here is a short bash script to help you automatically create both the EBS volume and Kubernetes persistent volume:

#!/bin/bash

if [ $# -ne 2 ]; then  
    echo "Usage: sh create-volumes.sh SIZE COUNT"
    exit
fi

for i in `seq 1 $2`; do  
  size=$1
  vol=$(ec2-create-volume --size $size --region ap-southeast-2 --availability-zone ap-southeast-2a --type gp2 --encrypted | awk '{print $2}')
  size+="Gi"

  echo "
  apiVersion: v1
  kind: PersistentVolume
  metadata:
    labels:
      failure-domain.beta.kubernetes.io/region: ap-southeast-2
      failure-domain.beta.kubernetes.io/zone: ap-southeast-2a
    name: pv-$vol
  spec:
    capacity:
      storage: $size
    accessModes:
      - ReadWriteOnce
    awsElasticBlockStore:
      fsType: ext4
      volumeID: aws://ap-southeast-2a/$vol
    persistentVolumeReclaimPolicy: Delete" | oc create -f -
done  
comments powered by Disqus