Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kubectl port-forward to another endpoint

Is there a corresponding command with kubectl to:

ssh -L8888:rds.aws.com:5432 example.com

kubectl has port-forward you can also specify --address but that strictly requires an IP address.

like image 344
Mahoni Avatar asked Apr 16 '20 22:04

Mahoni


2 Answers

The older answer is valid. Still, a workaround would be to use something like https://hub.docker.com/r/marcnuri/port-forward

kubectl run --env REMOTE_HOST=your.service.com --env REMOTE_PORT=8080 --env LOCAL_PORT=8080 --port 8080 --image marcnuri/port-forward test-port-forward

Run it on the cluster and then port forward to it.

kubectl port-forward test-port-forward 8080:8080

like image 63
giorgio Avatar answered Sep 19 '22 07:09

giorgio


Short answer, No.

In OpenSSH, local port forwarding is configured using the -L option:

   ssh -L 80:intra.example.com:80 gw.example.com

This example opens a connection to the gw.example.com jump server, and forwards any connection to port 80 on the local machine to port 80 on intra.example.com.

By default, anyone (even on different machines) can connect to the specified port on the SSH client machine. However, this can be restricted to programs on the same host by supplying a bind address:

   ssh -L 127.0.0.1:80:intra.example.com:80 gw.example.com

You can read the docs here.

The port-forward in Kubernetes works only within the cluster, you can forward traffic that will hit specified port to Deployment or Service or a Pod

kubectl port-forward TYPE/NAME [options] [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N]

--address flag is to specify what to listen on 0.0.0.0 means everything localhost is as name and you can set an IP on which it can be listening on. Documentation is available here, you can also read Use Port Forwarding to Access Applications in a Cluster.

like image 40
Crou Avatar answered Sep 21 '22 07:09

Crou