Created
October 22, 2022 00:47
-
-
Save tyzbit/554b583298a0b73aae5ebbed80937ccf to your computer and use it in GitHub Desktop.
Kube Proxy - Use a Kubernetes cluster to proxy out to somewhere else
This file contains 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
function kubeproxy() { | |
setopt local_traps | |
pod_name="socat-$(whoami)" | |
if [[ "${2}x" == "x" ]]; then | |
echo "Usage: kubeproxy [Hostname] [Port]" | |
return 0 | |
fi | |
if kubectl run --restart=Never --image=alpine/socat $pod_name -- -d -d tcp-listen:$2,fork,reuseaddr tcp-connect:$1:$2; then | |
while [[ $(kubectl get pod $pod_name -o json | jq -r '.status.phase') != "Running" ]]; do | |
sleep 0.1 | |
done | |
proxy_command="kubectl port-forward $pod_name $2:$2" | |
trap "kubectl delete pod --wait=false $pod_name" INT TERM EXIT | |
if eval "$proxy_command"; then | |
:; | |
else | |
echo -e "Pod created but \e[36;m$proxy_command\e[0;m failed" | |
echo -e "Trying a different local port (\e[32;m8888\e[0;m)" | |
if kubectl port-forward $pod_name 8888:$2; then | |
:; | |
else | |
echo -e "Unable to port-forward, deleting \e[32;m$pod_name\e[0;m" | |
kubectl delete pod --wait=false $pod_name | |
fi | |
fi | |
else | |
echo -e "\e[31;mPod did not start correctly, delete \e[32m$pod_name\e[31;m if it exists\e[0;m" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment