Skip to content

Instantly share code, notes, and snippets.

@ypresto
Last active February 2, 2022 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ypresto/ee4406de785325b3feaa4fc5be6f7ead to your computer and use it in GitHub Desktop.
Save ypresto/ee4406de785325b3feaa4fc5be6f7ead to your computer and use it in GitHub Desktop.
Port forward to somewhere only accessible from Kubernetes cluster
#!/bin/sh -ex
# $1: host
# $2: port
# $3: local port (defaults to $2)
pod_name=bastion-$USER
# extend timeout for fargate env
( sleep 3 && kubectl wait --for=condition=ready --timeout=1m0s pod/$pod_name && echo Start port forwarding && kubectl port-forward pod/$pod_name ${3:-$2}:$2 ) &
kubectl run $pod_name --rm -i --tty --image alpine --pod-running-timeout=1m0s -- sh -c "apk add socat && socat tcp-listen:$2,reuseaddr,fork tcp-connect:$1:$2"
kill -INT $! # kill last background job
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment