#!/bin/bash # # GlusterFS and NFS Cluster Setup Script # Configures a 3-node replica GlusterFS volume and mounts it + NFS across all 5 Swarm nodes. # set -euo pipefail # Cluster Configuration ALL_NODES=("140.44.4.71" "140.44.4.72" "140.44.4.73" "140.44.4.74" "140.44.4.75") STORAGE_NODES=("140.44.4.71" "140.44.4.72" "140.44.4.73") # Nodes holding the actual data bricks USER="snarf" PASS="Katzir476!" # NFS Configuration NFS_SERVER="14.10.10.71" NFS_SHARE="/volume1/docker" # Based on your nginxproxy.yml context if ! command -v sshpass &> /dev/null; then echo "ERROR: 'sshpass' is required. Please install it first." exit 1 fi # Helper function to run sudo commands remotely via sshpass run_remote() { local node=$1 local cmd=$2 echo " [${node}] Running command..." local escaped_cmd="${cmd//\'/\'\\\'\'}" sshpass -p "$PASS" ssh -o StrictHostKeyChecking=no "$USER@$node" "echo '$PASS' | sudo -S bash -c '$escaped_cmd'" } echo "=== Phase 1: Installing Prerequisites & Creating Directories ===" for NODE in "${ALL_NODES[@]}"; do echo "-> Configuring Node $NODE" run_remote "$NODE" " apt-get update -y && \ apt-get install -y glusterfs-server glusterfs-client nfs-common && \ systemctl enable --now glusterd && \ mkdir -p /data/glusterfs/swarm/brick && \ mkdir -p /mnt/swarm_shared && \ mkdir -p /mnt/nfs_shares " done echo "" echo "=== Phase 2: Peering GlusterFS Storage Nodes ===" PRIMARY_NODE="${STORAGE_NODES[0]}" # Peer node 2 and 3 from node 1 for i in {1..2}; do PEER_NODE="${STORAGE_NODES[$i]}" echo "-> Peering $PRIMARY_NODE with $PEER_NODE" run_remote "$PRIMARY_NODE" "gluster peer probe $PEER_NODE" done # Give peering a few seconds to stabilize sleep 5 echo "" echo "=== Phase 3: Creating and Starting GlusterFS Volume ===" echo "-> Creating 'replica 3' volume 'swarm_vols'..." run_remote "$PRIMARY_NODE" " gluster volume create swarm_vols replica 3 \ ${STORAGE_NODES[0]}:/data/glusterfs/swarm/brick \ ${STORAGE_NODES[1]}:/data/glusterfs/swarm/brick \ ${STORAGE_NODES[2]}:/data/glusterfs/swarm/brick \ force || echo 'Volume might already exist.' " echo "-> Starting volume 'swarm_vols'..." run_remote "$PRIMARY_NODE" "gluster volume start swarm_vols || echo 'Volume already started.'" echo "" echo "=== Phase 4: Mounting GlusterFS and NFS on ALL Nodes ===" for NODE in "${ALL_NODES[@]}"; do echo "-> Mounting file systems on $NODE" run_remote "$NODE" " # Remove old fstab entries to prevent duplicates if script is re-run sed -i '/swarm_vols/d' /etc/fstab sed -i '/nfs_shares/d' /etc/fstab # Add to fstab # Using primary node IP with backup servers instead of localhost (required for client-only nodes) echo '140.44.4.71:/swarm_vols /mnt/swarm_shared glusterfs defaults,_netdev,backup-volfile-servers=140.44.4.72:140.44.4.73 0 0' >> /etc/fstab echo '$NFS_SERVER:$NFS_SHARE /mnt/nfs_shares nfs defaults,nfsvers=4,_netdev 0 0' >> /etc/fstab # Reload systemd so it recognizes the fstab changes systemctl daemon-reload # Mount them individually so one failure doesn't halt the other mount /mnt/swarm_shared mount /mnt/nfs_shares || echo 'NFS Mount Failed, continuing...' " done echo "=== Storage Cluster Setup Complete ==="