Skip to content

Instantly share code, notes, and snippets.

@shubhamoli
Created August 28, 2024 12:56
Show Gist options
  • Save shubhamoli/679b3d6c788c5a9671fbd0c659964f73 to your computer and use it in GitHub Desktop.
Save shubhamoli/679b3d6c788c5a9671fbd0c659964f73 to your computer and use it in GitHub Desktop.
kubectl port-forward script
SVC=<service-name>
S_PORT=8000
D_PORT=8000
kubectl port-forward deployment/${SVC} $S_PORT:$D_PORT &
PID_PORTF=$!
# Wait for $S_PORT to become available
# This is required as port-forwarding is not instant
while ! nc -vz localhost "$S_PORT" > /dev/null 2>&1 ; do
echo "Waiting for $S_PORT:$D_PORT port-forwaring to be up..."
sleep 1
done
# Kill port-foward regardless of how script exits
trap '{
echo "Stopping port-forwading and killing PID $PID_PORTF"
kill $PID_PORTF
}' EXIT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment