Created
August 28, 2024 12:56
-
-
Save shubhamoli/679b3d6c788c5a9671fbd0c659964f73 to your computer and use it in GitHub Desktop.
kubectl port-forward script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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