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 |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 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 |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 23 | # system to refer to PV/PVC |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 24 | # mode = ReadWriteOnce|ReadOnlyMany|ReadWriteMany |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 25 | # Access Mode used by NFS normally uses ReadWriteMany |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 26 | # reclaim = recycle|delete|retain |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 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 | |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 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} |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 42 | |
| 43 | # Generate the PV |
| 44 | pv=$(cat << EOF |
| 45 | apiVersion: v1 |
| 46 | kind: PersistentVolume |
| 47 | metadata: |
| 48 | labels: |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 49 | app: ${name} |
| 50 | name: ${name} |
| 51 | namespace: ${ns} |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 52 | spec: |
| 53 | accessModes: |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 54 | - ${mode} |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 55 | capacity: |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 56 | storage: ${size} |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 57 | nfs: |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 58 | path: ${nfspath} |
| 59 | server: ${nfsip} |
| 60 | persistentVolumeReclaimPolicy: ${reclaim} |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 61 | EOF |
| 62 | ) |
| 63 | |
| 64 | # create the volume |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 65 | if [ -z $(kubectl get pv --namespace=${ns} | grep '^'${name}' ' | cut -d " " -f1) ];then |
| 66 | echo "Creating Persistent Volume ${name}" |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 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: |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 78 | name: ${name} |
| 79 | namespace: ${ns} |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 80 | spec: |
| 81 | accessModes: |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 82 | - ${mode} |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 83 | resources: |
| 84 | requests: |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 85 | storage: ${size} |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 86 | selector: |
| 87 | matchLabels: |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 88 | app: ${name} |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 89 | EOF |
| 90 | ) |
| 91 | |
| 92 | # create PVC's to bind the PV's |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 93 | if [ -z $(kubectl get pvc --namespace=${ns} | grep '^'${name}' ' | cut -d " " -f1) ];then |
| 94 | echo "Creating Persistent Volume Claim ${name}" |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 95 | kubectl create -f - <<< "${pvc}" |
| 96 | else |
| 97 | echo "Persistent volume claim already exists." |
| 98 | fi |