as in ngrok
Expose your local TCP server as Kubernetes service in remote cluster.
Giving we want to create service in namespace
with name service_name
on port service_port
forwarding all traffic from it to local_host
(usually 127.0.0.1) and local_port
.
-
Install krok client:
pip install krok
-
Install krok server:
kubectl -n <namespace> run --image=smpio/krok-server krok
-
Run krok:
krok -n <namespace> -p <local_port> <service_name> <service_port>
We have running krok server in the pod krok_pod
in namespace
. The server is simple OpenSSH server that allows to forward incoming connections.
Krok client is simple script that automates the following steps:
-
Run
kubectl port-forward <krok_pod> :22
. It will listen on randomlocal_ssh_port
forwarding all connections to krok's OpenSSH server onkrok_pod
. -
Run
ssh -N -R *:0:<local_host>:<local_port> -p <local_ssh_port> krok@localhost
. This causes krok's OpenSSH server onkrok_pod
to listen on randompod_port
forwarding all connections tolocal_host:local_port
using SSH tunnel. -
Create or update service
service_name
with portservice_port
and targetPortpod_port
, with selector matchingkrok_pod
.
In the result, all connections to service_name:service_port
will be forwarded to krok_pod
, then to your local machine via SSH tunnel forwarded by kubectl port-forward
, and then forwarded by SSH client to local_host:local_port
.