#!/bin/bash # Complete Deploy and Test Script for nginx-load-balancer # This script deploys a LoadBalancer service with automatic IPv6 assignment set -e echo "🚀 nginx-load-balancer Deploy and Test" echo "==================================" echo "" # Colors GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Step 1: Check if kubectl is available echo "🔍 Step 1: Checking prerequisites..." if ! command -v kubectl &> /dev/null; then echo "❌ kubectl is not installed or not in PATH" echo " Please install kubectl and configure it to connect to your Mycelium Cloud cluster" exit 1 fi # Test cluster connectivity if ! kubectl cluster-info &> /dev/null; then echo "❌ Cannot connect to Kubernetes cluster" echo " Please check your kubeconfig configuration" exit 1 fi echo -e "${GREEN}✅ kubectl is available and connected to cluster${NC}" echo "" # Step 2: Deploy all resources echo "🔍 Step 2: Deploying nginx-load-balancer resources..." echo "" echo " 📦 Deploying ConfigMaps..." kubectl apply -f nginx-load-balancer-configmaps.yaml echo -e " ${GREEN}✅ ConfigMaps deployed${NC}" echo "" echo " 📦 Deploying nginx application (3 replicas)..." kubectl apply -f nginx-load-balancer-deployment.yaml echo -e " ${GREEN}✅ nginx deployment created (worker-only, 3 replicas)${NC}" echo "" echo " 📦 Creating LoadBalancer service..." kubectl apply -f nginx-load-balancer-service.yaml echo -e " ${GREEN}✅ LoadBalancer service created${NC}" echo "" # Step 3: Wait for deployment to be ready echo "🔍 Step 3: Waiting for deployment to be ready..." echo "" echo " This may take up to 90 seconds due to 3 replicas..." if kubectl wait --for=condition=ready pod -l app=nginx-load-balancer --timeout=90s 2>/dev/null; then echo -e " ${GREEN}✅ nginx-load-balancer pods are ready${NC}" else echo -e " ${YELLOW}⚠️ Pods taking longer than expected, continuing anyway...${NC}" fi echo "" # Step 4: Check pod status echo "🔍 Step 4: Checking pod status..." POD_COUNT=$(kubectl get pods -l app=nginx-load-balancer --no-headers | wc -l) echo " Total pods running: $POD_COUNT/3" if [ "$POD_COUNT" -ne 3 ]; then echo -e " ${YELLOW}⚠️ Expected 3 pods, found $POD_COUNT. Check with:${NC}" echo " kubectl get pods -l app=nginx-load-balancer" echo " kubectl describe pod -l app=nginx-load-balancer" echo " kubectl logs -l app=nginx-load-balancer" fi echo "" # Step 5: Check service configuration echo "🔍 Step 5: Verifying service configuration..." SERVICE_TYPE=$(kubectl get svc nginx-load-balancer-service -o jsonpath='{.spec.type}' 2>/dev/null || echo "NotFound") EXTERNAL_IP=$(kubectl get svc nginx-load-balancer-service -o jsonpath='{.status.loadBalancer.ingress[0].ip}' 2>/dev/null || echo "NotFound") IP_FAMILIES=$(kubectl get svc nginx-load-balancer-service -o jsonpath='{.spec.ipFamilies}' 2>/dev/null || echo "NotFound") echo " Service type: $SERVICE_TYPE" echo " External IP: $EXTERNAL_IP" echo " IP families: $IP_FAMILIES" if [ "$SERVICE_TYPE" != "LoadBalancer" ]; then echo -e " ${YELLOW}⚠️ Service type is not LoadBalancer! Expected: LoadBalancer, Got: $SERVICE_TYPE${NC}" fi if [[ "$IP_FAMILIES" == *"IPv6"* ]]; then echo -e " ${GREEN}✅ Dual-stack configured (includes IPv6)${NC}" else echo -e " ${YELLOW}⚠️ IPv6 not configured! Service will not be accessible via Mycelium IPv6${NC}" echo " This is a critical requirement for Mycelium Cloud!" fi echo "" # Step 6: Update content with current node information echo "🔍 Step 6: Updating website content with current load balancer information..." echo "" ./update-content-load-balancer.sh if [ $? -eq 0 ]; then echo -e "${GREEN}✅ Content updated successfully${NC}" else echo -e "${YELLOW}⚠️ Content update failed, continuing anyway...${NC}" fi echo "" # Step 7: Restart deployment to apply content changes echo "🔍 Step 7: Restarting deployment to apply content changes..." echo "" kubectl rollout restart deployment/nginx-load-balancer if kubectl rollout status deployment/nginx-load-balancer --timeout=90s 2>/dev/null; then echo -e "${GREEN}✅ Deployment rolled out successfully${NC}" else echo -e "${YELLOW}⚠️ Rollout taking longer than expected, continuing...${NC}" fi echo "" # Step 8: Run load balancing tests echo "🔍 Step 8: Running load balancing tests..." echo "" # Check if service has external IP SERVICE_IP=$(kubectl get svc nginx-load-balancer-service -o jsonpath='{.status.loadBalancer.ingress[0].ip}' 2>/dev/null || echo "") if [ -n "$SERVICE_IP" ] && [ "$SERVICE_IP" != "null" ]; then echo " 🌍 Testing service access..." echo " Service URL: http://$SERVICE_IP:8080" echo "" echo " ⚖️ Load balancing test: All 3 replicas should respond to requests" echo " Replica 1: Check pod status for nginx-load-balancer-xxx-1" echo " Replica 2: Check pod status for nginx-load-balancer-xxx-2" echo " Replica 3: Check pod status for nginx-load-balancer-xxx-3" else echo " ⏳ External IP not yet assigned. Mycelium may be provisioning IPv6 address..." echo " Check with: kubectl get svc nginx-load-balancer-service" fi echo "" echo "==================================" echo "🎉 Deploy and Test Complete!" echo "==================================" echo "" echo "📊 Summary:" echo " • Resources deployed: ConfigMaps, Deployment (3 replicas), Service" echo " • Service type: LoadBalancer with dual-stack (IPv4 + IPv6)" echo " • External IP: $SERVICE_IP" echo " • Content updated: Yes" echo " • Load balancing: Active across 3 replicas" echo "" echo "🌐 Access Information:" if [ -n "$SERVICE_IP" ] && [ "$SERVICE_IP" != "null" ]; then echo " • Service URL: http://$SERVICE_IP:8080" echo "" echo " To access from a machine with Mycelium installed:" echo " curl -6 \"http://[$SERVICE_IP]:8080/\"" echo " Or open in browser:" echo " http://[$SERVICE_IP]:8080" else echo " • Service URL: http://[mycelium-assigned-ipv6]:8080" echo " • External IP: Pending (Mycelium assigning IPv6 address)" echo "" echo " Check status with:" echo " kubectl get svc nginx-load-balancer-service" fi echo "" echo "📋 Next Steps:" echo " • Monitor pod distribution: kubectl get pods -l app=nginx-load-balancer -o wide" echo " • Check service status: kubectl get svc nginx-load-balancer-service" echo " • Test load balancing: kubectl get pods -l app=nginx-load-balancer" echo " • Scale replicas: kubectl scale deployment nginx-load-balancer --replicas=5" echo " • Update content: ./update-content-load-balancer.sh" echo "" echo "📚 Documentation:" echo " • Guide: nginx-load-balancer.md" echo " • Load balancing theory: https://kubernetes.io/docs/concepts/services-networking/service/#loadbalancer" echo " • Mycelium IPv6: Check service status for automatic IPv6 assignment" echo ""