Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame^] | 1 | #!/bin/bash |
| 2 | ############################################################################### |
| 3 | # |
| 4 | # This script creates an NFS Persistent Volumes(PV) and also claims that PV |
| 5 | # with a PVC of the same size. |
| 6 | # Note: PVs can be claimed by one PVC at a time |
| 7 | # |
| 8 | ############################################################################### |
| 9 | # |
| 10 | # Requirements: |
| 11 | # - NFS server with directory to use as path for mount |
| 12 | # - Access to an existing Kubernetes Cluster |
| 13 | # - Kubectl installed and configured on machine running script |
| 14 | # |
| 15 | ############################################################################### |
| 16 | # |
| 17 | # The script expects a few variables which are needed to define PV's and PVC's |
| 18 | # NS = Namespace under which to create the mounts on the cluster |
| 19 | # NFSIP = Server IP for NFS server that will be used |
| 20 | # NFSPATH = Path of the directory that will be mounted from NFS server |
| 21 | # SIZE = The size of the volume, numeric value followed by Gi or Mi |
| 22 | # NAME = The name of the PV and PVC that will be used by the Kubernetes |
| 23 | # system to refer to PV/PVC |
| 24 | # MODE = ReadWriteOnce|ReadOnlyMany|ReadWriteMany |
| 25 | # Access Mode used by NFS normally uses ReadWriteMany |
| 26 | # RECLAIM = recycle|delete|retain |
| 27 | # The policy, defines what occurs when claim on PV is released, can |
| 28 | # be either: recycle, delete, or retain. |
| 29 | # |
| 30 | # Note: Kubernetes Systems permissions vary by implementation |
| 31 | # some will require permissions to create PV's or PVC's |
| 32 | # |
| 33 | ############################################################################### |
| 34 | |
| 35 | NS=${NS:-openbmc} |
| 36 | NFSIP=${NFSIP:-NFS-Server} |
| 37 | NFSPATH=${NFSPATH:-/san/dir} |
| 38 | SIZE=${SIZE:-10Gi} |
| 39 | NAME=${NAME:-placeholder} |
| 40 | MODE=${MODE:-ReadWriteMany} |
| 41 | RECLAIM=${RECLAIM:-Retain} |
| 42 | |
| 43 | # Generate the PV |
| 44 | pv=$(cat << EOF |
| 45 | apiVersion: v1 |
| 46 | kind: PersistentVolume |
| 47 | metadata: |
| 48 | labels: |
| 49 | app: ${NS} |
| 50 | name: ${NAME} |
| 51 | namespace: ${NS} |
| 52 | spec: |
| 53 | accessModes: |
| 54 | - ${MODE} |
| 55 | capacity: |
| 56 | storage: ${SIZE} |
| 57 | nfs: |
| 58 | path: ${NFSPATH} |
| 59 | server: ${NFSIP} |
| 60 | persistentVolumeReclaimPolicy: ${RECLAIM} |
| 61 | EOF |
| 62 | ) |
| 63 | |
| 64 | # create the volume |
| 65 | if [ -z $(kubectl get pv --namespace=${NS} | grep '^'${NAME}' ' | cut -d " " -f1) ];then |
| 66 | echo "Creating Persistent Volume ${NAME}" |
| 67 | kubectl create -f - <<< "${pv}" |
| 68 | else |
| 69 | echo "Persistent Volume already Exists" |
| 70 | fi |
| 71 | |
| 72 | |
| 73 | # Generate the PVC |
| 74 | pvc=$(cat << EOF |
| 75 | apiVersion: v1 |
| 76 | kind: PersistentVolumeClaim |
| 77 | metadata: |
| 78 | name: ${NAME} |
| 79 | namespace: ${NS} |
| 80 | spec: |
| 81 | accessModes: |
| 82 | - ${MODE} |
| 83 | resources: |
| 84 | requests: |
| 85 | storage: ${SIZE} |
| 86 | selector: |
| 87 | matchLabels: |
| 88 | app: ${NS} |
| 89 | EOF |
| 90 | ) |
| 91 | |
| 92 | # create PVC's to bind the PV's |
| 93 | if [ -z $(kubectl get pvc --namespace=${NS} | grep '^'${NAME}' ' | cut -d " " -f1) ];then |
| 94 | echo "Creating Persistent Volume Claim ${NAME}" |
| 95 | kubectl create -f - <<< "${pvc}" |
| 96 | else |
| 97 | echo "Persistent volume claim already exists." |
| 98 | fi |