fix: Correct nginx-nodeport script to use pod's node IPv6 with externalTrafficPolicy: Local

This commit is contained in:
mik-tf
2025-11-06 21:08:32 -05:00
parent bc2d47ec9c
commit bb838a3b06

View File

@@ -1,28 +1,41 @@
#!/bin/bash #!/bin/bash
# Dynamic Mycelium IPv6 Address Discovery Script for NodePort # Dynamic Mycelium IPv6 Address Discovery Script for NodePort
# This script fetches Mycelium IPv6 addresses from worker nodes and generates HTML content # This script fetches Mycelium IPv6 address of the node where the pod is running
# With externalTrafficPolicy: Local, service is only accessible on nodes with pods
set -e set -e
echo "🔍 Discovering Mycelium IPv6 addresses from worker nodes..." echo "🔍 Discovering Mycelium IPv6 address for pod's node..."
# Fetch IPv6 addresses from cluster worker nodes # Get the node where the nginx-nodeport pod is running
IPV6_ADDRESSES=$(kubectl get nodes -l kubernetes.io/role!=master -o jsonpath='{range .items[*]}{range .status.addresses[?(@.type=="InternalIP")]}{.address}{"\n"}{end}{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]+$') POD_NAME=$(kubectl get pods -l app=nginx-nodeport -o name | head -1)
if [ -z "$POD_NAME" ]; then
if [ -z "$IPV6_ADDRESSES" ]; then echo "❌ No nginx-nodeport pod found!"
echo "⚠️ No IPv6 addresses found from worker nodes!" echo "Please deploy the nginx-nodeport example first:"
echo "Trying all nodes..." echo " kubectl apply -f nginx-nodeport-deployment.yaml"
IPV6_ADDRESSES=$(kubectl get nodes -o jsonpath='{range .items[*]}{range .status.addresses[?(@.type=="InternalIP")]}{.address}{"\n"}{end}{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]+$')
fi
if [ -z "$IPV6_ADDRESSES" ]; then
echo "❌ No IPv6 addresses found!"
exit 1 exit 1
fi fi
echo "✅ Found IPv6 addresses:" POD_NODE=$(kubectl get pods -l app=nginx-nodeport -o jsonpath='{.items[0].spec.nodeName}')
echo "$IPV6_ADDRESSES" echo "Pod is running on node: $POD_NODE"
# Get Mycelium IPv6 address of the SPECIFIC node where pod is running
# This is critical with externalTrafficPolicy: Local
IPV6_ADDRESS=$(kubectl get node "$POD_NODE" -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_ADDRESS" ]; then
echo "❌ No IPv6 address found for node $POD_NODE!"
exit 1
fi
IPV6_ADDRESSES="$IPV6_ADDRESS"
echo "✅ Pod's node Mycelium IPv6 address: $IPV6_ADDRESS"
echo "⚠️ NOTE: With externalTrafficPolicy: Local, service is only accessible on THIS node"
echo ""
echo "To access all nodes, scale the deployment:"
echo " kubectl scale deployment nginx-nodeport --replicas=3"
# Generate HTML content with dynamic addresses # Generate HTML content with dynamic addresses
cat > /tmp/index.html << 'HTML_EOF' cat > /tmp/index.html << 'HTML_EOF'
@@ -180,18 +193,24 @@ cat > /tmp/index.html << 'HTML_EOF'
</div> </div>
<div class="urls"> <div class="urls">
<h3>🌐 Global Access URLs (NodePort: 30091)</h3> <h3>🌐 Access URL (NodePort: 30091)</h3>
<p><strong>Your website is accessible via these Mycelium worker node IPv6 addresses:</strong></p> <p><strong>Your website is accessible via this Mycelium worker node IPv6 address:</strong></p>
<ul> <ul>
HTML_EOF HTML_EOF
# Add each IPv6 address to the HTML # Add the single IPv6 address to the HTML
while IFS= read -r ipv6; do while IFS= read -r ipv6; do
echo " <li><code>http://[$ipv6]:30091</code> ✅</li>" >> /tmp/index.html echo " <li><code>http://[$ipv6]:30091</code> ✅</li>" >> /tmp/index.html
echo " <li><strong>Node:</strong> $POD_NODE</li>" >> /tmp/index.html
done <<< "$IPV6_ADDRESSES" done <<< "$IPV6_ADDRESSES"
cat >> /tmp/index.html << 'HTML_EOF' cat >> /tmp/index.html << 'HTML_EOF'
</ul> </ul>
<div style="background: rgba(255, 193, 7, 0.2); padding: 1rem; border-radius: 8px; margin: 1rem 0; border-left: 4px solid #FFC107;">
<strong>⚠️ Note:</strong> With <code>externalTrafficPolicy: Local</code>, the service is only accessible on the node where the pod is running.
</div>
<p><strong>To make accessible on all nodes:</strong></p>
<pre style="text-align: left; background: rgba(0,0,0,0.3); padding: 1rem; border-radius: 4px; font-size: 0.8rem;">kubectl scale deployment nginx-nodeport --replicas=3</pre>
<p><em>Anyone with Mycelium installed can access your website from anywhere!</em></p> <p><em>Anyone with Mycelium installed can access your website from anywhere!</em></p>
</div> </div>
@@ -219,7 +238,7 @@ cat >> /tmp/index.html << 'HTML_EOF'
</html> </html>
HTML_EOF HTML_EOF
echo "📝 Generated HTML content with $(echo "$IPV6_ADDRESSES" | wc -l) IPv6 addresses" echo "📝 Generated HTML content for pod's node: $POD_NODE"
# Update the ConfigMap # Update the ConfigMap
echo "🔄 Updating ConfigMap..." echo "🔄 Updating ConfigMap..."
@@ -230,10 +249,15 @@ echo ""
echo "🔄 To apply changes to running pods, restart the deployment:" echo "🔄 To apply changes to running pods, restart the deployment:"
echo " kubectl rollout restart deployment/nginx-nodeport" echo " kubectl rollout restart deployment/nginx-nodeport"
echo "" echo ""
echo "🌐 Website will be accessible at: http://[worker-node-ipv6]:30091" echo "🌐 Website will be accessible at: http://[$IPV6_ADDRESS]:30091"
echo "" echo ""
echo "📊 Discovered IPv6 addresses:" echo "📊 Pod's node information:"
echo "$IPV6_ADDRESSES" | nl echo " Node: $POD_NODE"
echo " Mycelium IPv6: $IPV6_ADDRESS"
echo ""
echo "⚠️ Note: Service is only accessible on this specific node"
echo " To make accessible on all nodes, scale to 3 replicas:"
echo " kubectl scale deployment nginx-nodeport --replicas=3"
# Cleanup # Cleanup
rm -f /tmp/index.html rm -f /tmp/index.html