<p>I am using a windows laptop where a vagrant box is installed, where I have a kubectl client that manages some external kubernetes cluster.</p>

<p>For debugging purposes I would like to do a port-forwarding via kubectl and access this port from the host machine. This works perfectly from inside vagrant to the kubernetes cluster, but obviously something doesn't work in conjunction with the vagrant port forwarding from host to vagrant.</p>

<p>Here my setup:</p>

<ol>
<li><p>Port-Forwarding in Vagrant:</p>

<p><code>config.vm.network "forwarded_port", guest: 8080, host: 8080, auto_correct:false</code></p></li>
<li><p>start nginx container in kubernetes:</p>

<p><code>kubectl run -i -t --image nginx test</code></p></li>
<li><p>forward port to localhost (inside vagrant):</p>

<p><code>kubectl port-forward test-64585bfbd4-zxpsd 8080:80</code></p></li>
<li><p>test nginx running inside vagrant-box:</p>

<pre><code>vagrant@csbox:~$ curl http://localhost:8080
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Welcome to nginx!&lt;/title&gt;
&lt;style&gt;
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Welcome to nginx!&lt;/h1&gt;
&lt;p&gt;If you see this page, the nginx web server is successfully installed and working. Further configuration is required.&lt;/p&gt;

&lt;p&gt;For online documentation and support please refer to
&lt;a href="http://nginx.org/"&gt;nginx.org&lt;/a&gt;.&lt;br/&gt;
Commercial support is available at
&lt;a href="http://nginx.com/"&gt;nginx.com&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thank you for using nginx.&lt;/em&gt;&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre></li>
</ol>

<p>Works.</p>

<ol start="5">
<li><p>Now going a level up - on the windows host:</p>

<pre><code>PS U:\&gt; Invoke-WebRequest http://localhost:8080

Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a receive.
At line:1 char:1
+ Invoke-WebRequest http://localhost:8080
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation:     (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
</code></pre></li>
</ol>

<p>Works Not.</p>

<p>From my understanding - just looking at the port forwardings everything should be okay. Do you have any ideas why this doesn't work like expected?</p>

<p>By default, <code>kubectl port-forward</code> binds to the address <code>127.0.0.1</code>. That's why you are not able to access it outside vagrant. The solution is to make <code>kubectl port-forward</code> to bind to <code>0.0.0.0</code> using the argument <code>--address 0.0.0.0</code></p>

<p>Running the command:</p>

<pre class="lang-sh prettyprint-override"><code>kubectl port-forward test-64585bfbd4-zxpsd --address 0.0.0.0 8080:80
</code></pre>

<p>will solve your issue.</p>
