Search code examples
linuxdockerkubernetesnetwork-programmingportforwarding

3 level Port forwarding (Kubernetes Pod -> Docker Container -> Local


I use a docker container to interact with my kubernetes cluster. I run kubectl from inside the container. All works fine except when I want to port forward. I can use kubectl port forward to forward from the pod to my container. But then I won't be able to access the site from my laptop browser. I can only curl from inside the container.

Is there any way at all I can access the site from my browser. docker host networking mode isn't supported on Macs and I use a Mac. Any suggestions?


Solution

  • I have this scenario you may adapt for your need :

    On my laptop, I run :

    docker run -it --rm -p 2222:2222 ubuntu bash
    

    From inside container, I run :

    kubectl port-forward --address 0.0.0.0 pod/my-pod-7f66c99ddd-6c429 2222:22
    

    Now this is the diagram for port-forwarding :

          2222             2222                 2222              22
    laptop -------------------> docker-container------------------->k8s-pod
    

    Now, in another terminal on my laptop, I can do

    ssh -p 2222 user-on-pod@laptop-hostname
    

    to arrive at pod