Skip to content

Instantly share code, notes, and snippets.

@xamox
Last active January 14, 2022 18:51
Show Gist options
  • Save xamox/44612830fa6fb7462b610209fcf0ec76 to your computer and use it in GitHub Desktop.
Save xamox/44612830fa6fb7462b610209fcf0ec76 to your computer and use it in GitHub Desktop.
How to forward a kubectl port-forward with socat

Yo dawg, I heard you like port-fowards so I'm going to port-forward your port-forward.

Say you want to test something like maybe run something in minikube locally but connect to something running in the kube cluster.

Do your kube port foward

kubectl --context k8s-uw1-gcp port-forward prometheus-prometheus-server-7b6f9d99f8-m4vt6 9090

On your host system

socat -v TCP-LISTEN:9091,fork TCP:127.0.0.1:9090

Get your ip address on your host system

ip addr
...
# Find the correct one
...
192.168.1.231

Then in minikube you can now reach the kube server:

~ # printf "GET / HTTP/1.0\r\n\r\n" | nc -vvvv 192.168.1.231 9091
192.168.1.231 (192.168.1.231:9092) open
HTTP/1.0 302 Found
Content-Type: text/html; charset=utf-8
Location: /graph
Date: Wed, 23 May 2018 16:34:34 GMT
Content-Length: 29

<a href="/graph">Found</a>.

sent 18, rcvd 166
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment