Skip to content

Instantly share code, notes, and snippets.

@rhyzx
Last active May 30, 2019 09:17
Show Gist options
  • Save rhyzx/0a5c22fc06bdfad032f7001290995c1f to your computer and use it in GitHub Desktop.
Save rhyzx/0a5c22fc06bdfad032f7001290995c1f to your computer and use it in GitHub Desktop.
Kubernetes port redirect
#!/usr/bin/env bash
# https://github.com/txn2/kubefwd/issues/35#issuecomment-462524408
# Usage
# kuberedir 3306 172.10.0.10:3306
ID="kuberedir-$(date +%s)"
LOCAL_PORT=${1}
REMOTE=(${2//:/ })
REMOTE_ADDR=${REMOTE[0]}
REMOTE_PORT=${REMOTE[1]}
kubectl run --generator=run-pod/v1 --image=alpine/socat --expose=true --port=$REMOTE_PORT $ID tcp-listen:$REMOTE_PORT,fork,reuseaddr tcp-connect:$REMOTE_ADDR:$REMOTE_PORT
kubectl wait --for=condition=ready --timeout=60s pod/$ID
kubectl port-forward service/$ID $LOCAL_PORT:$REMOTE_PORT
# kubectl delete -l run=$ID # serivce no label
kubectl delete pod,service $ID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment