将流量从Pod转发到在localhost上运行的服务 [英] Forwarding traffic from pod to service running on localhost
问题描述
我的k8集群在minikube上运行.
我熟悉kubectl port-forward命令,该命令允许将流量从本地主机路由到群集.
有没有其他方法可以做到?我可以将流量从其中一个Pod路由到在我的计算机上本地运行的Web服务器吗?
从 minikube
吊舱到主机的连接方式在很大程度上取决于-driver code>您使用过的.
每个-driver
可能会更改从Pod到主机的连接方式.我的意思是,每个-driver
可能有多个选项可以连接到主机.
用户@Srikrishna B H指出
在连接到本地运行的Web服务器时,请确保使用本地计算机IP而不是
localhost
.
我创建了3个示例:
- 带有
minikube start --driver = docker
的Ubuntu - 具有
minikube start --driver = hyperkit
的Mac OS - 带有
minikube start --driver = virtualbox
的Windows
下面使用的IP地址仅用于示例目的!
Ubuntu与Docker
假设您有一台安装了Docker的Ubuntu计算机,并且 nginx
用作Pod将连接到的服务器:
-
$ minikube start --driver = docker
-
$ ip addr show
:-
docker0
-docker接口 -
ensX
-物理"界面
-
上面的命令会告诉您要从您的Pod(主机上)连接的IP地址
2:ensX:< BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1460 qdisc mq状态UP组默认qlen 1000< ---->inet 10.0.0.2/32范围全局动态ensX< ---->
<代码> 3:docker0:< BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdisc noqueue状态UP组默认< ---->inet 172.17.0.1/16 brd 172.17.255.255作用域全局docker0< ---->
然后,您可以生成一个Pod,以检查是否可以连接到Nginx:
-
$ kubectl运行-i --tty busybox --image = busybox --restart =从不-sh
-
$ wget -q0-IP_ADDRESS
-busybox
图片未安装curl
-
172.17.0.1
-
10.0.0.2
-
带有Hyperkit的Mac OS
假设您有一台Mac OS计算机,并且已在端口 8080
上配置了 nginx
:
-
$ minikube start --driver = hyperkit
-
$ ifconfig
:-
bridge100
-
enX
-物理"界面
-
免责声明!
您将需要允许连接到防火墙中
localhost
之外的nginx或完全禁用它(不推荐!)
bridge100:flags = 8a63< UP,BROADCAST,SMART,RUNNING,ALLMULTI,SIMPLEX,MULTICAST>mtu 1500< ---->inet 192.168.64.1网络掩码0xffffff00广播192.168.64.255< ---->
enX:flags = 8863< UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST>mtu 1500< ---->inet 192.168.1.101网络掩码0xffffff00广播192.168.0.255< ---->
然后,您可以生成一个Pod,以检查是否可以连接到Nginx:
-
$ kubectl运行-i --tty busybox --image = busybox --restart =从不-sh
-
$ wget -q0-IP_ADDRESS
-busybox
图片未安装curl
-
192.168.64.1:8080
-
192.168.1.101:8080
-
带有Virtualbox的Windows
在Windows中使用-driver = virtualbox
创建 minikube
实例时,它将创建具有2个网络接口的 VM
:>
-
NAT
-用于与外部(Internet)通信 -
Virtualbox仅限主机的以太网适配器
-用于在minikube
与您的主机之间进行通信
假设您有一台装有Virtualbox的Windows机器,则已在端口 80
上配置了 nginx
,并且还具有一个正在运行的 minikube
实例.
您将需要获取( $ ipconfig
)的IP地址:
- 您的身体"介面(例如
Ethernet X
) - 您的
Virtualbox仅限主机的以太网适配器
以太网适配器以太网X:< ---->IPv4地址...........:192.168.1.3子网掩码 ...........:255.255.255.0默认网关 .........:192.168.0.1
以太网适配器VirtualBox仅限主机网络#X:< --->IPv4地址...........:192.168.99.1子网掩码 ...........:255.255.255.0默认网关 .........:
免责声明!
您需要检查防火墙是否会接受发往ngin的流量(如果未阻止).
然后,您可以生成一个Pod,以检查是否可以连接到Nginx:
-
$ kubectl运行-i --tty busybox --image = busybox --restart =从不-sh
-
$ wget -q0-IP_ADDRESS
-busybox
图片未安装curl
-
192.168.99.1
-
192.168.1.3
-
My k8 cluster runs on minikube.
I am familiar with kubectl port-forward command which allows to route traffic from localhost into the cluster.
Is there a way do do it the other way around? Can I route the traffic from one of the pods to the web server that runs locally on my machine?
The way to connect from your minikube
pod to your host will heavily depend on the type of --driver
you used.
Each --driver
could alter the way to connect from your pod to your host. What I mean is that there could be multiple options for each --driver
to connect to your host.
As pointed by user @Srikrishna B H
Make sure you use local machine IP instead of
localhost
while connecting to web server running locally in your machine.
I've created 3 examples:
- Ubuntu with
minikube start --driver=docker
- Mac OS with
minikube start --driver=hyperkit
- Windows with
minikube start --driver=virtualbox
IP addresses used below are only for example purposes!
Ubuntu with Docker
Assuming that you have an Ubuntu machine with Docker installed and nginx
working as a server that your pod will connect to:
$ minikube start --driver=docker
$ ip addr show
:docker0
- docker interfaceensX
- "physical" interface
Above command will tell you the IP address to connect from your pod (on host)
2: ensX: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1460 qdisc mq state UP group default qlen 1000
<---->
inet 10.0.0.2/32 scope global dynamic ensX
<---->
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
<---->
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
<---->
Then you can spawn a pod to check if you can connect to your nginx:
$ kubectl run -i --tty busybox --image=busybox --restart=Never -- sh
$ wget -q0 - IP_ADDRESS
-busybox
image doesn't havecurl
installed172.17.0.1
10.0.0.2
Mac OS with Hyperkit
Assuming that you have a Mac OS machine and you have configured nginx
on port 8080
:
$ minikube start --driver=hyperkit
$ ifconfig
:bridge100
enX
- "physical" interface
Disclaimer!
You will need to allow connections to your nginx outside of
localhost
in the firewall or completely disable it (not recommended!)
bridge100: flags=8a63<UP,BROADCAST,SMART,RUNNING,ALLMULTI,SIMPLEX,MULTICAST> mtu 1500
<---->
inet 192.168.64.1 netmask 0xffffff00 broadcast 192.168.64.255
<---->
enX: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
<---->
inet 192.168.1.101 netmask 0xffffff00 broadcast 192.168.0.255
<---->
Then you can spawn a pod to check if you can connect to your nginx:
$ kubectl run -i --tty busybox --image=busybox --restart=Never -- sh
$ wget -q0 - IP_ADDRESS
-busybox
image doesn't havecurl
installed192.168.64.1:8080
192.168.1.101:8080
Windows with Virtualbox
When you create a minikube
instance with --driver=virtualbox
in Windows, it creates a VM
with 2 network interfaces:
NAT
- used to communicate with outside (Internet)Virtualbox Host-Only Ethernet Adapter
- used to communicate betweenminikube
and your host
Assuming that you have a Windows machine with Virtualbox, you have configured nginx
on port 80
and also you have a running minikube
instance.
You will need to get the IP addresses of ($ ipconfig
) :
- Your "physical" interface (
Ethernet X
for example) - Your
Virtualbox Host-Only Ethernet Adapter
Ethernet adapter Ethernet X:
<---->
IPv4 Address. . . . . . . . . . . : 192.168.1.3
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.0.1
Ethernet adapter VirtualBox Host-Only Network #X:
<--->
IPv4 Address. . . . . . . . . . . : 192.168.99.1
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :
Disclaimer!
You will need check if your firewall will accept the traffic destined for ngin (If it's not blocked).
Then you can spawn a pod to check if you can connect to your nginx:
$ kubectl run -i --tty busybox --image=busybox --restart=Never -- sh
$ wget -q0 - IP_ADDRESS
-busybox
image doesn't havecurl
installed192.168.99.1
192.168.1.3
这篇关于将流量从Pod转发到在localhost上运行的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!