I am using a windows laptop where a vagrant box is installed, where I have a kubectl client that manages some external kubernetes cluster.
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.
Here my setup:
Port-Forwarding in Vagrant:
config.vm.network "forwarded_port", guest: 8080, host: 8080, auto_correct:false
start nginx container in kubernetes:
kubectl run -i -t --image nginx test
forward port to localhost (inside vagrant):
kubectl port-forward test-64585bfbd4-zxpsd 8080:80
test nginx running inside vagrant-box:
vagrant@csbox:~$ curl http://localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
Works.
Now going a level up - on the windows host:
PS U:\> 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
Works Not.
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?
To cancel or quit the kubectl command, you can simply press Ctrl + C and the port forwarding will end immediately.
Click Firewall > Port forwarding. Click the menu icon (...) next to the appropriate port forwarding rule and select Delete. Click Apply to save the configuration and apply the change.
The port command displays the full list of guest ports mapped to the host machine ports: In a multi-machine Vagrantfile, the name of the machine must be specified: --guest PORT - This displays just the host port that corresponds to the given guest port. If the guest is not forwarding that port, an error is returned.
Vagrant. configure ("2") do | config | config. vm. network "forwarded_port", guest: 80, host: 8080 end This will allow accessing port 80 on the guest via port 8080 on the host. For most providers, forwarded ports by default bind to all interfaces. This means that other devices on your network can access the forwarded ports.
This must be greater than port 1024 unless Vagrant is running as root (which is not recommended). host_ip (string) - The IP on the host you want to bind the forwarded port to. If not specified, it will be bound to every IP. By default, this is empty.
If the guest is not forwarding that port, an error is returned. This is useful for quick scripting, for example: --machine-readable - This tells Vagrant to display machine-readable output instead of the human-friendly output.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With