#!/bin/bash # Dynamic Mycelium IPv6 Address Discovery Script for Multi-Replica NodePort # This script fetches Mycelium IPv6 addresses from ALL nodes where pods are running # Works correctly with externalTrafficPolicy: Local and multiple replicas set -e echo "🔍 Discovering Mycelium IPv6 addresses for ALL pod nodes..." # Get ALL nginx-nodeport pods POD_COUNT=$(kubectl get pods -l app=nginx-nodeport --no-headers | wc -l) echo "Found $POD_COUNT pods running" if [ "$POD_COUNT" -eq 0 ]; then echo "❌ No nginx-nodeport pod found!" echo "Please deploy the nginx-nodeport example first:" echo " kubectl apply -f nginx-nodeport-deployment.yaml" exit 1 fi # Collect all node information declare -A NODE_IPV6_MAP declare -a NODES_WITH_PODS declare -a IPV6_ADDRESSES echo "Collecting node information for all pods..." # Read pod data into array first to avoid subshell issues mapfile -t POD_DATA < <(kubectl get pods -l app=nginx-nodeport -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.nodeName}{"\n"}{end}') for line in "${POD_DATA[@]}"; do IFS=$'\t' read -r pod_name node_name <<< "$line" if [ ! -z "$node_name" ] && [ "${NODE_IPV6_MAP[$node_name]+isset}" = "" ]; then echo "Pod $pod_name is on node $node_name" # Get IPv6 address for this node IPV6=$(kubectl get node "$node_name" -o jsonpath='{range .status.addresses[?(@.type=="InternalIP")]}{.address}{"\n"}{end}' | grep -E '^[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+$' | head -1) if [ ! -z "$IPV6" ]; then NODE_IPV6_MAP[$node_name]=$IPV6 NODES_WITH_PODS+=("$node_name") IPV6_ADDRESSES+=("$IPV6") echo " ✅ Node $node_name has IPv6: $IPV6" else echo " ❌ No IPv6 found for node $node_name" fi fi done # Build the IPv6 addresses string IPV6_LIST="" FIRST=true for ipv6 in "${IPV6_ADDRESSES[@]}"; do if [ "$FIRST" = true ]; then IPV6_LIST="$ipv6" FIRST=false else IPV6_LIST="$IPV6_LIST $ipv6" fi done if [ ${#IPV6_ADDRESSES[@]} -eq 0 ]; then echo "❌ No IPv6 addresses found for any pod nodes!" exit 1 fi echo "✅ Found ${#IPV6_ADDRESSES[@]} accessible nodes with pods:" for i in "${!NODES_WITH_PODS[@]}"; do echo " ${NODES_WITH_PODS[$i]}: ${IPV6_ADDRESSES[$i]}" done # Generate HTML content with all accessible addresses cat > /tmp/index.html << 'HTML_EOF' Mycelium Cloud - Nginx NodePort Website

🌐 Mycelium Cloud

Secure NodePort Website Hosting with IPv6!
✅ NODEPORT SECURE
🔒 ENHANCED SECURITY
Connected via IPv6:
Loading...

🌐 Access URLs (NodePort: 30091)

Your website is accessible via these Mycelium worker node IPv6 addresses:

✅ Success: All REPLICA_COUNT replicas are accessible with externalTrafficPolicy: Local
Service is available on all REPLICA_COUNT nodes where pods are running.

Anyone with Mycelium installed can access your website from any of these URLs from anywhere in the world!

🚀 Key Features:

🛡️ Enhanced security with network isolation
🌍 Peer-to-peer global access via NodePort
🔒 Standard Kubernetes service patterns
⚡ Clean pod networking without hostNetwork
🖥️ Multi-replica, multi-node Kubernetes cluster
🔄 Dynamic IPv6 discovery and routing
Loading timestamp...
Mycelium Cloud NodePort Demo
Security-First IPv6 Website Hosting
Auto-updated every 30 seconds
HTML_EOF # Replace the replica count placeholder sed -i "s/REPLICA_COUNT/${#IPV6_ADDRESSES[@]}/g" /tmp/index.html echo "📝 Generated HTML content for ${#IPV6_ADDRESSES[@]} accessible nodes" # Update the ConfigMap echo "🔄 Updating ConfigMap..." kubectl create configmap nginx-nodeport-content --from-file=index.html=/tmp/index.html --dry-run=client -o yaml | kubectl apply -f - echo "✅ Successfully updated nginx-nodeport-content ConfigMap" echo "" echo "🔄 To apply changes to running pods, restart the deployment:" echo " kubectl rollout restart deployment/nginx-nodeport" echo "" echo "🌐 Website will be accessible at ALL of these URLs:" for i in "${!IPV6_ADDRESSES[@]}"; do echo " http://[${IPV6_ADDRESSES[$i]}]:30091 (Node: ${NODES_WITH_PODS[$i]})" done echo "" echo "📊 Summary:" echo " Total replicas: $POD_COUNT" echo " Accessible nodes: ${#IPV6_ADDRESSES[@]}" echo " All URLs working: ✅ YES" echo "" # Cleanup rm -f /tmp/index.html echo "✅ Multi-replica update complete!"