您好,我的docker容器内多网卡间可以ping通,即使这些网卡不在一个网段,我想请问一下您如何隔离同一个docker容器内的不同网卡,让它们不能互ping?
您可以使用iptables来隔离不同网卡之间的通信,您可以使用iptables的FORWARD链来阻止不同网卡之间的通信,例如:
iptables -A FORWARD -i eth0 -o eth1 -j DROP
这条命令会阻止从eth0发出的数据包到达eth1,从而实现不同网卡之间的隔离。
server1:
添加网卡配置文件:
[root@server1 network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@server1 network-scripts]# vim ifcfg-eth1
[root@server1 network-scripts]# cat ifcfg-eth1
BOOTPROTO=none
DEVICE=eth1
ONBOOT=yes
[root@server1 network-scripts]# ifup eth1 #启动网卡
server2:
添加网卡配置文件:
[root@server2 ~]# cd /etc/sysconfig/network-scripts/
[root@server2 network-scripts]# vim ifcfg-eth1
[root@server2 network-scripts]# cat ifcfg-eth1
BOOTPROTO=none
DEVICE=eth1
ONBOOT=yes
[root@server2 network-scripts]# ifup eth1
macvlan本身是linxu kernel的模块,本质上是一种网卡虚拟化技术。其功能是允许在同一个物理网卡上虚拟出多个网卡,通过不同的MAC地址在数据链路层进行网络数据的转发,一块网卡上配置多个 MAC 地址(即多个 interface),每个interface可以配置自己的IP,Docker的macvlan网络实际上就是使用了Linux提供的macvlan驱 动。
因为多个MAC地址的网络数据包都是从同一块网卡上传输,所以需要打开网卡的混杂模式ip link set eth0 promisc on。
[root@server1 network-scripts]# ip link set eth1 promisc on #在server1上打开网卡eth0的混杂模式
[root@server2 network-scripts]# ip link set eth1 promisc on
看到PROMISC,表示成功
注意 : 如果不开启混杂模式,会导致macvlan网络无法访问外界,具体在不使用vlan时,表现为无法ping通路由,无法ping通同一网络内其他主机。