-
Notifications
You must be signed in to change notification settings - Fork 1
/
gluster-pv.sh
100 lines (86 loc) · 2.79 KB
/
gluster-pv.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/bash
glusterName=glusterfs-cluster
glusterEP=192.168.1.10
glusterNodesNumber=2
glusterNode01Path=192.168.1.10:/gluster-pool
glusterNode02Path=192.168.1.11:/gluster-pool
kubeConfigPath=/root/.kube/config
while true; do
check=$(kubectl --kubeconfig $kubeConfigPath get pvc --all-namespaces --no-headers |grep Pending | head -1)
if [[ "$check" != "" ]]; then
ns=$(echo "$check" |awk '{print $1}')
name=$(echo "$check" |awk '{print $2}')
checkExist=$(gluster volume list |grep "$name")
volume="$name"
if [[ "$checkExist" == "" ]]; then
# Let's fetch PVC infos
size=$(kubectl --kubeconfig $kubeConfigPath -n "$ns" get pvc "$name" -o json | jq -r .spec.resources.requests.storage)
label=$(kubectl --kubeconfig $kubeConfigPath -n "$ns" get pvc "$name" -o json | jq -r .metadata.labels.identifier)
accessMode=$(kubectl --kubeconfig $kubeConfigPath -n "$ns" get pvc "$name" -o json | jq -r '.spec.accessModes[0]')
sizeGluster=$(echo "$size" |cut -d 'G' -f1)
# Let's create the volume
echo "Let's create a Gluster volume ($volume) ..."
if ! gluster volume create $volume replica $glusterNodesNumber transport tcp $glusterNode01Path/$volume $glusterNode02Path/$volume
then
echo "Volume creation error !"
exit 1
fi
gluster volume start "$volume"
# Put Quota on Gluster volume
gluster volume quota "$volume" enable
gluster volume quota "$volume" limit-usage / "$sizeGluster"GB
# Let's create Kubernetes SVC & PV
cat <<EOF | kubectl --kubeconfig /root/.kube/config apply -f -
apiVersion: v1
kind: Service
metadata:
name: $glusterName
namespace: $ns
spec:
clusterIP: None
ports:
- port: 1
protocol: TCP
targetPort: 1
sessionAffinity: None
---
apiVersion: v1
kind: Endpoints
metadata:
name: $glusterName
namespace: $ns
subsets:
- addresses:
- ip: $glusterEP
ports:
- port: 1
protocol: TCP
EOF
cat <<EOF | kubectl --kubeconfig /root/.kube/config create -f -
apiVersion: v1
kind: PersistentVolume
metadata:
name: $volume
labels:
identifier: $label
spec:
capacity:
storage: $size
accessModes:
- $accessMode
claimRef:
namespace: $ns
name: $volume
glusterfs:
path: $volume
endpoints: $glusterName
readOnly: false
EOF
echo "$volume of $size is created. "
echo ""
else
echo "$volume already exist.... "
fi
fi
sleep 5
done