Skip to content

Instantly share code, notes, and snippets.

@iMartyn
Created December 20, 2017 17:34
Show Gist options
  • Save iMartyn/b435f9b5288810e8c1ab3fa37de044aa to your computer and use it in GitHub Desktop.
Save iMartyn/b435f9b5288810e8c1ab3fa37de044aa to your computer and use it in GitHub Desktop.
Port-forward minikube ports to localhost ports
#!/bin/bash
MINIKUBE_IP=$(minikube ip)
MINIKUBE_KEY_LOCATION=${HOME}/.minikube/machines/minikube/id_rsa
STARTING_PORT=8080
CURRENT_PORT=${STARTING_PORT}
for servicename in $*; do
HIGH_PORT=$(kubectl get svc ${servicename} -o custom-columns=':spec.ports[*].nodePort' | tail -n1)
if echo $HIGH_PORT | grep -q ,; then
PORT_FORWARDS_TEXT="${PORT_FORWARDS_TEXT}\nService ${servicename} is available on the following :\n"
for highport in $(echo $HIGH_PORT | sed s/','/' '/g); do
PORT_FORWARDS="${PORT_FORWARDS} -L ${CURRENT_PORT}:localhost:${highport}"
PORT_FORWARDS_TEXT="${PORT_FORWARDS_TEXT} http://localhost:${CURRENT_PORT} (minikube high port ${highport})\n"
CURRENT_PORT=$(expr ${CURRENT_PORT} + 1)
done
else
PORT_FORWARDS="${PORT_FORWARDS} -L ${CURRENT_PORT}:localhost:${HIGH_PORT}"
PORT_FORWARDS_TEXT="${PORT_FORWARDS_TEXT}\nService ${servicename} is available on http://localhost:${CURRENT_PORT}/ (minikube high port ${HIGH_PORT})"
CURRENT_PORT=$(expr ${CURRENT_PORT} + 1)
fi
done
echo "Starting ssh port forwarding (press ^c to stop)"
echo
echo -e "${PORT_FORWARDS_TEXT}"
echo
echo "SSH command that is running :"
echo "ssh docker@${MINIKUBE_IP} -i ${MINIKUBE_KEY_LOCATION} -N ${PORT_FORWARDS}"
ssh docker@${MINIKUBE_IP} -i ${MINIKUBE_KEY_LOCATION} -N ${PORT_FORWARDS}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment