Created
November 3, 2017 22:23
-
-
Save valentin2105/a9c89feac2b8848f52148048eb8ebadc to your computer and use it in GitHub Desktop.
Kubernetes Auto GlusterFS PV creation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
glusterName=glusterfs-cluster | |
glusterEP=10.240.0.10 | |
glusterNode01Path=gluster01:/storage-pool | |
glusterNode02Path=gluster02:/storage-pool | |
while true; do | |
check=$(kubectl 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 | |
echo "" | |
echo "Let's create a Gluster volume ($volume) ..." | |
size=$(kubectl -n $ns get pvc $name -o json | jq -r .spec.resources.requests.storage) | |
sizeGluster=$(echo $size |cut -d 'G' -f1) | |
gluster volume create $volume \ | |
replica 2 transport tcp \ | |
$glusterNode01Path/$volume \ | |
$glusterNode02Path/$volume | |
gluster volume start $volume | |
gluster volume quota $volume enable | |
gluster volume quota $volume limit-usage / "$sizeGluster"GB | |
cat <<EOF | kubectl 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 create -f - | |
apiVersion: v1 | |
kind: PersistentVolume | |
metadata: | |
name: $volume | |
spec: | |
capacity: | |
storage: $size | |
accessModes: | |
- ReadWriteMany | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment