六,管理虚拟网络
- [x] Linux网桥基本概念
- [x] qemu-kvm支持的网络
- [x] 向虚拟机添加虚拟网络连接
- [x] 基于NAT的虚拟网络
- [x] 基于网桥的虚拟网络
- [x] 用户自定义的隔离的虚拟网络
6.1 Linux网桥与qemu-kvm支持的网络
Linux网桥基本概念
- [x] 数据链路的设备,基于MAC地址进行转发
- [x] Redhat/CentOS配置网桥常用方法
- 命令行(推荐)
- nmtui:NetworkManager的文本用户接口
- nmcli:NetworkManager的命令行工具
# nmcli con add type bridge ifname br0
# nmcli con show
- 图形界面管理工具
qemu-kvm支持的网络
- [x] 虚拟机的网络模式:
- 基于NAT(NetworkAddressTranslation)的虚拟网络
- 基于网桥(Bridge)的虚拟网络
- 用户自定义的隔离的虚拟网络
- 直接分配网络设备(包括VT-d和SR-IOV)
- [x] 虚拟机的网卡:
- RTL8139,e1000,....
- virtio
# /usr/libexec/qemu-kvm -net nic,mode1=?
演示:考察默认的虚拟网络的配置
- [x] 查看宿主机的网络配置
- [x] 查看虚拟机的网络配置
#qemu-kvm的虚拟网络配置文件在哪?
[root@localhost ~]# ls /etc/libvirt/ #libvirt的所有配置文件目录
libvirt-admin.conf lxc.conf qemu.conf virtlockd.conf
libvirt.conf nwfilter qemu-lockd.conf virtlogd.conf
libvirtd.conf qemu storage #storage目录,所有存储池的XML配置文件
[root@localhost ~]# ls /etc/libvirt/qemu #qemu目录所有qemu有关的配置文件
autostart centos6.5-2.xml centos6.5.xml erp.xml LNMP.xml oa.xml
Base_CentOS7.xml centos6.5-3.xml crm.xml hr.xml networks vm2.xml
[root@localhost ~]# ls /etc/libvirt/qemu/networks/ #qemu里存储所有虚拟网络配置文件的目录networks
autostart default.xml #default.xml这个就是默认的虚拟网络的XML配置文件
[root@localhost ~]# cat /etc/libvirt/qemu/networks/default.xml #查看default.xml内容
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
virsh net-edit default
or other application using the libvirt API.
-->
<network>
<name>default</name> #虚拟网络的名字
<uuid>5687d2e1-c14d-42bb-abe2-fcb4bfac2a12</uuid> #UUID号
<forward mode='nat'/> #虚拟网络的模式NAT
<bridge name='virbr0' stp='on' delay='0'/> #虚拟网络的网桥名称
<mac address='52:54:00:79:e3:41'/>
<ip address='192.168.122.1' netmask='255.255.255.0'> #网桥的IP和掩码
<dhcp>
<range start='192.168.122.2' end='192.168.122.254'/> #DHCP的分发范围
</dhcp>
</ip>
</network>
利用virsh 管理虚拟网络
#virsh里关于网络部分的命令
[root@localhost ~]# virsh help network
Networking (help keyword 'network'):
net-autostart 自动开始网络
net-create 从一个 XML 文件创建一个网络
net-define define an inactive persistent virtual network or modify an existing persistent one from an XML file
net-destroy 销毁(停止)网络
net-dhcp-leases print lease info for a given network
net-dumpxml XML 中的网络信息
net-edit 为网络编辑 XML 配置
net-event Network Events
net-info 网络信息
net-list 列出网络
net-name 把一个网络UUID 转换为网络名
net-start 开始一个(以前定义的)不活跃的网络
net-undefine undefine a persistent network
net-update 更新现有网络配置的部分
net-uuid 把一个网络名转换为网络UUID
#查看所有虚拟网络信息
[root@localhost ~]# virsh net-list
名称 状态 自动开始 持久
----------------------------------------------------------
default 活动 是 是
#查看某虚拟网络详细信息
[root@localhost ~]# virsh net-info default
名称: default
UUID: 5687d2e1-c14d-42bb-abe2-fcb4bfac2a12
活跃: 是
持久: 是
自动启动: 是
桥接: virbr0
#查看某虚拟网络的XML配置文件信息
[root@localhost ~]# virsh net-dumpxml default
<network connections='1'>
<name>default</name>
<uuid>5687d2e1-c14d-42bb-abe2-fcb4bfac2a12</uuid>
<forward mode='nat'>
<nat>
<port start='1024' end='65535'/>
</nat>
</forward>
<bridge name='virbr0' stp='on' delay='0'/>
<mac address='52:54:00:79:e3:41'/>
<ip address='192.168.122.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.122.2' end='192.168.122.254'/>
</dhcp>
</ip>
</network>
什么叫做网桥?网桥到底是怎么回事?
[root@localhost ~]# ifconfig -a
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 #宿主机的真实网卡接口
inet 192.168.200.132 netmask 255.255.255.0 broadcast 192.168.200.255
inet6 fe80::d302:4c4f:17a0:b161 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:96:3a:c5 txqueuelen 1000 (Ethernet)
RX packets 793722 bytes 74452602 (71.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1308099 bytes 2734536899 (2.5 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 #宿主机的lo回环接口
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 739954 bytes 1460949048 (1.3 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 739954 bytes 1460949048 (1.3 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 #虚拟网桥(虚拟交换机)virbr0
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:79:e3:41 txqueuelen 1000 (Ethernet)
RX packets 2780 bytes 222708 (217.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3652 bytes 360625 (352.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0-nic: flags=4098<BROADCAST,MULTICAST> mtu 1500 #连接到网桥virbr0上的宿主机的虚拟网卡接口
ether 52:54:00:79:e3:41 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 ##连接到virbr0上的虚拟机的网卡接口
inet6 fe80::fc54:ff:fe0c:8bd2 prefixlen 64 scopeid 0x20<link>
ether fe:54:00:0c:8b:d2 txqueuelen 1000 (Ethernet)