pee*_*z80 5 vagrant kubernetes vagrant-windows kubectl
我正在使用安装了 vagrant box 的 Windows 笔记本电脑,在那里我有一个 kubectl 客户端来管理一些外部 kubernetes 集群。
出于调试目的,我想通过 kubectl 进行端口转发并从主机访问此端口。这从 vagrant 内部到 kubernetes 集群都可以完美运行,但显然有些东西与从主机到 vagrant 的 vagrant 端口转发一起不起作用。
这是我的设置:
Vagrant 中的端口转发:
config.vm.network "forwarded_port", guest: 8080, host: 8080, auto_correct:false
在 Kubernetes 中启动 nginx 容器:
kubectl run -i -t --image nginx test
将端口转发到本地主机(在 vagrant 内部):
kubectl port-forward test-64585bfbd4-zxpsd 8080:80
测试在 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>
Run Code Online (Sandbox Code Playgroud)作品。
现在更上一层楼 - 在 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
Run Code Online (Sandbox Code Playgroud)不工作。
根据我的理解 - 只看端口转发,一切都应该没问题。你有什么想法为什么这不像预期的那样工作吗?
默认情况下,kubectl port-forward绑定到地址127.0.0.1。这就是为什么您无法在 vagrant 之外访问它的原因。解决方案是使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
Run Code Online (Sandbox Code Playgroud)
将解决您的问题。
小智 0
kubectl port-forward绑定到 127.0.0.1 并且不允许您定义绑定地址。来自 Windows 主机的流量会到达 Vagrant VM 的主网络接口,因此这不起作用。您可以通过使用以下命令将流量从 Vagrant VM 的主网络接口路由到环回接口来解决此问题iptables:
$PORT为您要转发的端口):$ $ iptables -t nat -I PREROUTING -p tcp --dport $PORT -j DNAT --to-destination 127.0.0.1:$PORT$ 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) $MAIN_NETWORK_INTERFACE_NAME在上例中替换为接口名称enp0s3):sysctl -w net.ipv4.conf.$MAIN_NETWORK_INTERFACE_NAME.route_localnet=1| 归档时间: |
|
| 查看次数: |
1183 次 |
| 最近记录: |