#!/bin/bash # Comprehensive access testing for nginx-load-balancer # Tests different networking scenarios and boundaries set -e echo "๐ŸŒ nginx-load-balancer Access Testing" echo "=====================================" echo "" # Colors GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' RED='\033[0;31m' NC='\033[0m' # No Color echo "๐Ÿ” Testing network accessibility and boundaries..." echo "" # Get service information SERVICE_IP=$(kubectl get svc nginx-load-balancer-service -o jsonpath='{.status.loadBalancer.ingress[0].ip}') SERVICE_PORT="8080" # Get all LoadBalancer IPs LB_IPS=$(kubectl get svc nginx-load-balancer-service -o jsonpath='{.status.loadBalancer.ingress[*].ip}') SERVICE_CLUSTER_IP=$(kubectl get svc nginx-load-balancer-service -o jsonpath='{.spec.clusterIP}') # Get node information WORKER_NODES=$(kubectl get nodes -l "!node-role.kubernetes.io/master" -o name) MASTER_NODES=$(kubectl get nodes -l "node-role.kubernetes.io/master" -o name) echo "๐Ÿ“Š Service Information:" echo "โ€ข Cluster IP: $SERVICE_CLUSTER_IP" echo "โ€ข LoadBalancer IPs: $LB_IPS" echo "โ€ข Port: $SERVICE_PORT" echo "" echo "๐Ÿ—๏ธ Cluster Node Information:" echo "Worker nodes:" for node in $WORKER_NODES; do echo " โ€ข $node" done if [ -n "$MASTER_NODES" ]; then echo "Master nodes:" for node in $MASTER_NODES; do echo " โ€ข $node" done fi echo "" # Test 1: Cluster-internal access (from within cluster) echo "๐Ÿงช Test 1: Cluster-Internal Access" echo "==================================" echo "Testing access from within the cluster..." echo "" # Create a test pod to access the service from inside the cluster echo "Creating test pod in cluster..." cat < /dev/null 2>&1 apiVersion: v1 kind: Pod metadata: name: access-test-pod labels: app: access-test spec: containers: - name: curl image: curlimages/curl:latest command: ["sleep", "3600"] restartPolicy: Never EOF echo "Waiting for test pod to be ready..." kubectl wait --for=condition=ready pod/access-test-pod --timeout=30s > /dev/null 2>&1 echo "Testing cluster-internal access:" # Test via service name echo "โ€ข Service name (nginx-load-balancer-service):" if kubectl exec access-test-pod -- curl -s -f "http://nginx-load-balancer-service:$SERVICE_PORT" > /dev/null 2>&1; then echo -e "${GREEN} โœ… SUCCESS: Can access via service name${NC}" else echo -e "${RED} โŒ FAILED: Cannot access via service name${NC}" fi # Test via cluster IP echo "โ€ข Cluster IP ($SERVICE_CLUSTER_IP):" if kubectl exec access-test-pod -- curl -s -f "http://$SERVICE_CLUSTER_IP:$SERVICE_PORT" > /dev/null 2>&1; then echo -e "${GREEN} โœ… SUCCESS: Can access via cluster IP${NC}" else echo -e "${RED} โŒ FAILED: Cannot access via cluster IP${NC}" fi # Test via LoadBalancer IP echo "โ€ข LoadBalancer IP ($SERVICE_IP):" if [ -n "$SERVICE_IP" ]; then if kubectl exec access-test-pod -- curl -s -f "http://$SERVICE_IP:$SERVICE_PORT" > /dev/null 2>&1; then echo -e "${GREEN} โœ… SUCCESS: Can access via LoadBalancer IP${NC}" else echo -e "${RED} โŒ FAILED: Cannot access via LoadBalancer IP${NC}" fi else echo -e "${YELLOW} โš ๏ธ No LoadBalancer IP available${NC}" fi # Clean up test pod kubectl delete pod access-test-pod --ignore-not-found=true > /dev/null 2>&1 echo "" echo -e "${BLUE}๐Ÿ’ก Cluster-Internal Access Results:${NC}" echo "โ€ข This tests if the service works from inside the Kubernetes cluster" echo "โ€ข Service name should always work (DNS resolution)" echo "โ€ข Cluster IP should work (internal networking)" echo "โ€ข LoadBalancer IP may or may not work from inside (depends on network config)" echo "" # Test 2: External access from current machine echo "๐Ÿงช Test 2: External Access (Current Machine)" echo "============================================" echo "Testing access from your current machine (local PC)..." echo "" echo "Current machine location: $(hostname)" echo "Current user: $(whoami)" echo "" # Test LoadBalancer IP access if [ -n "$SERVICE_IP" ]; then echo "Testing LoadBalancer IP ($SERVICE_IP) from current machine:" # Test IPv4 echo "โ€ข IPv4 access (http://$SERVICE_IP:$SERVICE_PORT):" if timeout 10 curl -s -f "http://$SERVICE_IP:$SERVICE_PORT" > /dev/null 2>&1; then echo -e "${GREEN} โœ… SUCCESS: IPv4 access works from current machine${NC}" echo " Content preview:" curl -s "http://$SERVICE_IP:$SERVICE_PORT" | head -3 else echo -e "${RED} โŒ FAILED: IPv4 access does not work from current machine${NC}" echo " This means the LoadBalancer IP is not routable from your location" fi echo "" # Test IPv6 echo "โ€ข IPv6 access (curl -6 'http://$SERVICE_IP:$SERVICE_PORT'):" if timeout 10 curl -6 -s -f "http://$SERVICE_IP:$SERVICE_PORT" > /dev/null 2>&1; then echo -e "${GREEN} โœ… SUCCESS: IPv6 access works from current machine${NC}" echo " Content preview:" curl -6 -s "http://$SERVICE_IP:$SERVICE_PORT" | head -3 else echo -e "${RED} โŒ FAILED: IPv6 access does not work from current machine${NC}" echo " This means the IPv6 address is not routable from your location" fi else echo -e "${YELLOW}โš ๏ธ No LoadBalancer IP to test${NC}" fi echo "" echo -e "${BLUE}๐Ÿ’ก External Access Results:${NC}" echo "โ€ข This tests if you can access the service from your local machine" echo "โ€ข If this fails, the service is only accessible from within the cluster" echo "โ€ข This is normal for many cloud setups (LoadBalancer IPs are cluster-internal)" echo "" # Test 3: Network diagnostics echo "๐Ÿงช Test 3: Network Diagnostics" echo "==============================" echo "" echo "๐Ÿ” Network Interface Information:" echo "Current machine network configuration:" ip addr show 2>/dev/null | grep -E "(inet|interface)" | head -5 || echo "Could not retrieve network info" echo "" echo "๐Ÿ” Routing Information:" echo "Current routing table:" ip route 2>/dev/null | head -5 || echo "Could not retrieve routing info" echo "" echo "๐Ÿ” DNS Resolution:" echo "Testing DNS for the LoadBalancer IP:" host $SERVICE_IP 2>/dev/null || echo "No DNS record for $SERVICE_IP" echo "" echo "๐Ÿ” Ping Test:" if [ -n "$SERVICE_IP" ]; then echo "Pinging LoadBalancer IP ($SERVICE_IP):" if ping -c 2 $SERVICE_IP > /dev/null 2>&1; then echo -e "${GREEN} โœ… SUCCESS: IP is pingable${NC}" else echo -e "${RED} โŒ FAILED: IP is not pingable${NC}" fi fi echo "" # Test 4: Access method analysis echo "๐Ÿงช Test 4: Access Method Analysis" echo "=================================" echo "" echo "๐ŸŽฏ Access Scenarios Analysis:" echo "" # Scenario 1: Cluster-internal only echo "Scenario 1: Cluster-Internal Only (Most Common)" echo "โ€ข How: kubectl exec into a pod and access the service" echo "โ€ข Use case: Microservices communicating with each other" echo "โ€ข Command: kubectl run test --image=curlimages/curl --rm -it -- curl http://nginx-load-balancer-service:8080" echo "" # Scenario 2: Local machine access echo "Scenario 2: Local Machine Access" echo "โ€ข How: Direct HTTP requests from your PC to LoadBalancer IP" echo "โ€ข Use case: Testing services from development machine" echo "โ€ข Command: curl http://$SERVICE_IP:$SERVICE_PORT" echo "" # Scenario 3: Node port access echo "Scenario 3: Node Port Access (Alternative)" echo "โ€ข How: Access via individual node IPs + port" echo "โ€ข Use case: When LoadBalancer IP is not externally accessible" echo "โ€ข Get node IPs: kubectl get nodes -o wide" echo "โ€ข Test: curl http://[node-ip]:8080" echo "" # Final recommendations echo "======================================" echo "๐Ÿ“‹ NETWORK ACCESS SUMMARY" echo "======================================" echo "" echo -e "${BLUE}๐Ÿ” Current Status:${NC}" # Check if external access works if [ -n "$SERVICE_IP" ]; then if timeout 5 curl -s -f "http://$SERVICE_IP:$SERVICE_PORT" > /dev/null 2>&1; then echo -e "${GREEN}โœ… EXTERNAL ACCESS: Works from your local machine${NC}" echo " You can access http://$SERVICE_IP:$SERVICE_PORT directly" echo " LoadBalancer is externally routable" else echo -e "${YELLOW}โš ๏ธ EXTERNAL ACCESS: Does not work from your local machine${NC}" echo " LoadBalancer IP is cluster-internal only" echo " This is normal for many cloud environments" fi else echo -e "${YELLOW}โš ๏ธ NO LOADBALANCER IP ASSIGNED${NC}" fi echo "" echo -e "${BLUE}๐ŸŽฏ Recommended Access Methods:${NC}" echo "1. For testing from local machine:" if [ -n "$SERVICE_IP" ]; then echo " โ€ข Try: curl http://$SERVICE_IP:$SERVICE_PORT" echo " โ€ข Try: curl -6 http://$SERVICE_IP:$SERVICE_PORT" else echo " โ€ข LoadBalancer IP not available" fi echo "" echo "2. For cluster-internal testing:" echo " โ€ข kubectl run test --image=curlimages/curl --rm -it -- curl http://nginx-load-balancer-service:8080" echo "" echo "3. For alternative access (if LoadBalancer doesn't work externally):" echo " โ€ข kubectl get nodes -o wide" echo " โ€ข Test direct node access: curl http://[node-ip]:8080" echo "" echo -e "${BLUE}๐Ÿ› ๏ธ If External Access Doesn't Work:${NC}" echo "โ€ข This is normal for many Kubernetes setups" echo "โ€ข LoadBalancer services may only be accessible within the cluster" echo "โ€ข Mycelium Cloud may require specific network configuration for external access" echo "โ€ข Consider using port forwarding: kubectl port-forward svc/nginx-load-balancer-service 8080:8080" echo "" echo "โœ… Access testing complete!" echo "" echo "๐Ÿ’ก Next steps based on results:" echo "โ€ข If external access works: Use LoadBalancer IP for development" echo "โ€ข If external access doesn't work: Use port-forwarding or internal testing" echo "โ€ข Always test both IPv4 and IPv6 when available"