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 | 1347ea6 | 2018-02-25 01:07:59 -0600 | [diff] [blame] | 18 | # mode ReadWriteOnce|ReadOnlyMany|ReadWriteMany |
| 19 | # Access Mode used by NFS normally uses ReadWriteMany |
| 20 | # Default: ReadWriteMany |
| 21 | # name The name of the PV and PVC that will be used by the Kubernetes |
| 22 | # system to refer to PV/PVC |
| 23 | # Default: "placeholder" |
| 24 | # nfs_ip Server IP for NFS server that will be used |
| 25 | # Default: "NFS-Server" |
| 26 | # nfs_path Path of the directory that will be mounted from NFS server |
| 27 | # Default: "/san/dir" |
| 28 | # ns Namespace under which to create the mounts on the cluster |
| 29 | # Default: "openbmc" |
| 30 | # reclaim recycle|delete|retain |
| 31 | # The policy, defines what occurs when claim on PV is released, can |
| 32 | # be either: recycle, delete, or retain. |
| 33 | # Default: "Retain" |
| 34 | # size The size of the volume, numeric value followed by Gi or Mi |
| 35 | # Default: "10Gi" |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 36 | # |
| 37 | # Note: Kubernetes Systems permissions vary by implementation |
| 38 | # some will require permissions to create PV's or PVC's |
| 39 | # |
| 40 | ############################################################################### |
| 41 | |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 42 | mode=${mode:-ReadWriteMany} |
Alanny Lopez | 1347ea6 | 2018-02-25 01:07:59 -0600 | [diff] [blame] | 43 | name=${name:-placeholder} |
| 44 | nfs_ip=${nfs_ip:-NFS-Server} |
| 45 | nfs_path=${nfs_path:-/san/dir} |
| 46 | ns=${ns:-openbmc} |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 47 | reclaim=${reclaim:-Retain} |
Alanny Lopez | 1347ea6 | 2018-02-25 01:07:59 -0600 | [diff] [blame] | 48 | size=${size:-10Gi} |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 49 | |
| 50 | # Generate the PV |
| 51 | pv=$(cat << EOF |
| 52 | apiVersion: v1 |
| 53 | kind: PersistentVolume |
| 54 | metadata: |
| 55 | labels: |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 56 | app: ${name} |
| 57 | name: ${name} |
| 58 | namespace: ${ns} |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 59 | spec: |
| 60 | accessModes: |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 61 | - ${mode} |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 62 | capacity: |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 63 | storage: ${size} |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 64 | nfs: |
Alanny Lopez | 1347ea6 | 2018-02-25 01:07:59 -0600 | [diff] [blame] | 65 | path: ${nfs_path} |
| 66 | server: ${nfs_ip} |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 67 | persistentVolumeReclaimPolicy: ${reclaim} |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 68 | EOF |
| 69 | ) |
| 70 | |
| 71 | # create the volume |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 72 | if [ -z $(kubectl get pv --namespace=${ns} | grep '^'${name}' ' | cut -d " " -f1) ];then |
| 73 | echo "Creating Persistent Volume ${name}" |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 74 | kubectl create -f - <<< "${pv}" |
| 75 | else |
| 76 | echo "Persistent Volume already Exists" |
| 77 | fi |
| 78 | |
| 79 | |
| 80 | # Generate the PVC |
| 81 | pvc=$(cat << EOF |
| 82 | apiVersion: v1 |
| 83 | kind: PersistentVolumeClaim |
| 84 | metadata: |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 85 | name: ${name} |
| 86 | namespace: ${ns} |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 87 | spec: |
| 88 | accessModes: |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 89 | - ${mode} |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 90 | resources: |
| 91 | requests: |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 92 | storage: ${size} |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 93 | selector: |
| 94 | matchLabels: |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 95 | app: ${name} |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 96 | EOF |
| 97 | ) |
| 98 | |
| 99 | # create PVC's to bind the PV's |
Alanny Lopez | aef0a49 | 2017-12-28 16:03:14 -0600 | [diff] [blame] | 100 | if [ -z $(kubectl get pvc --namespace=${ns} | grep '^'${name}' ' | cut -d " " -f1) ];then |
| 101 | echo "Creating Persistent Volume Claim ${name}" |
Alanny Lopez | 7b73093 | 2017-04-24 13:12:20 -0500 | [diff] [blame] | 102 | kubectl create -f - <<< "${pvc}" |
| 103 | else |
| 104 | echo "Persistent volume claim already exists." |
| 105 | fi |