linux kvm bridge

  • Post author:
  • Post category:linux


物理主机上有5台kvm虚拟机,需要通过网桥的方式使其与外界通信。为了达到这个目的,需要将5台虚拟机的虚拟网卡(vnet0-vnet4)以及物理主机对外通信的物理网卡(这里为eth0)连接到同一个网桥(br0)上。物理主机操作系统为rhel6.3,虚拟机操作系统为rhel5.6。下面将记录如何通过创建网桥的方式来使kvm虚拟机与外界通信。


注意:

以下的操作可能导致网络中断,请在物理主机操作。


vhost NIC          Bridge       Phy NIC


|vnet0|


|vnet1|


|vnet2|———–>|br0|——->| eth0 |——>LAN


|vnet3|


|vnet4|


通过命令行方式来创建网桥

1.检查是否安装了相关的包


rpm -qa | grep bridge-utils

如果未安装,可以使用yum或者直接rpm的方式进行安装。


yum install bridge-utils



关闭NetworkManager服务


/etc/init.d/NetworkManager stop


chkconfig –level 35 NetworkManager off



检查网络配置


[root@node2 ~]# ifconfig

eth0   Link encap:Ethernet  HWaddr D4:8C:B5:BC:04:68

inet addr:192.168.10.51  Bcast:10.190.105.255  Mask:255.255.255.0

inet6 addr: fe80::d68c:b5ff:febc:468/64 Scope:Link

UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

RX packets:2719 errors:0 dropped:0 overruns:0 frame:0

TX packets:200 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0

RX bytes:434192 (424.0 KiB)  TX bytes:24782 (24.2 KiB)


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:16436  Metric:1

RX packets:79957 errors:0 dropped:0 overruns:0 frame:0

TX packets:79957 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0

RX bytes:104719505 (99.8 MiB)  TX bytes:104719505 (99.8 MiB)


virbr0    Link encap:Ethernet  HWaddr 52:54:00:A4:20:F0

inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0

UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

RX packets:3389 errors:0 dropped:0 overruns:0 frame:0

TX packets:13814 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0

RX bytes:370195 (361.5 KiB)  TX bytes:875398 (854.8 KiB)


vnet0     Link encap:Ethernet  HWaddr FE:54:00:F1:41:92

inet6 addr: fe80::fc54:ff:fef1:4192/64 Scope:Link

UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

RX packets:210 errors:0 dropped:0 overruns:0 frame:0

TX packets:4581 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:500

RX bytes:10757 (10.5 KiB)  TX bytes:566977 (553.6 KiB)


vnet1     Link encap:Ethernet  HWaddr FE:54:00:42:CC:18

inet6 addr: fe80::fc54:ff:fe42:cc18/64 Scope:Link

UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

RX packets:84 errors:0 dropped:0 overruns:0 frame:0

TX packets:4676 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:500

RX bytes:5564 (5.4 KiB)  TX bytes:572720 (559.2 KiB)


vnet2     Link encap:Ethernet  HWaddr FE:54:00:5D:EB:0C

inet6 addr: fe80::fc54:ff:fe5d:eb0c/64 Scope:Link

UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

RX packets:209 errors:0 dropped:0 overruns:0 frame:0

TX packets:4562 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:500

RX bytes:10711 (10.4 KiB)  TX bytes:566179 (552.9 KiB)


vnet3     Link encap:Ethernet  HWaddr FE:54:00:9F:08:C5

inet6 addr: fe80::fc54:ff:fe9f:8c5/64 Scope:Link

UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

RX packets:211 errors:0 dropped:0 overruns:0 frame:0

TX packets:4545 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:500

RX bytes:10841 (10.5 KiB)  TX bytes:565419 (552.1 KiB)


vnet4     Link encap:Ethernet  HWaddr FE:54:00:E7:47:45

inet6 addr: fe80::fc54:ff:fee7:4745/64 Scope:Link

UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

RX packets:211 errors:0 dropped:0 overruns:0 frame:0

TX packets:4549 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:500

RX bytes:10841 (10.5 KiB)  TX bytes:565587 (552.3 KiB)


[root@node2 ~]#

检查虚拟机


[root@node2 ~]# virsh list

Id    Name                           State

—————————————————-

10    node1                          running

11    node2                          running

12    node3                          running

13    node4                          running

14    node5                          running


2.创建名称为br0的网桥


brctl addbr br0


为br0创建配置文件,使其永久生效


vi /etc/sysconfig/network-scripts/ifcfg-br0


DEVICE=br0

ONBOOT=yes

TYPE=Bridge

BOOTPROTO=none

IPADDR=10.190.105.10

NETMASK=255.255.255.0

STP=on

DELAY=0


使用命令行方式把虚拟网卡(vnet0-vnet4)和物理网卡(eth0)加入到br0


brctl addif br0 vnet0


brctl addif br0 vnet1


brctl addif br0 vnet2


brctl addif br0 vnet3


brctl addif br0 vnet4


brctl addif br0 eth0


[root@node2 ~]# brctl show

bridge name     bridge id               STP enabled     interfaces

br0             8000.d48cb5bc0468       yes             eth0

vnet0

vnet1

vnet2

vnet3

vnet4

virbr0          8000.525400a420f0       yes             virbr0-nic

[root@node2 ~]#

编辑eth0网卡配置文件,为eth0指定网桥


vi /etc/sysconfig/network-scripts/ifcfg-eth0


DEVICE=eth0

HWADDR=90:b1:1c:11:58:d1

ONBOOT=yes

BRIDGE=br0


重启网络服务


/etc/init.d/network restart


[root@node2 ~]# ifconfig

br0       Link encap:Ethernet  HWaddr D4:8C:B5:BC:04:68

inet addr:192.168.10.51 Bcast:10.190.105.255  Mask:255.255.255.0

inet6 addr: fe80::d68c:b5ff:febc:468/64 Scope:Link

UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

RX packets:3032 errors:0 dropped:0 overruns:0 frame:0

TX packets:259 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0

RX bytes:484624 (473.2 KiB)  TX bytes:35056 (34.2 KiB)


eth0      Link encap:Ethernet  HWaddr D4:8C:B5:BC:04:68

inet6 addr: fe80::d68c:b5ff:febc:468/64 Scope:Link

UP BROADCAST RUNNING PROMISC MULTICAST  MTU:1500  Metric:1

RX packets:562 errors:0 dropped:0 overruns:0 frame:0

TX packets:511 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:97253 (94.9 KiB)  TX bytes:75599 (73.8 KiB)

Interrupt:40 Memory:90000000-90012800


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:16436  Metric:1

RX packets:79969 errors:0 dropped:0 overruns:0 frame:0

TX packets:79969 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0

RX bytes:104720105 (99.8 MiB)  TX bytes:104720105 (99.8 MiB)


virbr0    Link encap:Ethernet  HWaddr 52:54:00:A4:20:F0

inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0

UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

RX packets:3389 errors:0 dropped:0 overruns:0 frame:0

TX packets:13817 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0

RX bytes:370195 (361.5 KiB)  TX bytes:875536 (855.0 KiB)


vnet0     Link encap:Ethernet  HWaddr FE:54:00:F1:41:92

inet6 addr: fe80::fc54:ff:fef1:4192/64 Scope:Link

UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

RX packets:215 errors:0 dropped:0 overruns:0 frame:0

TX packets:5011 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:500

RX bytes:10987 (10.7 KiB)  TX bytes:625973 (611.3 KiB)


vnet1     Link encap:Ethernet  HWaddr FE:54:00:42:CC:18

inet6 addr: fe80::fc54:ff:fe42:cc18/64 Scope:Link

UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

RX packets:89 errors:0 dropped:0 overruns:0 frame:0

TX packets:5106 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:500

RX bytes:5794 (5.6 KiB)  TX bytes:631716 (616.9 KiB)


vnet2     Link encap:Ethernet  HWaddr FE:54:00:5D:EB:0C

inet6 addr: fe80::fc54:ff:fe5d:eb0c/64 Scope:Link

UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

RX packets:214 errors:0 dropped:0 overruns:0 frame:0

TX packets:4992 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:500

RX bytes:10941 (10.6 KiB)  TX bytes:625175 (610.5 KiB)


vnet3     Link encap:Ethernet  HWaddr FE:54:00:9F:08:C5

inet6 addr: fe80::fc54:ff:fe9f:8c5/64 Scope:Link

UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

RX packets:216 errors:0 dropped:0 overruns:0 frame:0

TX packets:4975 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:500

RX bytes:11071 (10.8 KiB)  TX bytes:624415 (609.7 KiB)


vnet4     Link encap:Ethernet  HWaddr FE:54:00:E7:47:45

inet6 addr: fe80::fc54:ff:fee7:4745/64 Scope:Link

UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

RX packets:216 errors:0 dropped:0 overruns:0 frame:0

TX packets:4979 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:500

RX bytes:11071 (10.8 KiB)  TX bytes:624583 (609.9 KiB)

测试


[root@node2 ~]# ping 192.168.10.1

PING 192.168.10.1 (192.168.10.1) 56(84) bytes of data.

64 bytes from 192.168.10.1: icmp_seq=1 ttl=128 time=0.237 ms

64 bytes from 192.168.10.1: icmp_seq=2 ttl=128 time=0.259 ms

^C

— 192.168.10.1 ping statistics —

2 packets transmitted, 2 received, 0% packet loss, time 1351ms

rtt min/avg/max/mdev = 0.237/0.248/0.259/0.011 ms



版权声明:本文为ww1982_0_0_0原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。