繁体   English   中英

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

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

我正在使用Windows笔记本电脑,该笔记本电脑上安装了一个无所事事的盒子,那里有一个管理某些外部kubernetes群集的kubectl客户端。

为了调试,我想通过kubectl进行端口转发,并从主机访问该端口。 从内部游民到kubernetes集群这是完美的工作方式,但是很明显,从主机到游民的游民端口转发不起作用。

这是我的设置:

  1. 流浪汉的港口转运:

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

  2. 在kubernetes中启动Nginx容器:

    kubectl run -i -t --image nginx test

  3. 转发端口到本地主机(在流浪汉内部):

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

  4. 测试在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> 

作品。

  1. 现在要升级-在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 

无效。

根据我的理解-仅查看端口转发就可以了。 您有什么主意,为什么它不能按预期工作?

默认情况下, kubectl port-forward绑定到地址127.0.0.1 这就是为什么您无法在无业游民的情况下访问它。 该解决方案是使kubectl port-forward绑定到0.0.0.0使用参数--address 0.0.0.0

运行命令:

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

将解决您的问题。

kubectl port-forward绑定到127.0.0.1,不允许您定义绑定地址。 Windows主机的流量访问了Vagrant VM的主网络接口,因此这是行不通的。 您可以使用iptables通过将流量从Vagrant VM的主网络接口路由到回送接口来解决此问题:

  1. 将流量从无用的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. 查找您的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. 由于默认情况下禁用将流量转发到回送接口,因此请启用转发到回送接口(在上面的示例中,将$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