KVM创建多网卡桥接网络虚拟机

环境:centos7 关闭防火墙 selinux=disabled

设置:

(1)CPU开启虚拟化

(2)验证开启虚拟化 [root@kvm-server1 ~]# grep -E "vmx|svm" /proc/cpuinfo | wc -l

2

(3)安装KVM工具包

#yum install qemu-kvm qemu-kvm-tools libvirt libvirt- client virt-manager virt-install

# systemctl start libvirtd

# systemctl enable libvirtd

(4)创建桥接网卡 

[root@kvm-server1 network-scripts]# cat ifcfg-eth0
TYPE="Ethernet"
BOOTPROTO="static"
NAME="eth0"
DEVICE="eth0"
ONBOOT="yes"
BRIDGE=br0
[root@kvm-server1 network-scripts]# cat ifcfg-br0
TYPE=Bridge
BOOTPROTO=static
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=10.0.0.128
NETMASK=255.255.0.0
GATEWAY=10.0.0.2
DNS1=10.0.0.2

(5)上传镜像文件并创建虚拟机

[root@kvm-server1 network-scripts]# ll /data/ISOs/      #需要创建目录并上传镜像文件
total 4669440
-rw-r--r-- 1 qemu qemu 4781506560 Jun 7 21:16 CentOS-7-x86_64-DVD-2003.iso

#创建系统磁盘

[root@kvm-server1 network-scripts]#qemu-img create -f qcow2  /var/lib/libvirt/images/centos7-bridge.qcow2 10G

Formatting '/var/lib/libvirt/images/centos7-bridge.qcow2',fmt=qcow2 size=10737418240 cluster_size=65536 lazy_refcounts=off refcount_bits=16
#创建基于桥接网络的虚拟机

[root@kvm-server1 network-scripts]#virt-install \

--virt-type kvm \

--name centos7-bridge \      -n NAME, --name=NAME:虚拟机名称,需全局惟一; 

--ram 1024 \                       # --ram=MEMORY:虚拟机内在大小,单位为MB;

--vcpus 2 \                         #VCPU个数及相关配置;

--cdrom=/data/ISOs/CentOS-7-x86_64-DVD-2003.iso \                    #镜像源

--disk path=/var/lib/libvirt/images/centos7-bridge.qcow2 \                     #-disk=DISKOPTS:指定存储设备及其属性

--network bridge=br0 \                       # bridge=BRIDGE:连接至名为“BRIDEG”的桥设备;

--graphics vnc,listen=0.0.0.0 \          #图形配置:定义虚拟机显示功能相关的配置

--noautoconsole                              #--noautoconsole:禁止自动连接至虚拟机的控制台

[root@kvm-server1 network-scripts]#virt-manager

#一步步安装虚拟机就可以了,安装完reboot重启

#安装的虚拟机 

[root@localhost network-scripts]# cat ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=10.0.0.130
NETMASK=255.255.255.0
GATEWAY=10.0.0.2
DNS1=114.114.114.114
[root@localhost network-scripts]#

#设置多网卡

宿主机添加网卡,我这里设置为仅主机模式

#设置eth1

[root@kvm-server1 network-scripts]# cp ifcfg-eth0 ifcfg-eth1

[root@kvm-server1 network-scripts]# vim ifcfg-eth1

[root@kvm-server1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
TYPE="Ethernet"
BOOTPROTO="static"
NAME="eth1"
DEVICE="eth1"
ONBOOT="yes"
IPADDR=192.168.10.4
NETMASK=255.255.255.0
BRIDGE=br1
#设置br1

[root@kvm-server1 network-scripts]# cp ifcfg-br0 ifcfg-br1

[root@kvm-server1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-br1
TYPE=Bridge
BOOTPROTO=static
NAME=br1
DEVICE=br1
ONBOOT=yes
IPADDR=192.168.10.4
NETMASK=255.255.255.0

[root@kvm-server1 network-scripts]# systemctl restart network

[root@kvm-server1 network-scripts]# ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.128 netmask 255.255.0.0 broadcast 10.0.255.255
inet6 fe80::20c:29ff:fed3:e11c prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:d3:e1:1c txqueuelen 1000 (Ethernet)
RX packets 30627 bytes 3437251 (3.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 28475 bytes 7362182 (7.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

br1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.4 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::6088:67ff:fe8f:ca9e prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:d3:e1:26 txqueuelen 1000 (Ethernet)
RX packets 2352 bytes 221884 (216.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 11 bytes 782 (782.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:d3:e1:1c txqueuelen 1000 (Ethernet)
RX packets 33704 bytes 4189587 (3.9 MiB)
RX errors 0 dropped 20 overruns 0 frame 0
TX packets 30832 bytes 9059601 (8.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:d3:e1:26 txqueuelen 1000 (Ethernet)
RX packets 2385 bytes 256890 (250.8 KiB)
RX errors 0 dropped 19 overruns 0 frame 0
TX packets 71 bytes 6566 (6.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 54088 bytes 87109072 (83.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 54088 bytes 87109072 (83.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:d2:6d:fa txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

vnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet6 fe80::fc54:ff:fe25:6246 prefixlen 64 scopeid 0x20<link>
ether fe:54:00:25:62:46 txqueuelen 1000 (Ethernet)
RX packets 587 bytes 79819 (77.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2293 bytes 285786 (279.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

vnet1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet6 fe80::fc54:ff:feff:ec00 prefixlen 64 scopeid 0x20<link>
ether fe:54:00:ff:ec:00 txqueuelen 1000 (Ethernet)
RX packets 45 bytes 4114 (4.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2292 bytes 248960 (243.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

[root@kvm-server1 network-scripts]# virt-manager

#这个时候虚拟机就可以添加网卡了,重启

[root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-eth1

[root@localhost network-scripts]# cat ifcfg-eth1
TYPE=Ethernet
BOOTPROTO=static
NAME=eth1
DEVICE=eth1
ONBOOT=yes
IPADDR=192.168.10.5
NETMASK=255.255.255.0

[root@localhost network-scripts]# 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=16.1 ms
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=2 ttl=128 time=13.8 ms
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=3 ttl=128 time=29.3 ms

posted @ 2021-06-08 16:46  一动不动的咸鱼  阅读(1610)  评论(0编辑  收藏  举报