目录
🍎虚拟机的网络模式
🍒网络配置文件
🍒virsh查看命令
🍎基于NAT的虚拟网络
🍎基于网桥的虚拟网络
🍎基于隔离的虚拟网络
🦐博客主页:大虾好吃吗的博客
🦐专栏地址:KVM虚拟化专栏
虚拟机的网络模式
基于NAT(Network Address Translation)的虚拟网络
基于网桥(Bridge)的虚拟网络
用户自定义的隔离的虚拟网络
直接分配网络设备(包括VT-d和SR-IOV)
网络配置文件
下面是默认网络配置文件,地址是122网段,默认的网络为NAT模式。
[root@kvmserver ~]# cat /etc/libvirt/qemu/networks/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>d01fdb52-9f49-4e03-a2d4-fd6382f11396</uuid>
<forward mode='nat'/>
<bridge name='virbr0' stp='on' delay='0'/>
<mac address='52:54:00:fb:02:fe'/>
<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>
virsh查看命令
[root@kvmserver ~]# virsh net-list
Name State Autostart Persistent
----------------------------------------------------------
default active yes yes
[root@kvmserver ~]# virsh net-info default
Name: default
UUID: d01fdb52-9f49-4e03-a2d4-fd6382f11396
Active: yes
Persistent: yes
Autostart: yes
Bridge: virbr0
[root@kvmserver ~]# virsh net-dumpxml default
<network connections='2'>
<name>default</name>
<uuid>d01fdb52-9f49-4e03-a2d4-fd6382f11396</uuid>
<forward mode='nat'>
<nat>
<port start='1024' end='65535'/>
</nat>
</forward>
<bridge name='virbr0' stp='on' delay='0'/>
<mac address='52:54:00:fb:02:fe'/>
<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>
基于NAT的虚拟网络
默认宿主机会有一个虚拟网卡virbr0,其实是一个虚拟交换机,默认模式就不在详细介绍了。
[root@kvmserver ~]# ifconfig virbr0
virbr0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:fb:02:fe 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
基于网桥的虚拟网络
首先需要创建虚拟交换机,可以使用virt-manager来创建,也可以修改网络配置文件。
[root@kvmserver ~]# cd /etc/sysconfig/network-scripts/
[root@kvmserver network-scripts]# cp ifcfg-ens33 ifcfg-br10
[root@kvmserver network-scripts]# vim ifcfg-br10
DEVICE=br10
TYPE=Bridge
BOOTPROTO=static
NAME=br10
ONBOOT=yes
IPADDR=192.168.8.100
GATEWAY=192.168.8.2
DNS1=8.8.8.8
[root@kvmserver network-scripts]# vim ifcfg-ens33
TYPE=Ethernet
NAME=ens33
DEVICE=ens33
ONBOOT=yes
BRIDGE=br10
[root@kvmserver network-scripts]# systemctl restart network
查看br10是否配置成功。
[root@kvmserver ~]# ip a show br10
11: br10: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 00:0c:29:bb:33:e4 brd ff:ff:ff:ff:ff:ff
inet 192.168.8.100/24 brd 192.168.8.255 scope global noprefixroute br10
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:febb:33e4/64 scope link
valid_lft forever preferred_lft forever
创建虚拟机测试,首先创建dp1池。
[root@kvmserver ~]# virsh pool-define-as dp1 dir --target /dir
Pool dp1 defined
[root@kvmserver ~]# virsh pool-start dp1
Pool dp1 started
[root@kvmserver ~]# virsh pool-autostart dp1
Pool dp1 marked as autostarted
[root@kvmserver ~]# virsh pool-list --all
Name State Autostart
-------------------------------------------
dirpool active yes
dp1 active yes
img active yes
iso active yes
nfspool active yes
test active yes
vgpool active yes
创建在dp1池创建磁盘文件,名称为vol1.qcow2。
[root@kvmserver ~]# virsh vol-create-as dp1 vol1.qcow2 8G --format qcow2
Vol vol1.qcow2 created
[root@kvmserver ~]# virsh vol-list dp1
Name Path
------------------------------------------------------------------------------
vol1.qcow2 /dir/vol1.qcow2
安装虚拟机测试网络,桥接模式的网络应是可以访问外网,而外网也可以访问虚拟机。这里要记得不是一定要重新安装,可以使用attach-interface命令给已有虚拟机添加网络。
[root@kvmserver ~]# virt-install -n vm001 -r 1024 --vcpus 1 -l /kvm/iso/centos.iso --disk /dir/vol1.qcow2 -w bridge=br10 --nographics -x "console=ttyS0"
经过安装等待,我们来到了用户登录界面,输入root用户加密码,进入终端。
CentOS Linux 7 (Core)
Kernel 3.10.0-1160.el7.x86_64 on an x86_64
localhost login: root
Password:
直接使用dhcp获取地址,来测试网络
[root@localhost ~]# dhclient
[root@localhost ~]# ip a show ens3
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 52:54:00:14:8f:0b brd ff:ff:ff:ff:ff:ff
inet 192.168.8.131/24 brd 192.168.8.255 scope global dynamic ens3
valid_lft 1670sec preferred_lft 1670sec
[root@localhost ~]# ping www.baidu.com -c 4
PING www.a.shifen.com (182.61.200.7) 56(84) bytes of data.
64 bytes from 182.61.200.7 (182.61.200.7): icmp_seq=1 ttl=128 time=34.7 ms
64 bytes from 182.61.200.7 (182.61.200.7): icmp_seq=2 ttl=128 time=45.0 ms
64 bytes from 182.61.200.7 (182.61.200.7): icmp_seq=3 ttl=128 time=75.7 ms
64 bytes from 182.61.200.7 (182.61.200.7): icmp_seq=4 ttl=128 time=81.2 ms
--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3027ms
rtt min/avg/max/mdev = 34.754/59.204/81.264/19.735 ms
打开windows终端,ping虚拟机成功的话,双向通信完成。未成功则检查网络信息。
基于隔离的虚拟网络
查看虚拟机网络配置文件都有哪些
[root@kvmserver ~]# cd /etc/libvirt/qemu/networks/
[root@kvmserver networks]# ls
autostart default.xml
创建一个用户定义的隔离网络配置文件
[root@kvmserver networks]# vim dir.xml
<network>
<name>dir</name>
<uuid>7b0b9f14-ff50-4f8f-ac0f-9adcc4714441</uuid>
<bridge name='virbr1' stp='on' delay='0'/>
<mac address='52:54:00:65:0e:f9'/>
<domain name='dir'/>
<ip address='192.168.100.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.100.128' end='192.168.100.254'/>
</dhcp>
</ip>
</network>
把网络加入域并查看
[root@kvmserver networks]# virsh net-define dir.xml
Network dir defined from dir.xml
[root@kvmserver networks]# virsh net-start dir
Network dir started
[root@kvmserver networks]# virsh net-autostart dir
Network dir marked as autostarted
[root@kvmserver networks]# virsh net-list --all
Name State Autostart Persistent
----------------------------------------------------------
default active yes yes
dir active yes yes
在dp1池中创建磁盘文件
[root@kvmserver networks]# virsh vol-create-as dp1 vol2.qcow2 8G --format qcow2
Vol vol2.qcow2 created
[root@kvmserver networks]# virsh vol-list dp1
Name Path
------------------------------------------------------------------------------
vol1.qcow2 /dir/vol1.qcow2
vol2.qcow2 /dir/vol2.qcow2
创建虚拟机,在创建好的磁盘文件中创建vm002虚拟机,网络状态指定dir(dir是上面配置的配置文件dir.xml)等待配置测试即可。
[root@kvmserver networks]# virt-install -n vm002 -r 1024 --vcpus 1 -l /kvm/iso/centos.iso --disk /dir/vol2.qcow2 -w network=dir --nographics -x "console=ttyS0"
测试结果应为完全独立的网络,上不了外网,而外网也无法访问内网。