在RedHat7上搭了一台KVM服务器,创建好虚拟机之后发现,外部电脑与KVM服务器、外部机器与VM虚拟机、两台VM虚拟机之间都可以通讯,但是KVM服务器与VM虚拟机却无法通讯。此时外部电脑、KVM服务器、VM虚拟机都关闭了防火墙。
此时VM虚拟机的网卡配置如图:选择的源设备为 Host device eno1 :macvtap
[root@kvm02 ~]# ifconfig eno1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.254 netmask 255.255.255.0 broadcast 192.168.1.255 ........ eno2: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 ........ lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 ........ macvtap0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ........ macvtap1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ........ virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 查看网桥信息 [root@kvm02 ~]# brctl show bridge name bridge id STP enabled interfaces virbr0 8000.525400c2d3bd yes virbr0-nic
查看网卡信息后发现现在存在物理网卡eno1、eno2 网桥virbr0、虚拟网卡macvtap0、macvtap1。
网桥virbr0,相当于VMware的 VMNET8,提供NAT的网卡,当有虚拟机网卡使用桥接模式并且启动时,使用#brctl show 查看,在virbr0会有的interfaces下会出现网卡vnetX。
虚拟网卡macvtap0、macvtap1,是现在VM虚拟机的网卡,此时我启动了两台虚拟机,网卡配置如上文。
但此时虚拟机跟宿主机是无法通讯的。
[root@kvm02 ~]# ping 192.168.1.10 PING 192.168.1.10 (192.168.1.10) 56(84) bytes of data. From 192.168.1.254 icmp_seq=1 Destination Host Unreachable From 192.168.1.254 icmp_seq=2 Destination Host Unreachable From 192.168.1.254 icmp_seq=3 Destination Host Unreachable
现在开始创建网桥,并把eno1,虚拟机的网卡接入网桥。
[root@kvm02 ~]# virsh iface-bridge eno1 br0 Created bridge br0 with attached device eno1
使用virsh iface-bridge创建网桥,此时我是ssh到KVM服务器的,添加之后断开了连接,去到物理机前重启网卡,发现有了网桥br0,但是网桥没有成员,查看message日志发现eno1已经是一个网桥成员了。
[root@kvm02 network-scripts]# brctl show bridge name bridge id STP enabled interfaces br0 8000.000000000000 no virbr0 8000.525400c2d3bd yes virbr0-nic message日志 Jan 19 05:09:34 kvm02 network: Bringing up interface eno1: device eno1 is already a member of a bridge; can't enslave it to bridge br0.
折腾一番后发现,启动的两台虚拟机绑定在eno1上,所以暂时先关掉绑定到这块网卡的虚拟机,删除网卡然后重建网桥。
[root@kvm02 ~]# virsh iface-bridge eno1 br0 Created bridge br0 with attached device eno1 此时又断开远程了。重新回到机器前面重启网卡 [root@kvm02 ~]# systemctl restart network 重启第一次的时候报错了,然后多重启两次,发现没有出现错误,网卡正常启动,eno1成功加入br0了。 [root@kvm02 ~]# brctl show bridge name bridge id STP enabled interfaces br0 8000.0024e8584bc8 yes eno1 virbr0 8000.525400c2d3bd yes virbr0-nic
网卡eno1成功加入网桥之后,修改虚拟机的网卡配置。把网卡设备修改为:Bridge br0:Host device eno1,如下图:
修改完成后启动虚拟机,发现虚拟机的网卡也加入了br0,此时虚拟机的网卡名由macvtapX变成了vnetX。启动完成后,虚拟机与宿主机就可以正常通讯了。
[root@kvm02 ~]# ifconfig br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.254 netmask 255.255.255.0 broadcast 192.168.1.255 ........ eno1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ........ eno2: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 ........ lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 ........ virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 ........ vnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ........ vnet1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ........ [root@kvm02 ~]# brctl show bridge name bridge id STP enabled interfaces br0 8000.0024e8584bc8 yes eno1 vnet0 virbr0 8000.525400c2d3bd yes virbr0-nic [root@kvm02 ~]# ping 192.168.1.10 PING 192.168.1.10 (192.168.1.10) 56(84) bytes of data. 64 bytes from 192.168.1.10: icmp_seq=1 ttl=64 time=0.676 ms 64 bytes from 192.168.1.10: icmp_seq=2 ttl=64 time=0.228 ms
对于网桥,可以大致理解为一个虚拟的交换机,把网卡接到同一个交换机上面,各个主机就可以正常通讯了。