#!/bin/bash
# 실습 #13 ① — 기본 VRRP HA VIP (Master/Backup 2 노드)
# 사용: bash setup_01_basic.sh master    (lb-1 에서)
#       bash setup_01_basic.sh backup    (lb-2 에서)
set -e

ROLE="${1:-master}"
DIR="$(dirname "$0")/configs"
case "$ROLE" in
    master) CFG="$DIR/01_keepalived_basic_master.conf" ;;
    backup) CFG="$DIR/01_keepalived_basic_backup.conf" ;;
    *) echo "사용: bash $0 {master|backup}"; exit 1 ;;
esac

sudo apt install -y keepalived
sudo cp "$CFG" /etc/keepalived/keepalived.conf
sudo systemctl enable --now keepalived
sleep 2

echo
echo "=== $ROLE 노드 검증 ==="
ip addr show eth0 | grep -E "inet " || true
echo
echo "→ MASTER 면 VIP 192.168.56.100 부착되어 있어야 함"
echo "→ BACKUP 면 VIP 부착 안 됨 (정상)"
echo
echo "→ 호스트에서 VIP ping 확인: ping -c 3 192.168.56.100"
echo
echo "→ Failover 시연 (MASTER 측): sudo systemctl stop keepalived"
echo "   1~3초 후 BACKUP 측 ip addr 확인 — VIP 이동"
