您现在的位置是:首页 >技术交流 >KVM网络管理-创建桥接网络网站首页技术交流

KVM网络管理-创建桥接网络

Rkun18 2024-06-17 10:29:49
简介KVM网络管理-创建桥接网络

KVM网络管理-创建桥接网络

分类

网络

  • nat

  • isolated

接口

  • bridge

虚拟交换机

  • linux-bridge(linux)

  • ovs(open-Vswitch)

NAT网络拓扑图
在这里插入图片描述

仅主机模式网络拓扑图
在这里插入图片描述

桥接模式网络拓扑图

在这里插入图片描述

在Linux主机上开启vm1,从交换机上把vnet网卡删除

首先查看虚拟交换机

[root@myserver ~]# brctl show
bridge name     bridge id               STP enabled     interfaces
virbr0          8000.5254000760f5       yes             virbr0-nic
                                                        vnet0

测试vm1能否ping通外网

在这里插入图片描述

删除网卡

[root@myserver ~]# brctl delif virbr0 vnet0
[root@myserver ~]# brctl show
bridge name     bridge id               STP enabled     interfaces
virbr0          8000.5254000760f5       yes             virbr0-nic

去虚拟机vm1里查看能否ping通

在这里插入图片描述

把vnet0添加到virbr0交换机里,虚拟机vm1能否ping通?

[root@myserver ~]# brctl addif virbr0 vnet0
[root@myserver ~]# brctl show
bridge name     bridge id               STP enabled     interfaces
virbr0          8000.5254000760f5       yes             virbr0-nic
                                                        vnet0

在这里插入图片描述

配置文件方式配置桥接

在操作之前关闭GuestOS主机

查看宿主机IP地址

      [root@myserver ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:8c:e3:7a brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.126/24 brd 192.168.10.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::1327:e13c:8386:825b/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:07:60:f5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.124.1/24 brd 192.168.124.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:07:60:f5 brd ff:ff:ff:ff:ff:ff

进入存放网卡配置文件的目录中

[root@myserver ~]# cd /etc/sysconfig/network-scripts/
[root@myserver network-scripts]# ls
ifcfg-ens33  ifdown-ib    ifdown-ppp       ifdown-tunnel  ifup-ib    ifup-plusb   ifup-Team         network-functions
ifcfg-lo     ifdown-ippp  ifdown-routes    ifup           ifup-ippp  ifup-post    ifup-TeamPort     network-functions-ipv6
ifdown       ifdown-ipv6  ifdown-sit       ifup-aliases   ifup-ipv6  ifup-ppp     ifup-tunnel
ifdown-bnep  ifdown-isdn  ifdown-Team      ifup-bnep      ifup-isdn  ifup-routes  ifup-wireless
ifdown-eth   ifdown-post  ifdown-TeamPort  ifup-eth       ifup-plip  ifup-sit     init.ipv6-global

首先确认IP地址,使用route查看网关

[root@myserver ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 ens33
192.168.10.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.124.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0


[root@myserver ~]# ip route show default
default via 192.168.10.2 dev ens33 proto static metric 100
192.168.10.0/24 dev ens33 proto kernel scope link src 192.168.10.126 metric 100
192.168.124.0/24 dev virbr0 proto kernel scope link src 192.168.124.1


[root@myserver network-scripts]# vim ifcfg-br0
[root@myserver network-scripts]# cat ifcfg-br0

TYPE=Bridge
NAME=br0
DEVICE=br0
ONBOOT="yes"
BOOTPROTO=static
IPADDR=192.168.10.126  #宿主机IP地址
GATEWAY=192.168.10.2  #默认网关
NETMASK=255.255.255.0
DNS1=114.114.114.114
DNS2=8.8.8.8

修改物理机网卡配置文件,首先备份信息

[root@myserver network-scripts]# cp ifcfg-ens33 ifcfg-ens33.bak
[root@myserver network-scripts]# ls
ifcfg-br0        ifdown-bnep  ifdown-isdn    ifdown-Team      ifup-bnep  ifup-isdn   ifup-routes    ifup-wireless
ifcfg-ens33      ifdown-eth   ifdown-post    ifdown-TeamPort  ifup-eth   ifup-plip   ifup-sit       init.ipv6-global
ifcfg-ens33.bak  ifdown-ib    ifdown-ppp     ifdown-tunnel    ifup-ib    ifup-plusb  ifup-Team      network-functions
ifcfg-lo         ifdown-ippp  ifdown-routes  ifup             ifup-ippp  ifup-post   ifup-TeamPort  network-functions-ipv6
ifdown           ifdown-ipv6  ifdown-sit     ifup-aliases     ifup-ipv6  ifup-ppp    ifup-tunnel
[root@myserver network-scripts]# vim ifcfg-ens33
[root@myserver network-scripts]# cat ifcfg-ens33
 
  
DEVICE="ens33"
ONBOOT="yes"
BRIDGE=br0  #将物理网卡桥接到桥接网卡br0

重启服务

 systemctl restart libvirtd
 systemctl restart network


在vm1添加网卡,运行vm1
在这里插入图片描述

查看vm1网卡

在这里插入图片描述

网卡与宿主机在同一网段

查看宿主机网卡


[root@myserver network-scripts]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP group default qlen 1000
    link/ether 00:0c:29:8c:e3:7a brd ff:ff:ff:ff:ff:ff
3: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 52:54:00:07:60:f5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.124.1/24 brd 192.168.124.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:07:60:f5 brd ff:ff:ff:ff:ff:ff
5: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:0c:29:8c:e3:7a brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.126/24 brd 192.168.10.255 scope global noprefixroute br0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe8c:e37a/64 scope link
       valid_lft forever preferred_lft forever
6: vnet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master virbr0 state UNKNOWN group default qlen 1000
    link/ether fe:54:00:26:47:ff brd ff:ff:ff:ff:ff:ff
    inet6 fe80::fc54:ff:fe26:47ff/64 scope link
       valid_lft forever preferred_lft forever
7: vnet1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UNKNOWN group default qlen 1000
    link/ether fe:54:00:b0:f4:8f brd ff:ff:ff:ff:ff:ff
    inet6 fe80::fc54:ff:feb0:f48f/64 scope link
       valid_lft forever preferred_lft forever

同时桥接之后虚拟机vm1可以通过外部SSH链接

在这里插入图片描述

移除网卡

对vm1进行移除网卡

在这里插入图片描述

宿主机操作

[root@myserver network-scripts]# rm -rf ifcfg-br0
[root@myserver network-scripts]# rm -rf ifcfg-ens33
[root@myserver network-scripts]# mv ifcfg-ens33.bak ifcfg-ens33
[root@myserver network-scripts]# ls
ifcfg-ens33  ifdown-ib    ifdown-ppp       ifdown-tunnel  ifup-ib    ifup-plusb   ifup-Team         network-functions
ifcfg-lo     ifdown-ippp  ifdown-routes    ifup           ifup-ippp  ifup-post    ifup-TeamPort     network-functions-ipv6
ifdown       ifdown-ipv6  ifdown-sit       ifup-aliases   ifup-ipv6  ifup-ppp     ifup-tunnel
ifdown-bnep  ifdown-isdn  ifdown-Team      ifup-bnep      ifup-isdn  ifup-routes  ifup-wireless
ifdown-eth   ifdown-post  ifdown-TeamPort  ifup-eth       ifup-plip  ifup-sit     init.ipv6-global
[root@myserver network-scripts]# systemctl restart libvirtd
[root@myserver network-scripts]# systemctl restart network


查看宿主机网络信息,测试


[root@myserver ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:8c:e3:7a brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.126/24 brd 192.168.10.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::1327:e13c:8386:825b/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:07:60:f5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.124.1/24 brd 192.168.124.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:07:60:f5 brd ff:ff:ff:ff:ff:ff
[root@myserver ~]# ping www.baidu.com
PING www.a.shifen.com (110.242.68.3) 56(84) bytes of data.
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=1 ttl=128 time=32.3 ms
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=2 ttl=128 time=36.3 ms
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=3 ttl=128 time=32.8 ms
^C
--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 32.303/33.849/36.376/1.801 ms

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。