Skip to content

Instantly share code, notes, and snippets.

@valentin2105
Created November 3, 2017 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valentin2105/a9c89feac2b8848f52148048eb8ebadc to your computer and use it in GitHub Desktop.
Save valentin2105/a9c89feac2b8848f52148048eb8ebadc to your computer and use it in GitHub Desktop.
Kubernetes Auto GlusterFS PV creation
#!/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