windows host + vagrant + kubectl port-forward: stuck inside vagrant
By default, kubectl port-forward
binds to the address 127.0.0.1
. That's why you are not able to access it outside vagrant. The solution is to make kubectl port-forward
to bind to 0.0.0.0
using the argument --address 0.0.0.0
Running the command:
kubectl port-forward test-64585bfbd4-zxpsd --address 0.0.0.0 8080:80
will solve your issue.
kubectl port-forward
binds to 127.0.0.1 and doesn't allow you to define a bind address. The traffic from your Windows host machine hits the main network interface of your Vagrant VM and therefore, this doesn't work. You can fix the issue by routing traffic from the Vagrant VM's main network interface to the loopback interface using iptables
:`
- Forward traffic from your vagrant VM's main network interface to 127.0.0.1 (replace
$PORT
with the port you're forwarding):$ $ iptables -t nat -I PREROUTING -p tcp --dport $PORT -j DNAT --to-destination 127.0.0.1:$PORT
- Look up the name of your Vagrant VM's main network interface:
$ ifconfigenp0s3 Link encap:Ethernet HWaddr 02:38:b8:f5:60:7e inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0 inet6 addr: fe80::38:b8ff:fef5:607e/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1106 errors:0 dropped:0 overruns:0 frame:0 TX packets:736 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:423190 (423.1 KB) TX bytes:80704 (80.7 KB)lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
- As forwarding traffic to the loopback interface is disabled per default, enable forwarding to the loopback interface (replace
$MAIN_NETWORK_INTERFACE_NAME
with the interface name, in the example aboveenp0s3
):sysctl -w net.ipv4.conf.$MAIN_NETWORK_INTERFACE_NAME.route_localnet=1