- 6-node MariaDB cluster with GTID replication - MaxScale 24.02 proxy with automatic failover - Flask dashboard with SSE transaction monitor - Per-server toggle controls + mode selector - Systemd services for auto-start on boot - One-command deploy.sh
93 lines
4.7 KiB
Bash
93 lines
4.7 KiB
Bash
#!/usr/bin/env bash
|
|
# ── MariaDB HA Demo — One-Command Deploy ────────────────
|
|
# Installs dependencies, deploys MariaDB cluster, starts the dashboard.
|
|
# Run as: bash deploy.sh
|
|
set -euo pipefail
|
|
|
|
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
readonly DEMO_HOME="${SCRIPT_DIR}"
|
|
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m'
|
|
|
|
log() { echo -e "${GREEN}[✓]${NC} $1"; }
|
|
warn() { echo -e "${RED}[!]${NC} $1"; }
|
|
info() { echo -e "${BLUE}[i]${NC} $1"; }
|
|
|
|
# ── Check prerequisites ──────────────────────────────────
|
|
command -v docker >/dev/null 2>&1 || {
|
|
warn "Docker not found. Installing..."
|
|
sudo apt-get update -qq && sudo apt-get install -y -qq docker.io docker-compose-v2
|
|
}
|
|
command -v python3 >/dev/null 2>&1 || {
|
|
warn "Python3 not found. Installing..."
|
|
sudo apt-get install -y -qq python3 python3-pip python3-venv
|
|
}
|
|
command -v mariadb >/dev/null 2>&1 || {
|
|
warn "MariaDB client not found. Installing..."
|
|
sudo apt-get install -y -qq mariadb-client
|
|
}
|
|
|
|
# ── Docker ───────────────────────────────────────────────
|
|
if ! systemctl is-active --quiet docker; then
|
|
sudo systemctl enable --now docker
|
|
log "Docker started"
|
|
fi
|
|
|
|
# ── Python venv ──────────────────────────────────────────
|
|
if [ ! -d "${DEMO_HOME}/venv" ]; then
|
|
info "Creating Python virtual environment..."
|
|
python3 -m venv "${DEMO_HOME}/venv"
|
|
"${DEMO_HOME}/venv/bin/pip" install -q -r "${DEMO_HOME}/requirements.txt"
|
|
log "Python dependencies installed"
|
|
fi
|
|
|
|
# ── Docker images ────────────────────────────────────────
|
|
info "Pulling Docker images..."
|
|
docker pull mariadb:latest -q 2>/dev/null &
|
|
docker pull mariadb/maxscale:latest -q 2>/dev/null &
|
|
wait
|
|
log "Docker images ready"
|
|
|
|
# ── Cluster ──────────────────────────────────────────────
|
|
chmod +x "${DEMO_HOME}/scripts/cluster.sh"
|
|
"${DEMO_HOME}/scripts/cluster.sh" start
|
|
|
|
# ── Systemd ──────────────────────────────────────────────
|
|
if [ -d /etc/systemd/system ]; then
|
|
sudo cp "${DEMO_HOME}/scripts/mariadb-cluster.service" /etc/systemd/system/
|
|
sudo cp "${DEMO_HOME}/scripts/mariadb-monitor.service" /etc/systemd/system/
|
|
# Update paths in service files
|
|
sudo sed -i "s|/home/engineer/mariadb-monitor|${DEMO_HOME}|g" \
|
|
/etc/systemd/system/mariadb-cluster.service \
|
|
/etc/systemd/system/mariadb-monitor.service
|
|
sudo systemctl daemon-reload
|
|
sudo systemctl enable mariadb-cluster.service mariadb-monitor.service 2>/dev/null || true
|
|
log "Systemd services installed (auto-start on boot)"
|
|
fi
|
|
|
|
# ── Dashboard ────────────────────────────────────────────
|
|
"${DEMO_HOME}/venv/bin/python3" "${DEMO_HOME}/app.py" &
|
|
DASHBOARD_PID=$!
|
|
sleep 3
|
|
|
|
echo ""
|
|
echo "╔══════════════════════════════════════════════════════════╗"
|
|
echo "║ MariaDB HA Demo — Ready ║"
|
|
echo "╠══════════════════════════════════════════════════════════╣"
|
|
echo "║ Dashboard: http://$(hostname -I | awk '{print $1}'):5000 ║"
|
|
echo "║ MaxScale GUI: http://$(hostname -I | awk '{print $1}'):5000/maxscale ║"
|
|
echo "║ MaxScale API: http://127.0.0.1:8989 ║"
|
|
echo "╠══════════════════════════════════════════════════════════╣"
|
|
echo "║ Cluster: 6 nodes (Cluster 1 + Cluster 2) ║"
|
|
echo "║ Proxy: MaxScale 24.02 (port 4000) ║"
|
|
echo "║ Monitor: HA-monitor + readwritesplit ║"
|
|
echo "╚══════════════════════════════════════════════════════════╝"
|
|
echo ""
|
|
echo " Stop: sudo systemctl stop mariadb-cluster"
|
|
echo " Start: sudo systemctl start mariadb-cluster && sudo systemctl start mariadb-monitor"
|
|
echo ""
|
|
echo " Dashboard PID: ${DASHBOARD_PID}"
|
|
echo " Logs: journalctl -u mariadb-cluster -f"
|