简体   繁体   English

Windows主机+无业游民+ kubectl端口转发:卡在无业游民

[英]windows host + vagrant + kubectl port-forward: stuck inside vagrant

I am using a windows laptop where a vagrant box is installed, where I have a kubectl client that manages some external kubernetes cluster. 我正在使用Windows笔记本电脑,该笔记本电脑上安装了一个无所事事的盒子,那里有一个管理某些外部kubernetes群集的kubectl客户端。

For debugging purposes I would like to do a port-forwarding via kubectl and access this port from the host machine. 为了调试,我想通过kubectl进行端口转发,并从主机访问该端口。 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. 从内部游民到kubernetes集群这是完美的工作方式,但是很明显,从主机到游民的游民端口转发不起作用。

Here my setup: 这是我的设置:

  1. Port-Forwarding in Vagrant: 流浪汉的港口转运:

    config.vm.network "forwarded_port", guest: 8080, host: 8080, auto_correct:false

  2. start nginx container in kubernetes: 在kubernetes中启动Nginx容器:

    kubectl run -i -t --image nginx test

  3. forward port to localhost (inside vagrant): 转发端口到本地主机(在流浪汉内部):

    kubectl port-forward test-64585bfbd4-zxpsd 8080:80

  4. test nginx running inside vagrant-box: 测试在vagrant-box内运行的nginx:

     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. 作品。

  1. Now going a level up - on the windows host: 现在要升级-在Windows主机上:

     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? 您有什么主意,为什么它不能按预期工作?

By default, kubectl port-forward binds to the address 127.0.0.1 . 默认情况下, kubectl port-forward绑定到地址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 该解决方案是使kubectl port-forward绑定到0.0.0.0使用参数--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. kubectl port-forward绑定到127.0.0.1,不允许您定义绑定地址。 The traffic from your Windows host machine hits the main network interface of your Vagrant VM and therefore, this doesn't work. Windows主机的流量访问了Vagrant VM的主网络接口,因此这是行不通的。 You can fix the issue by routing traffic from the Vagrant VM's main network interface to the loopback interface using iptables : ` 您可以使用iptables通过将流量从Vagrant VM的主网络接口路由到回送接口来解决此问题:

  1. Forward traffic from your vagrant VM's main network interface to 127.0.0.1 (replace $PORT with the port you're forwarding): 将流量从无用的VM的主网络接口转发到127.0.0.1(将$PORT替换$PORT要转发的端口):
    $ $ iptables -t nat -I PREROUTING -p tcp --dport $PORT -j DNAT --to-destination 127.0.0.1:$PORT
  2. Look up the name of your Vagrant VM's main network interface: 查找您的Vagrant VM主网络接口的名称:
    $ ifconfig enp0s3 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)
  3. 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 above enp0s3 ): 由于默认情况下禁用将流量转发到回送接口,因此请启用转发到回送接口(在上面的示例中,将$MAIN_NETWORK_INTERFACE_NAME替换$MAIN_NETWORK_INTERFACE_NAME接口名称,例如enp0s3 ):
    sysctl -w net.ipv4.conf.$MAIN_NETWORK_INTERFACE_NAME.route_localnet=1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM