Skip to content

Instantly share code, notes, and snippets.

@valentin2105
Created November 3, 2017 22:23
Show Gist options
  • Select an option

  • Save valentin2105/a9c89feac2b8848f52148048eb8ebadc to your computer and use it in GitHub Desktop.

Select an option

Save valentin2105/a9c89feac2b8848f52148048eb8ebadc to your computer and use it in GitHub Desktop.

Revisions

  1. Valentin Ouvrard created this gist Nov 3, 2017.
    85 changes: 85 additions & 0 deletions volume_provision.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,85 @@
    #!/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