#!/bin/bash # # Kubernetes Port Forwarding Setup Script # Sets up port forwarding for accessing K8s services locally # set -e # Colors for output GREEN='\033[0;32m' YELLOW='\033[1;33m' RED='\033[0;31m' NC='\033[0m' # No Color echo -e "${GREEN}========================================${NC}" echo -e "${GREEN}Starting K8s Port Forwarding${NC}" echo -e "${GREEN}========================================${NC}" # Function to stop existing port forwards stop_existing_forwards() { echo -e "${YELLOW}Stopping existing port forwards...${NC}" pkill -f "kubectl.*port-forward" 2>/dev/null || true sleep 2 } # Function to start port forward start_port_forward() { local service=$1 local local_port=$2 local service_port=$3 echo -e "Starting port forward: ${GREEN}$service${NC} (localhost:$local_port → service:$service_port)" kubectl -n site11-pipeline port-forward service/$service $local_port:$service_port & # Wait a moment for the port forward to establish sleep 2 # Check if port forward is working if lsof -i :$local_port | grep -q LISTEN; then echo -e " ${GREEN}✓${NC} Port forward established on localhost:$local_port" else echo -e " ${RED}✗${NC} Failed to establish port forward on localhost:$local_port" fi } # Stop existing forwards first stop_existing_forwards # Start port forwards echo -e "\n${YELLOW}Starting port forwards...${NC}\n" # Console Frontend start_port_forward "console-frontend" 8080 3000 # Console Backend start_port_forward "console-backend" 8000 8000 # Summary echo -e "\n${GREEN}========================================${NC}" echo -e "${GREEN}Port Forwarding Active!${NC}" echo -e "${GREEN}========================================${NC}" echo -e "\n${YELLOW}Available endpoints:${NC}" echo -e " Console Frontend: ${GREEN}http://localhost:8080${NC}" echo -e " Console Backend: ${GREEN}http://localhost:8000${NC}" echo -e " Health Check: ${GREEN}http://localhost:8000/health${NC}" echo -e " API Health: ${GREEN}http://localhost:8000/api/health${NC}" echo -e "\n${YELLOW}To stop port forwarding:${NC}" echo -e " pkill -f 'kubectl.*port-forward'" echo -e "\n${YELLOW}To check status:${NC}" echo -e " ps aux | grep 'kubectl.*port-forward'" # Keep script running echo -e "\n${YELLOW}Port forwarding is running in background.${NC}" echo -e "Press Ctrl+C to stop all port forwards..." # Trap to clean up on exit trap "echo -e '\n${YELLOW}Stopping port forwards...${NC}'; pkill -f 'kubectl.*port-forward'; exit" INT TERM # Keep the script running while true; do sleep 60 # Check if port forwards are still running if ! pgrep -f "kubectl.*port-forward" > /dev/null; then echo -e "${RED}Port forwards stopped unexpectedly. Restarting...${NC}" start_port_forward "console-frontend" 8080 3000 start_port_forward "console-backend" 8000 8000 fi done